I have the following C # code that generates keys:
public static byte[] Encrypt(byte[] plainData, string salt) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(salt); DES.IV = ASCIIEncoding.ASCII.GetBytes(salt); ICryptoTransform desencrypt = DES.CreateEncryptor(); byte[] encryptedData = desencrypt.TransformFinalBlock(plainData, 0, plainData.Length); return encryptedData; } private string GetEncryptedKey(string key) { return BitConverter.ToString(KeyGeneratorForm.Encrypt(ASCIIEncoding.ASCII.GetBytes(key), "abcdefgh")).Replace("-", ""); }
I am trying to do the exact same thing in PHP:
function get_encrypted_key($key){ $salt = "abcdefgh"; return bin2hex(mcrypt_encrypt(MCRYPT_DES, $salt, $key, MCRYPT_MODE_CBC, $salt)); }
However, there is a slight discrepancy in the results, since the last 16 characters are always different:
With key "Benjamin Franklin": C
I also tried converting padding to my key using the following code:
function get_encrypted_key($key){ $salt = "abcdefgh"; $extra = 8 - (strlen($key) % 8); if($extra > 0) { for($i = 0; $i < $extra; $i++) { $key.= "\0"; } } return bin2hex(mcrypt_encrypt(MCRYPT_DES, $salt, $key, MCRYPT_MODE_CBC, $salt)); }
But I get the same results as without filling.
If you have any clue as to what is happening, I would be happy to hear about it! :)
thanks
karlipoppins
source share