我有一个 C# 代码来解密数据,现在我试图在 PHP 中做同样的事情,但我不能得到相同的结果。我知道我遗漏了一些东西,但我不知道如何将其转移到 PHP。有任何想法吗?谢谢。
这是 C# 代码:
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);
}
这是试图做同样的PHP代码(我知道它不完整):
function Decrypt($data, $key) {
$method = 'rc2-cbc';
$iv = '0000000000000000';
return utf8_encode(openssl_decrypt($data, $method, sha1($key), OPENSSL_ZERO_PADDING, $iv));
}