加密:使用 vb.net 我得到的结果与 php 不同
Encryption: with vb.net I get a different result than php
我有这个php代码
$plain_text = "abc";
$salt = "123";
echo $encrypted_text = openssl_encrypt($plain_text, "AES-128-ECB", $salt);
// result: kR/1uaFarptS5+n951MVsQ==
我在vb.net上尝试了几种方法(类和函数),但是用这种语言加密的结果每次都和上面用php加密的结果不一样。
例如这个:
Public Function AES_Encrypt (ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash (31) As Byte
Dim temp As Byte () = Hash_AES.ComputeHash (System.Text.ASCIIEncoding.ASCII.GetBytes (pass))
Array.Copy (temp, 0, hash, 0, 16)
Array.Copy (temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte () = System.Text.ASCIIEncoding.ASCII.GetBytes (input)
encrypted = Convert.ToBase64String (DESEncrypter.TransformFinalBlock (Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function
sEnc = AES_Encrypt("abc", "123")
Console.WriteLine(sEnc)
'result: Z3hCHcS0b2zJ7fEod3jcrw==
拜托,使用 vb.net(没有 C#),我怎样才能得到结果“kR/1uaFarptS5+n951MVsQ==”,使用算法“AES-128-ECB”?
由于PHP代码中的规范AES-128-ECB
,ECB模式使用AES-128,即密钥长度为16字节。但是由于只应用了一个 3 字节的大密钥 (123
),PHP 使用 0x00
值填充到必要的 16 字节大小。注意key太长会被截断
在 VB 代码中使用了一个 32 字节的密钥。由于在 .NET 中密钥大小决定了 AES 变体,因此应用 AES-256。而且,传递的密钥不是直接使用的,而是实际的密钥是从传递的值和摘要MD5中派生出来的。
所以VB代码returns是PHP代码的结果,PHP代码的逻辑必须在VB中实现代码:
...
'Dim hash(31) As Byte
'Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass))
'Array.Copy(temp, 0, hash, 0, 16)
'Array.Copy(temp, 0, hash, 15, 16)
'AES.Key = hash
Dim keyPadded(15) As Byte
Dim key = System.Text.ASCIIEncoding.ASCII.GetBytes(pass)
Array.Copy(key, 0, keyPadded, 0, Math.Min(16, key.Length))
AES.Key = keyPadded
...
几点说明:
我有这个php代码
$plain_text = "abc";
$salt = "123";
echo $encrypted_text = openssl_encrypt($plain_text, "AES-128-ECB", $salt);
// result: kR/1uaFarptS5+n951MVsQ==
我在vb.net上尝试了几种方法(类和函数),但是用这种语言加密的结果每次都和上面用php加密的结果不一样。 例如这个:
Public Function AES_Encrypt (ByVal input As String, ByVal pass As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash (31) As Byte
Dim temp As Byte () = Hash_AES.ComputeHash (System.Text.ASCIIEncoding.ASCII.GetBytes (pass))
Array.Copy (temp, 0, hash, 0, 16)
Array.Copy (temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte () = System.Text.ASCIIEncoding.ASCII.GetBytes (input)
encrypted = Convert.ToBase64String (DESEncrypter.TransformFinalBlock (Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function
sEnc = AES_Encrypt("abc", "123")
Console.WriteLine(sEnc)
'result: Z3hCHcS0b2zJ7fEod3jcrw==
拜托,使用 vb.net(没有 C#),我怎样才能得到结果“kR/1uaFarptS5+n951MVsQ==”,使用算法“AES-128-ECB”?
由于PHP代码中的规范AES-128-ECB
,ECB模式使用AES-128,即密钥长度为16字节。但是由于只应用了一个 3 字节的大密钥 (123
),PHP 使用 0x00
值填充到必要的 16 字节大小。注意key太长会被截断
在 VB 代码中使用了一个 32 字节的密钥。由于在 .NET 中密钥大小决定了 AES 变体,因此应用 AES-256。而且,传递的密钥不是直接使用的,而是实际的密钥是从传递的值和摘要MD5中派生出来的。
所以VB代码returns是PHP代码的结果,PHP代码的逻辑必须在VB中实现代码:
... 'Dim hash(31) As Byte 'Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass)) 'Array.Copy(temp, 0, hash, 0, 16) 'Array.Copy(temp, 0, hash, 15, 16) 'AES.Key = hash Dim keyPadded(15) As Byte Dim key = System.Text.ASCIIEncoding.ASCII.GetBytes(pass) Array.Copy(key, 0, keyPadded, 0, Math.Min(16, key.Length)) AES.Key = keyPadded ...
几点说明: