I have to implement some TripleDES encryption in my PHP application to communicate with a C#/.NET application, but I get different results compared to the server-side C#/.NET application. I already tried to use different encodings and flags, without much success.
I am already able to decrypt the string from C#/.NET and get the desired result in PHP (based on examples on StackOverflow and other sites).
The server-side C#/.NET code can be found and executed here:
https://dotnetfiddle.net/OrwaOl
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class Program
{
public static void Main()
{
Console.WriteLine(Encrypt("SourceString123", "fsgerh", "Dataehaeh"));
Console.WriteLine(Decrypt("Vql0pOr7ouxfUScI3H8exeFbfmcfE5uM3TBtAZXAq0zwkJmqEe13EA==", "fsgerh", "Dataehaeh"));
}
public static string Encrypt(string value, string password, string salt)
{
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateEncryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream())
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.Unicode))
{
writer.Write(value);
}
}
return Convert.ToBase64String(buffer.ToArray());
}
}
public static string Decrypt(string text, string password, string salt)
{
DeriveBytes rgb = new Rfc2898DeriveBytes(password, Encoding.Unicode.GetBytes(salt));
SymmetricAlgorithm algorithm = new TripleDESCryptoServiceProvider();
byte[] rgbKey = rgb.GetBytes(algorithm.KeySize >> 3);
byte[] rgbIV = rgb.GetBytes(algorithm.BlockSize >> 3);
ICryptoTransform transform = algorithm.CreateDecryptor(rgbKey, rgbIV);
using (MemoryStream buffer = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream stream = new CryptoStream(buffer, transform, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Unicode))
{
return reader.ReadToEnd();
}
}
}
}
}
The "cient"-side PHP code can be found and executed here:
http://phpfiddle.org/lite/code/hkgu-n1fe
<?php
$decrypted = 'SourceString123';
$encrypted = 'Vql0pOr7ouxfUScI3H8exeFbfmcfE5uM3TBtAZXAq0zwkJmqEe13EA==';
$password = 'fsgerh';
$salt = 'Dataehaeh';
$hash = hash_pbkdf2("sha1", $password, mb_convert_encoding($salt, 'UTF-16LE'), 1000, 32, true);
$key = substr($hash, 0, 24);
$iv = substr($hash, 24, 8);
echo base64_encode($key) . "<br />";
echo base64_encode($iv) . "<br /><br />";
$result = mb_convert_encoding(openssl_decrypt($encrypted, 'des-ede3-cbc', $key, 0, $iv), 'UTF-8', 'UTF-16');
echo $result;
echo "<br /><br />";
$result = openssl_encrypt(mb_convert_encoding($decrypted, 'UTF-8'), 'des-ede3-cbc', $key, 0, $iv);
echo $result;
echo "<br /><br />";
$result = openssl_encrypt(mb_convert_encoding($decrypted, 'UTF-16'), 'des-ede3-cbc', $key, 0, $iv);
echo $result;
echo "<br /><br />";
?>
I spent hours to figure out the problem, but haven't been able yet.
I would really appreciate if anybody can spot the problem or point me into the right direction.