使用 Rijndael 进行跨平台(php 到 C# .NET)加密/解密
我目前在解密由 php mcrypt 加密的消息时遇到一些问题。 php代码如下:
<?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[] 并通过解密过程运行它。但是,我无法得到想要的结果...有什么想法吗?
提前致谢。
I'm currently having a bit of problem with decrypting a message encrypted by php mcrypt.
The php code is as following:
<?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/>";
?>
The the encrypted message is then sent to a ASP.NET platform (C#). However, I'm having problem retaining the order of decryption (base64 decode to urldecode). The code I had in ASP.NET is as the following (iv and key is the same as in 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);
The problem I'm having is that after I recieved the encrypted message from php, I converted it into byte[] and then converted back to UTF8 encoded string so I can urldecode it. then I feed the result into the function where I converted the string back to byte[] and ran it through the decryption process. However, I can't get the desired result...any ideas?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里我可以看到双方的问题。
请记住,编码时得到的不是字符串,而是字节数组。
所以在 PHP 中你不需要对密文进行 urlencode。
Base64 编码就是您所需要的。当您打开 base64_encode 帮助 时,您会看到
还有一件事 - 要在 .net 中以正确的长度解码消息,您必须手动在其后面附加填充字符。 RijndaelManaged 的默认填充模式是 PKCS7,让我们坚持使用它。您必须将源字符串扩展到偶数块,其字符代码等于填充字节数。
在 C# 端,您可以从 base64 到 byte[] 到 string 到 byte[] 进行转换。您必须仅进行从 base64 到 byte[] 的第一次转换。请记住,base64 保存的是二进制数据的加密文本,而不是字符串。
另请注意,RijndaelManaged 是 IDisposable,因此我将其包装在 using() 构造中。正如 MSDN 中所述,调用 Close() 是必要的,但还不够。
因此,以下 C# 代码将返回初始字符串:
Here I can see problems on both sides.
Please keep in mind that what you get when encoding is not string, but rather an array of bytes.
So in PHP you don't need to urlencode cyphertext.
base64 encoding is all you need. When you open base64_encode help you see
One more thing - to have your message decoded in .net with a correct length, you have to manually append it with padding characters. Default padding mode for RijndaelManaged is PKCS7, lets' stick with it. You have to extend your source string to even blocks with characters code equal to number of padding bytes.
At C# side you have casting from base64 to byte[] to string to byte[]. You have to do the first conversion from base64 to byte[] only. Remember, base64 is holding the cyphered text that is binary data, not string.
Also please note that RijndaelManaged is IDisposable, so I have wrapped it in using() construct. Calling Close() is necessary but not enough as stated in MSDN.
As a result, following code in C# will return you the initial string: