Php 从 C# .NET RIJNDAEL 解密字符串 256
修好了。
$data = base64_decode(str_replace(' ', '+', $_GET['data']));
无论出于何种原因,Php 将 GET 变量中的 + 转换为空格
-
我正在尝试解密正在 C#.NET 中解密的字符串。
代码的结果各不相同,有几次最终字符串的某些部分被解密,其余部分是随机字符。
大多数时候“解密”的字符串都是随机字符,我也尝试了一些 Php 函数来删除 PKCS7 填充,但没有一个解决问题。
我在网站上查看了几个类似的问题,但没有一个有帮助。
C#
// called as Response.Redirect(url + encryptParams(param));
private string encryptData(string data)
{
Rijndael aes = Rijndael.Create();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String("b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo=");
ICryptoTransform crypto = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] txt = ASCIIEncoding.UTF8.GetBytes(data);
byte[] cipherText = crypto.TransformFinalBlock(txt, 0, txt.Length);
return "&data=" + Convert.ToBase64String(cipherText) + "&iv=" + Convert.ToBase64String(aes.IV);
}
Php:
$data = base64_decode($_GET['data']);
$iv = base64_decode($_GET['iv']);
echo "<br /><b>IV</b>: " . $_GET['iv'] .
"<br /><b>Encrypted String</b>: <br /><textarea>".$_GET['data']."</textarea>" .
"<br /><b>key size:</b> " . mcrypt_get_key_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) .
"<br /><b>block size:</b> " . mcrypt_get_block_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) .
"<br /><b>cipher:</b> " . mcrypt_get_cipher_name ( MCRYPT_RIJNDAEL_256 ) .
"<br /><b>iv size:</b> " . mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) . "<br />";
echo "Result: " .
mcrypt_decrypt
(
MCRYPT_RIJNDAEL_256,
base64_decode("b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo="),
$data,
MCRYPT_MODE_CBC,
$iv
);
Php 输出:
IV: WzsMlG39tfCGuX2EQM3vq8CoqGA xC0nW jICls8Cno=
密钥:b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo=
加密字符串: olxa21fxfQGg0EJ5rwMjEzMblvcaTq0AInDASD88wAkNeLqOdon0ukLjz49Hpp36KPTKcTGkj1f7EPYPAAbuADnr3Ff0zpptZkx2d22VRbHrMgj QLF9vDxQRT3er3UAXsAfKKTyW8qeSIgrzACFLX3yoro/bzWic rt7ED7y0jZ7a1Hci3GMz/4KhwaftarbV QQWStJlSOqdxAdmtRRe84Vi3085S6um51bNrh5QzGRH PcpucfqaTb3junfO9g67j2JUQaM/Tj1EGnv6oX3wATR/LuWyhnhrCH86u10I=
密钥大小: 32
块大小: 32
密码: Rijndael-256
iv 大小: 32
结果: /ci�����^/�c�g�������s��c�(
�� 原始字符串 (JSON): {"user":"jsmith","firstName":"John","lastName":"Smith","phone":"12223334444.5555","email":"[电子邮件受保护]","address":"123 Some Street","address2":"apt 456","city":"Some City","state" :"洛杉矶","邮政编码":"55555"}
Fixed it.
$data = base64_decode(str_replace(' ', '+', $_GET['data']));
for whatever reason, Php was converting the +'s from the GET variablesinto spaces
--
I am trying to decrypt a string that is being decrypted in C#.NET.
The results of the code vary, There were several occasions where the final string had some parts decrypted, and the rest of it was random characters.
Most of the time the "decrypted" string is just all random characters, I also tried some Php functions to remove PKCS7 padding but none of them fixed the problem.
I've looked at several similar questions on the site but none of them were of help.
C#
// called as Response.Redirect(url + encryptParams(param));
private string encryptData(string data)
{
Rijndael aes = Rijndael.Create();
aes.KeySize = 256;
aes.BlockSize = 256;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = Convert.FromBase64String("b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo=");
ICryptoTransform crypto = aes.CreateEncryptor(aes.Key, aes.IV);
byte[] txt = ASCIIEncoding.UTF8.GetBytes(data);
byte[] cipherText = crypto.TransformFinalBlock(txt, 0, txt.Length);
return "&data=" + Convert.ToBase64String(cipherText) + "&iv=" + Convert.ToBase64String(aes.IV);
}
Php:
$data = base64_decode($_GET['data']);
$iv = base64_decode($_GET['iv']);
echo "<br /><b>IV</b>: " . $_GET['iv'] .
"<br /><b>Encrypted String</b>: <br /><textarea>".$_GET['data']."</textarea>" .
"<br /><b>key size:</b> " . mcrypt_get_key_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) .
"<br /><b>block size:</b> " . mcrypt_get_block_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) .
"<br /><b>cipher:</b> " . mcrypt_get_cipher_name ( MCRYPT_RIJNDAEL_256 ) .
"<br /><b>iv size:</b> " . mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC) . "<br />";
echo "Result: " .
mcrypt_decrypt
(
MCRYPT_RIJNDAEL_256,
base64_decode("b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo="),
$data,
MCRYPT_MODE_CBC,
$iv
);
Php output:
IV: WzsMlG39tfCGuX2EQM3vq8CoqGA xC0nW jICls8Cno=
key: b0dJN2c6cklVUX1qUGlFfGMweXRKbH5fSEMuXjAgfQo=
Encrypted String: oLxa21fxfQGg0EJ5rwMjEzMblvcaTq0AInDAsD88wAkNeLqOdon0ukLjz49Hpp36KPTKcTGkj1f7EPYPAAbuADnr3Ff0zpptZkx2d22VRbHrMgj QLF9vDxQRT3er3UAXsAfKKTyW8qeSIgrzACFLX3yoro/bzWic rt7ED7y0jZ7a1Hci3GMz/4KhwaftarbV QQWStJlSOqdxAdmtRRe84Vi3085S6um51bNrh5QzGRH PcpucfqaTb3junfO9g67j2JUQaM/Tj1EGnv6oX3wATR/LuWyhnhrCH86u10I=
key size: 32
block size: 32
cipher: Rijndael-256
iv size: 32
Result:
/ci�����^/�c�g�������s��c�(��
Original String (JSON):
{"user":"jsmith","firstName":"John","lastName":"Smith","phone":"12223334444.5555","email":"[email protected]","address":"123 Some Street","address2":"apt 456","city":"Some City","state":"LA","zip":"55555"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下代码修复了它。
无论出于何种原因,PHP 将 GET 变量中的 + 转换为空格。
Fixed it by using the following code.
For whatever reason, PHP was converting the +'s from the GET variablesinto spaces.