I am attempting to convert a C# function into 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();
// Base64 encode the encrypted data
RetVal = Convert.ToBase64String(Buffer.ToArray());
}
catch
{
throw;
}
}
return RetVal;
}
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 a test phrase "test" using these two functions, I get different values. It looks like the PHP version takes a string for $key
and $iv
values, where the C# version takes an array of bytes.
How would I modify my PHP function to mimic the C# function?
[edit] the c# function is 3rd party and I have no access to change it; I need to write the equivalent in PHP to encode a given string in the same manner