我正在尝试用 C# 重写这个函数。但 C# 输出与 php 不匹配
PHP版本
// Encrypt data using AES128-cbc
function encrypt($data, $key, $iv) {
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
mcrypt_generic_init($cipher, $key, $iv);
$multipass = mcrypt_generic($cipher, $data);
mcrypt_generic_deinit($cipher);
return $multipass;
}
C#版本
public static string encrypt(string encryptionString, string iv, string key)
{
byte[] clearTextBytes = Encoding.UTF8.GetBytes(encryptionString);
var rijn = SymmetricAlgorithm.Create();
rijn.KeySize = 128;
rijn.Mode = CipherMode.CBC;
var ms = new MemoryStream();
var cs = new CryptoStream(ms, rijn.CreateEncryptor(Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(iv)), CryptoStreamMode.Write);
cs.Write(clearTextBytes, 0, clearTextBytes.Length);
cs.Close();
var tmp = Encoding.UTF8.GetString(ms.ToArray());
return tmp;
}