I am trying to convert a C # function to PHP.
Here is the C # function:
public string Encrypt(string Value) { string RetVal = ""; if(Value != string.Empty && Value != null) { MemoryStream Buffer = new MemoryStream(); RijndaelManaged RijndaelManaged = new RijndaelManaged(); UnicodeEncoding UnicodeEncoder = new UnicodeEncoding(); byte[] KeyArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; byte[] IVArray = new Byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; try { byte[] ValueBytes = UnicodeEncoder.GetBytes(Value); CryptoStream EncryptStream = new CryptoStream(Buffer, RijndaelManaged.CreateEncryptor(KeyArray, IVArray), CryptoStreamMode.Write); EncryptStream.Write(ValueBytes, 0, ValueBytes.Length); EncryptStream.FlushFinalBlock();
and here is my attempt in PHP:
function EncryptString ($cleartext) { $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); $key128 = '111111111111111111111111111'; $iv = '111111111111111111111111111'; if (mcrypt_generic_init($cipher, $key128, $iv) != -1) //Parameter iv will be ignored in ECB mode { $cipherText = mcrypt_generic($cipher,$cleartext ); mcrypt_generic_deinit($cipher); $encrypted = (bin2hex($cipherText)); return base64_encode($encrypted); } }
Currently, when I encode the test phrase "test" using these two functions, I get different values. It looks like the PHP version takes a string for the values ββof $key and $iv , where the C # version takes an array of bytes.
How do I change my PHP function to mimic a C # function?
[edit] C # function is a third party, and I do not have access to change it; I need to write an equivalent in PHP to encode a given string in the same way