mcrypt的与PHP和C#的base64 [英] Mcrypt and base64 with PHP and c#
问题描述
我在两个平台,我认为应该得到同样的事情,但它不是写发生同样的方法。我曾用加密从而导致不同的相同的密钥相同的文本。有人能弄清楚为什么会发生?
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 == 的
PHP encrypted string: 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填充默认情况下,使用PKCS7。
C# does Rijndael padding by default and uses PKCS7.
这意味着你要垫你的PHP侧按PKCS7,code以下应该工作:
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);
}
有关详细信息,请参阅<一个href=\"http://stackoverflow.com/questions/4329260/cross-platform-php-to-c-sharp-net-encryption-decryption-with-rijndael\">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的与PHP和C#的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!