php - 在 VB 中加密的 PHP 中的解密
问题描述
我必须解密在 VB 中加密的 PHP 数据。当我解密数字时我没有问题,但是当我解密文本时我只得到前 8 个字符,然后是随机的。这是密钥“a1R@f7D$”这是我要解密的内容:
LwEe+sQCn63m9kjtqiy67ul5R1Ng7SZPVO4YYxQvZtUZBwNTb+Ey0qCNsrczI4jN
我明白了:
{Preinsc]hn��m�ȕ�!��^߇� $! �E&;�e^#S�)6Ui�4�
我尝试使用 MCRYPT_RIJNDAEL_256 和 ecb 但没有一个对我有用。
function decrypt($data ){
$encryption_key = "a1R@f7D$";
$data = urldecode($data);
$key = md5(utf8_encode($encryption_key), true);
//Take first 8 bytes of $key and append them to the end of $key.
$key .= substr($key, 0, 8);
$data = base64_decode($data);
$data = mcrypt_decrypt('tripledes', $key, $data, 'ecb');
$block = mcrypt_get_block_size('tripledes', 'ecb');
$len = strlen($data);
$pad = ord($data[$len-1]);
return substr($data, 0, strlen($data) - $pad);
}
这是对此进行加密的函数:
Public Shared Function tryingTripleDes (ByVal value As String, ByVal key As String) As String
Dim des As New Security.Cryptography.TripleDESCryptoServiceProvider
des.IV des.IV = New Byte(7) {}
Dim pdb As New Security.Cryptography.PasswordDeriveBytes(key, New Byte(-1) {})
des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
Dim ms As New IO.MemoryStream((value.Length * 2) - 1)
Dim encStream As New Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), Security.Cryptography.CryptoStreamMode.Write)
Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)
encStream.Write(plainBytes, 0, plainBytes.Length)
encStream.FlushFinalBlock()
Dim encryptedBytes(CInt(ms.Length - 1)) As Byte
ms.Position = 0
ms.Read(encryptedBytes, 0, CInt(ms.Length))
encStream.Close()
Return Convert.ToBase64String(encryptedBytes)
End Function
解决方案
如果密文是用全零字节 IV 加密的 CBC,通常会发生这种情况。其他模式也可能有效,但 CBC 是最有可能的。你的密钥和密码是正确的,否则你只会得到垃圾作为回报——当然不是清晰的文本。
请注意,这MCRYPT_RIJNDAEL_256
甚至不是 AES,因此尝试这样做是没有用的,您应该使用MCRYPT_RIJNDAEL_128
正确大小的密钥来获取 AES。
我不会使用零字节 IV 和不推荐使用的密码(如 3DES)来探讨 CBC 的安全性。这不是一个好主意,尤其是对于运输安全而言。
推荐阅读
- ios - 来自 HTML 的 SwiftUI 属性字符串使应用程序崩溃
- windows - 如何从 DLL 连接到蓝牙 LE 设备
- architecture - 在 NestJS 中获取解耦模块
- javascript - 如何在第二个 API 函数中访问第一个 API 函数中的变量
- phpstorm - 在之前和之后添加一个新行PhpStorm 中的标记
- python - Python - 将 .gzip 附件添加到电子邮件
- asp.net-core - 如何保证操作和日志成功?
- c# - c# asp net updatepanel 导致页面刷新
- xamarin - Xamarin.Forms 设计开始菜单
- sql - 具有深度嵌套数组循环的 SQL 查询 OpenJson