问题描述
我在两个平台上编写了相同的方法,我认为这些方法应该会产生相同的结果,但它没有发生.我用相同的密钥加密了相同的文本,结果不同.有人能弄清楚为什么会这样吗?
I have written the same methods in two platforms which I believe should result same thing but its not happening. I have encrypted the same text with same key which result different. Can someone figure it out why is it happening ?
字符串:这是测试
密钥:1234567812345678
PHP 加密字符串:ybUaKwQlRNwOjJhxLWtLYQ==
C# 加密字符串:r2YjEFPyDDacnPmDFcGTLA==
C# 函数
static string Encrypt(string plainText, string key)
{
string cipherText;
var rijndael = new RijndaelManaged()
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.ECB,
BlockSize = 128,
};
ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, rijndael.IV);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(plainText);
streamWriter.Flush();
}
cipherText = Convert.ToBase64String(memoryStream.ToArray());
//cryptoStream.FlushFinalBlock();
}
}
return cipherText;
}
private static string Decrypt(string cipherText, string key)
{
string plainText;
byte[] cipherArray = Convert.FromBase64String(cipherText);
var rijndael = new RijndaelManaged()
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.ECB,
BlockSize = 128
};
ICryptoTransform decryptor = rijndael.CreateDecryptor(rijndael.Key, rijndael.IV);
using (var memoryStream = new MemoryStream(cipherArray))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
using (var streamReader = new StreamReader(cryptoStream))
{
plainText = streamReader.ReadToEnd();
}
}
}
return plainText;
}
PHP 函数
function string_encrypt($string, $key) {
$crypted_text = mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$key,
$string,
MCRYPT_MODE_ECB
);
return base64_encode($crypted_text);
}
function string_decrypt($encrypted_string, $key) {
return mcrypt_decrypt(
MCRYPT_RIJNDAEL_128,
$key,
base64_decode($encrypted_string),
MCRYPT_MODE_ECB
);
}
我不太擅长 C#,但我知道 PHP 函数运行良好.因此,必须对 C# 函数进行一些处理.可能是要加密的字符串应该转换为拉丁字符.
I am not so good in C# and I know the PHP function is working fine. So, there must be something done on C# functions. May be the string to be encrypted should converted to Latin chars.
推荐答案
C#默认做Rijndael padding,使用PKCS7.
C# does Rijndael padding by default and uses PKCS7.
这意味着你必须按照 PKCS7 填充你的 PHP 端,下面的代码应该可以工作:
This means you have to pad your PHP side as per PKCS7, code below should work:
function string_encrypt($string, $key) {
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
$padding = $block - (strlen($string) % $block);
$string .= str_repeat(chr($padding), $padding);
$crypted_text = mcrypt_encrypt(
MCRYPT_RIJNDAEL_128,
$key,
$string,
MCRYPT_MODE_ECB
);
return base64_encode($crypted_text);
}
有关详细信息,请参阅 first在这里回答
For further information, see the first answer here
我还应该补充一点,如果您想更改 C# 端而不使用填充,请进行以下修改而不是并单独保留 PHP 端:
I should add also, that if you want to change the C# side and not use padding, make the below modification instead and leave the PHP side alone:
static string Encrypt(string plainText, string key)
{
string cipherText;
var rijndael = new RijndaelManaged()
{
Key = Encoding.UTF8.GetBytes(key),
Mode = CipherMode.ECB,
BlockSize = 128,
Padding = PaddingMode.Zeros,
};
ICryptoTransform encryptor = rijndael.CreateEncryptor(rijndael.Key, null);
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (var streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(plainText);
streamWriter.Flush();
}
//cipherText = Convert.ToBase64String(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(memoryStream.ToArray())));
cipherText = Convert.ToBase64String(memoryStream.ToArray());
//cryptoStream.FlushFinalBlock();
}
}
return cipherText;
}
这篇关于Mcrypt 和 base64 与 PHP 和 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!