如何解决加密:使用vb.net,我得到的结果不同于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,hash,16)
Array.Copy (temp,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,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-对文本” abc”和盐“ 123”进行加密欧洲央行”?
解决方法
由于PHP代码中的规范AES-128-ECB
,AES-128用于ECB模式,即密钥长度为16个字节。但是,由于仅应用了3个字节的大键(123
),因此PHP会使用0x00
值将其填充到16个字节的必要大小。请注意,如果密钥太长,它将被截断。
在VB代码中,使用了32个字节的密钥。由于在.NET中,密钥大小决定了AES变体,因此将应用AES-256。此外,传递的密钥不直接使用,而是实际的密钥是从摘要MD5的传递值中得出的。
为使VB代码返回PHP代码的结果,必须在VB代码中实现PHP代码的逻辑:
... 'Dim hash(31) As Byte 'Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(pass)) 'Array.Copy(temp,hash,16) 'Array.Copy(temp,15,16) 'AES.Key = hash Dim keyPadded(15) As Byte Dim key = System.Text.ASCIIEncoding.ASCII.GetBytes(pass) Array.Copy(key,keyPadded,Math.Min(16,key.Length)) AES.Key = keyPadded ...
一些说明:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。