所以它使用的256塊大小,但現在的hello world看起來像這樣http://pastebin.com/5sXhMV11,我不能找出我應該RTRIM使用()來我所進行的更改到C#代碼在最後得到一團糟。
另外,當你說IV應該是隨機的,這是否意味着不要使用相同的IV多於一次,或者是我編寫錯誤的方式?
再次感謝!
嗨,
我試圖解密與在C#中被加密PHP的字符串。我似乎無法讓PHP使用mcrypt解密,並可以做一些幫助。我得到以下錯誤與PHP,所以我猜我沒有正確設置IV。
錯誤:IV參數必須是隻要塊大小
兩個函數都使用相同的密碼,密鑰,IV和設置爲CBC模式:
加密文本從C#= UmzUCnAzThH0nMkIuMisqg ==
鍵32長= qwertyuiopasdfghjklzxcvbnmqwerty
IV 16長= 123456789
C#
public static string EncryptString(string message, string KeyString, string IVString)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
try
{
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
encrypted = Convert.ToBase64String(encoded);
ms.Close();
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
return null;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
rj.Clear();
}
return encrypted;
}
PHP
var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;
function decrypt($key, $iv, $encrypted)
{
$encrypted = base64_decode($encrypted);
$decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
return $decrypted;
}
感謝
IV應該真的是隨機的。如果不是這樣的話,它就會失敗。 – quantumSoup 2010-08-07 21:10:09
帶有256位塊的Rijndael是非標準的。 – kroiz 2014-12-16 08:22:16