php - MCRYPT in .Net -


I have a PHP decryption algorithm at the end, and on the other there is a net encryption algorithm. I am using mcrypt_decrypt for the decrypt purpose in PHP code, and I am looking for the equivalent of mcrypt_encrypt . More specifically, the PHP code that I , It is the following:

  $ cipher_alg = MCRYPT_RIJNDAEL_128; $ Iv = mcrypt_create_iv (mcrypt_get_iv_size ($ cipher_alg, MCRYPT_MODE_ECB), MCRYPT_RAND); $ Encrypted_string = mcrypt_encrypt ($ cipher_alg, $ session key, $ string, MCRYPT_MODE_CBC, $ iv); $ Hex_encrypted_string = bin2hex ($ encrypted_string);  

Is the mcrypt_encrypt in the net equal to?

I have come together

here is an excerpt:

String String CreateEncryptedString (string mystring, string hexiv, string key) {RijndaelManaged alg = new RijndaelManaged (); Alg.Padding = PaddingMode.Zeros; Alg.Mode = Ciphermode.cBC; Alg.BlockSize = 16 * 8; Alg.Key = ASCIIEncoding.UTF8.GetBytes (key); Alg.IV = StringTobetre (hexiv); ICryptoTransform encryptor = alg.CreateEncryptor (alg.Key, alg.IV); Memorystream msstream = new memorystream (); Cryptoestream mcsWriter = new cryptostream (MSSTream, encryptor, cryptostarm mode.); StreamWriter mSWriter = new streamer (mcsWriter); MSWriter.Write (myString); MSWriter.Flush (); McsWriter.FlushFinalBlock (); Var encrypted bit = new byte [MSSTream.length]; MsStream.Position = 0; MsStream.Read (encryptedbite, 0, (int) msStream.Length); Return ByteArrayToHexString (EncryptedByte); } Public Static Byte [] StringTobetre (String Hex) {Int Number Charge = Hex. Langith; Byte [] bytes = new byte [number chars / 2]; For (int i = 0; i and lieutenant; number chars; i + = 2) bytes [i / 2] = convert Toight (Hex.Substring (I, 2), 16); Return bytes; } Public Static String Bacteriatexexeter (Byte [] BA) {StringBinder HEX = New StringBuilder (BA. Long * 2); Foreign Hair (Byte B in BA) Hex Apxfarmet ("{0: x2}", b); Return hex Toasting (); }

There is one thing to note. The main size should be 16. This means that the key parameter should be a string of 16 characters. Or encryption will not work and throw cryptographic expansion.

PHP Finally, here's how you decoding:

  $ cipher_alg = MCRYPT_RIJNDAEL_128; $ Decrypted_string = mcrypt_decrypt ($ cipher_alg, $ key, $ encrypted_string, MCRYPT_MODE_CBC, trim (hex2bin (trim ($ hexiv)));  

Comments