[PHP] Converting PHP code to C#?
Hi, I'm not sure if this is the right place to post this, I have some decryption routines that use mcrypt that I need to decrypt in .NET, does anyone know how why I cant get this to work? PHP $mcryptSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $mcryptIV = mcrypt_create_iv($mcryptSize, MCRYPT_RAND); $mcryptData = pack("H*", $data); $decryptData = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,$mcryptData,MCRYPT_MODE_ECB,$mcryptIV); - $decryptData should be 2048 in size. Here's my C# converted bits: - C# RijndaelManaged rj = new RijndaelManaged(); rj.Mode = CipherMode.ECB; rj.Key = ASCIIEncoding.ASCII.GetBytes(KEY); rj.KeySize = 128; rj.GenerateIV(); rj.Padding = PaddingMode.Zeros; ICryptoTransform trans = rj.CreateDecryptor(rj.Key, rj.IV); byte[] Buffer = Convert.FromBase64String(DATA); string dataD = ASCIIEncoding.ASCII.GetString(trans.TransformFinalBlock(Buffer,0, Buffer.Length)); - However the length is 3017 Any ideas? Thanks, Sym -- View this message in context: http://www.nabble.com/Converting-PHP-code-to-C---tf4397727.html#a12541304 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Converting PHP code to C#?
Mike, Thanks for the reply and your very useful notes. I'm unable to workout how to get the same IV as the one generated by the PHP script is random. The thing is that we cant change the PHP script as its readily being used now. Maybe I should look at the encryption routine and reverse that first? Thanks, Sym mike-22 wrote: > > I've done it in reverse - something encrypted in .NET and then decrypted > in PHP > > these were my notes... > > > To get .NET and PHP to play friendly with two-way encryption, you need > to make sure some things happen in both: > In .NET: > 1) You need to set the Padding to PaddingMode.Zeros, i.e.: > Rijndael alg = Rijndael.Create(); > alg.Padding = PaddingMode.Zeros; > 2) You also need to make sure to use System.Text.Encoding.ASCII or > System.Text.Encoding.UTF8; System.Text.Encoding.Unicode will *not* > work (perhaps in PHP6 this might be possible.) > 3) Need to make sure the IV and key are the same as defined in PHP. > > In PHP: > 1) You need the mcrypt extension installed. > 2) Need to make sure the IV and key are the same as defined in .NET. > 3) You can issue this single line of code to decrypt: > mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted_text, "cbc", $iv); > > Notes: > 1) By default .NET uses a 128-bit cipher in CBC mode when you > initialize Rijndael. That might not be common knowledge. > 2) If you're sending this data via a URL or cookie or something else, > be sure to base64 encode on the .NET side, and base64_decode() the > data on the PHP side. > > > below i would say that: > > a) you need to make sure the IV is the same. right now it looks like > you are creating a random one in PHP and a different one in .NET. that > would be my first thing to check. > > b) not sure if ECB vs. CBC is any different; i know CBC will work though. > > hope that helps some. it took some debugging for us, and it didn't > help that our .NET guy created the IV using low and high ascii > character codes that I had to reproduce in PHP for the IV and the > key... I would get different sizes as well, but once the stars aligned > it worked perfectly. Be sure that any base64 encoding/decoding or > anything like that is done in the proper order (typically start out > with no encoding, get it to work, then add on encoding and decoding on > both ends properly, etc.) > > good luck :) > -- View this message in context: http://www.nabble.com/Converting-PHP-code-to-C---tf4397727.html#a12565713 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Converting PHP code to C#?
mike-22 wrote: > > just hard code the IV in both places. > Thanks for the link, that was most useful, especially the commented bits! RE: The IV, reading the php manual it states that the IV is not used when ECB mode is used (which is what we are using). So knowing this does the use of IV matter? I stripped the decrypt routine from the PHP file (big file!) and tried it on a seperate PHP page without parsing a IV like so: mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,$mcryptData,MCRYPT_MODE_ECB,""); While i got an error from the module: Warning: mcrypt_decrypt() [function.mcrypt-decrypt]: The IV parameter must be as long as the blocksize in crypto.php on line 38 It still decrypted it fine on the php page. -- View this message in context: http://www.nabble.com/Converting-PHP-code-to-C---tf4397727.html#a12575594 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Pack function in C#
hello, Our PHP dev (who has since left) uses the pack function to turn a base64 string into a binary blob (?): $blob = pack("H*", $postBase64Data); Does anyone know what the above is doing? I need to translate that to C# terms, which I thought was getting the eqivalent of the bytes: byte[] blob = ASCIIEncoding.UTF8.GetBytes(postedData); But this may not be right. Sym -- View this message in context: http://www.nabble.com/Pack-function-in-C--tf4407990.html#a12575709 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Converting PHP code to C#?
mike-22 wrote: > > i'm pretty sure the IV mattered in our stuff. > > and remember i used CBC i think not EBC, and it worked fine. not sure > if you want to try that and make it work for you or not without any > warnings :) > Ah! We're using EBC, also I realised that I needed to implement the eqivalent of the pack function in PHP which we use, so I finally have the correct (MD5'd both the PHP and C# bits) string going *into* the function. Now I have to wrestle with the Rijndael class again:( Rijndael r = Rijndael.Create(); r.Mode = CipherMode.ECB; r.Padding = PaddingMode.None; r.Key = Encoding.ASCII.GetBytes(KEY); ICryptoTransform de = r.CreateDecryptor(); byte[] output = CryptoTransform(input, de); Sym -- View this message in context: http://www.nabble.com/Converting-PHP-code-to-C---tf4397727.html#a12576182 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php