I try to convert C# encryption to PHP openssl Encryption, but the outside i different. I already use the same encryption method, but output still different, can someone advise me, which part i make mistake ?
C#
public static void Main()
{
Console.WriteLine(Encrypt("testt123boowe456", "{\"my_account\":\"100081\",\"status\":\"2\",\"ID\":\"3\",\"message\":\"Approved\"}"));
}
public static string Encrypt(string strKey, string strData)
{
try
{
byte[] key = Encoding.UTF8.GetBytes(strKey);
byte[] iv = Encoding.UTF8.GetBytes("13313310");
byte[] data = Encoding.UTF8.GetBytes(strData);
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
tdsp.Mode = CipherMode.CBC;
tdsp.Padding = PaddingMode.PKCS7;
// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
tdsp.CreateEncryptor(key, iv),
CryptoStreamMode.Write);
// Write the byte array to the crypto stream and flush it.
cStream.Write(data, 0, data.Length);
cStream.FlushFinalBlock();
// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();
// Close the streams.
cStream.Close();
mStream.Close();
// Return the encrypted buffer.
return byteToHexString(ret); //Convert.ToBase64String(ret);
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
public static string byteToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder(bytes.Length * 2 + 32);
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
}
return hexString;
}
PHP
public function encrypt()
{
$data = [
'my_account' => "100081",
'status' => "2",
'ID' => "3",
'message' => 'Approved'
];
$stringData = json_encode($data);
$key = utf8_encode("testt123boowe456");
$iv = utf8_encode("13313310");
$result = openssl_encrypt($this->zeroPadding(utf8_encode($stringData), 8), 'DES-EDE3-CBC', $key, 0, $iv);
return bin2hex($result);
}
C# output
A5C56BEAC60ECAD0CFC9D44898EF4B159BC8E1FB318F83ADD6E474BEA17D0C6DF0F20C672FE0372F304DCB0A001A6005828702C54CA301AF06D49F21FF48260C15D278D2841963FF
PHP output
2f73547a753951745135785373564d38444757564a534666425a41744c6e38666161732f724d2b42335276536c76474450327343353675795a795577577850444a4c4668315a385a32463142676549676b6448587347416a2b684d71364d73734870744374797677744d513d