我目前对解密php mcrypt加密的消息有点问题。 PHP代码是如下:Rijndael加密/解密的跨平台(php to C#.NET)
<?php
//$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$iv = "45287112549354892144548565456541";
$key = "anjueolkdiwpoida";
$text = "This is my encrypted message";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
$crypttext = urlencode($crypttext);
$crypttext64=base64_encode($crypttext);
print($crypttext64) . "\n<br/>";
?>
的经加密的消息然后被发送到一个ASP.NET平台(C#)。但是,我遇到了解密顺序问题(base64解码为urldecode)。我在ASP.NET中的代码是如下述(IV和密钥是相同的在PHP):
public string Decode(string str)
{
byte[] decbuff = Convert.FromBase64String(str);
return System.Text.Encoding.UTF8.GetString(decbuff);
}
static public String DecryptRJ256(string cypher, string KeyString, string IVString)
{
string sRet = "";
RijndaelManaged rj = new RijndaelManaged();
UTF8Encoding encoding = new UTF8Encoding();
try
{
//byte[] message = Convert.FromBase64String(cypher);
byte[] message = encoding.GetBytes(cypher);
byte[] Key = encoding.GetBytes(KeyString);
byte[] IV = encoding.GetBytes(IVString);
rj.Padding = PaddingMode.Zeros;
rj.Mode = CipherMode.CBC;
rj.KeySize = 256;
rj.BlockSize = 256;
rj.Key = Key;
rj.IV = IV;
MemoryStream ms = new MemoryStream(message);
using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Read))
{
using (StreamReader sr = new StreamReader(cs))
{
sRet = sr.ReadToEnd();
}
}
}
finally
{
rj.Clear();
}
return sRet;
}
string temp = DecryptRJ256(Server.UrlDecode(Decode(cypher)), keyString, ivString);
时遇到的问题是,我收到从PHP加密的消息后,我转换它转换为byte [],然后转换回UTF8编码的字符串,以便我可以对它进行urldecode。然后我将结果提供给函数,在函数中我将字符串转换回byte []并通过解密过程运行。然而,我无法得到理想的结果......任何想法?
在此先感谢。
你不需要`urlencode`之前,你`base64_encode`它加密的数据。 – Powerlord 2010-12-01 21:41:55