1

I have a C# code to decrypt data, now I'm trying to make the same in PHP but I can't get the same result. I know that I missing something but I don't know how to transfer this to PHP. Any ideas? Thanks.

This is the C# code:

public static string Decrypt(string cipherData, string key)
{
  byte[] salt = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
  byte[] IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

  PasswordDeriveBytes cdk = new PasswordDeriveBytes(key, salt);
  byte[] kex = cdk.CryptDeriveKey("RC2", "SHA1", 128, salt);
  RijndaelManaged rijKey = new RijndaelManaged();
  rijKey.Mode = CipherMode.CBC;

  byte[] textBytes = Convert.FromBase64String(cipherData);
  ICryptoTransform decryptor = rijKey.CreateDecryptor(kex, IV);
  MemoryStream memoryStream = new MemoryStream(textBytes);
  CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
  byte[] pTextBytes = new byte[(textBytes.Length - 1) + 1];
  int decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);

  memoryStream.Close();
  cryptoStream.Close();

  return Encoding.UTF8.GetString(pTextBytes, 0, decryptedByteCount);
}

This is the PHP code trying to make the same (I know that it is incomplete):

function Decrypt($data, $key) {

    $method = 'rc2-cbc';
    $iv = '0000000000000000';

    return utf8_encode(openssl_decrypt($data, $method, sha1($key), OPENSSL_ZERO_PADDING, $iv));
}
epablo
  • 31
  • 1
  • 1
    [link 1](http://stackoverflow.com/questions/3431950/rijndael-256-encrypt-decrypt-between-c-sharp-and-php) [link 2](http://stackoverflow.com/questions/4329260/cross-platform-php-to-c-sharp-net-encryption-decryption-with-rijndael) [link 3](http://stackoverflow.com/questions/6130989/php-decrypt-a-string-from-c-sharp-net-rijndael-256) ...plus 3000 more – Ňɏssa Pøngjǣrdenlarp Nov 28 '14 at 15:57
  • Thanks Plutonix, I will check the links – epablo Nov 28 '14 at 15:59

0 Answers0