Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加密:使用vb.net,我得到的结果与php不同_Php_Vb.net_Encryption_Aes - Fatal编程技术网

加密:使用vb.net,我得到的结果与php不同

加密:使用vb.net,我得到的结果与php不同,php,vb.net,encryption,aes,Php,Vb.net,Encryption,Aes,我有这个php代码 $plain_text = "abc"; $salt = "123"; echo $encrypted_text = openssl_encrypt($plain_text, "AES-128-ECB", $salt); // result: kR/1uaFarptS5+n951MVsQ== 我在vb.net上尝试了几种方法(类和函数),但使用这种语言加密的结果每次都与上面使用php时的结果不同。 例如,这个: P

我有这个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”对文本“abc”和salt“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, 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 ... ... '作为字节的Dim哈希(31) 'Dim temp As Byte()=Hash_AES.ComputeHash(System.Text.ascienceoding.ASCII.GetBytes(pass)) '数组.Copy(临时,0,哈希,0,16) '数组.Copy(临时、0、哈希、15、16) 'AES.Key=hash Dim键盘填充(15)作为字节 Dim key=System.Text.AscienceODing.ASCII.GetBytes(通过) Array.Copy(key,0,带键区的,0,Math.Min(16,key.Length)) AES.Key=键盘填充 ... 几句话:

  • 在PHP代码中,键名为
    $salt
    。这是误导,因为a有不同的含义
  • 该模式通常不安全
  • -128使用16字节的密钥
    123
    不是强键(但可能这只是一个伪值)
  • 如果
    123
    不表示密钥,而是表示从中派生密钥的密码,则通常不应使用MD5,而应使用特别设计的算法,如或,另请参阅

由于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, 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 ... ... '作为字节的Dim哈希(31) 'Dim temp As Byte()=Hash_AES.ComputeHash(System.Text.ascienceoding.ASCII.GetBytes(pass)) '数组.Copy(临时,0,哈希,0,16) '数组.Copy(临时、0、哈希、15、16) 'AES.Key=hash Dim键盘填充(15)作为字节 Dim key=System.Text.AscienceODing.ASCII.GetBytes(通过) Array.Copy(key,0,带键区的,0,Math.Min(16,key.Length)) AES.Key=键盘填充 ... 几句话:

  • 在PHP代码中,键名为
    $salt
    。这是误导,因为a有不同的含义
  • 该模式通常不安全
  • -128使用16字节的密钥
    123
    不是强键(但可能这只是一个伪值)
  • 如果
    123
    不表示密钥,而是表示从中派生密钥的密码,则通常不应使用MD5,而应使用特别设计的算法,如或,另请参阅
有帮助吗?似乎你的代码设计为每次都提供不同的密文。有帮助吗?似乎您的代码设计为每次都提供不同的密文。