C# method to decrypt in PHP

146 Views Asked by At

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));
}
0

There are 0 best solutions below