VB.Net AES加密的值与http://extranet.cryptomathic.com/aescalc/index

时间:2019-06-26 13:31:02

标签: vb.net aes

我正在VB.Net中编写代码以AES加密长度为16的HEX字节字符串,并且期望得到相同的长度结果。

我从Microsoft网站(https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.8)中找到了C#代码,并使用https://codeconverter.icsharpcode.net/将此代码转换为VB.Net,当然,对其进行更改使其在VB.Net中可以正常工作

下面是用于调用以及加密和解密的修改后的代码。我遗漏了一些东西,导致16(发送的内容)+48字节长度的加密值。我需要的是使用AES,ECB模式加密的16字节十六进制值

下面是呼叫代码

Private Sub ProcessData()
    Dim original As String = sKi
    Dim encryptedKi() As Byte
    Dim KeyBytes() As Byte = {"&h" & Mid(sK4, 1, 2), "&h" & Mid(sK4, 3, 2), "&h" & Mid(sK4, 5, 2), "&h" & Mid(sK4, 7, 2), "&h" & Mid(sK4, 9, 2), "&h" & Mid(sK4, 11, 2), "&h" & Mid(sK4, 13, 2), "&h" & Mid(sK4, 15, 2), "&h" & Mid(sK4, 1, 2), "&h" & Mid(sK4, 3, 2), "&h" & Mid(sK4, 5, 2), "&h" & Mid(sK4, 7, 2), "&h" & Mid(sK4, 9, 2), "&h" & Mid(sK4, 11, 2), "&h" & Mid(sK4, 13, 2), "&h" & Mid(sK4, 15, 2)}
    j = 0
    Using myAes As Aes = Aes.Create()
        myAes.Key = KeyBytes
        encryptedKi = clsCryptoDES.EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV)
        For x = 0 To encryptedKi.Length - 1
            sKi = sKi & Mid("0" & Hex(encryptedKi(x)), Len(Hex(encryptedKi(x))), 2)
        Next x
    End Using
End Sub

“下面是用于加密和解密的代码

Public Shared Function EncryptStringToBytes_Aes(ByVal plainText As String, ByVal Key As Byte(), ByVal IV As Byte()) As Byte()
    If plainText Is Nothing OrElse plainText.Length <= 0 Then Throw New ArgumentNullException("plainText")
    If Key Is Nothing OrElse Key.Length <= 0 Then Throw New ArgumentNullException("Key")
    If IV Is Nothing OrElse IV.Length <= 0 Then Throw New ArgumentNullException("IV")
    Dim encrypted As Byte()

    Using aesAlg As Aes = Aes.Create()
        aesAlg.Key = Key
        aesAlg.IV = IV
        aesAlg.KeySize = 128
        aesAlg.Mode = CipherMode.ECB
        Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

        Using msEncrypt As MemoryStream = New MemoryStream()

            Using csEncrypt As CryptoStream = New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

                Using swEncrypt As StreamWriter = New StreamWriter(csEncrypt)
                    swEncrypt.Write(plainText)
                End Using

                encrypted = msEncrypt.ToArray()
            End Using
        End Using
    End Using

    Return encrypted
End Function

Public Shared Function DecryptStringFromBytes_Aes(ByVal cipherText As Byte(), ByVal Key As Byte(), ByVal IV As Byte()) As String
    If cipherText Is Nothing OrElse cipherText.Length <= 0 Then Throw New ArgumentNullException("cipherText")
    If Key Is Nothing OrElse Key.Length <= 0 Then Throw New ArgumentNullException("Key")
    If IV Is Nothing OrElse IV.Length <= 0 Then Throw New ArgumentNullException("IV")
    Dim plaintext As String = Nothing

    Using aesAlg As Aes = Aes.Create()
        aesAlg.Key = Key
        aesAlg.IV = IV
        Dim decryptor As ICryptoTransform = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)

        Using msDecrypt As MemoryStream = New MemoryStream(cipherText)

            Using csDecrypt As CryptoStream = New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

                Using srDecrypt As StreamReader = New StreamReader(csDecrypt)
                    plaintext = srDecrypt.ReadToEnd()
                End Using
            End Using
        End Using
    End Using

    Return plaintext
End Function

预期产量

878CB02AA535DB9BEA77FA9107F4A29B

http://extranet.cryptomathic.com/aescalc/index?key=0123456789ABCDEF0123456789ABCDEF&iv=00000000000000000000000000000000&input=6E67229083422AD33A096657F0D01761&mode=ecb&action=Encrypt&output=

实际结果(16字节纯字符串+ 48字节加密结果) 6E67229083422AD33A096657F0D01761E03A1ED32270B4E934017D8429871CC7F78392C21477DD25BF1C633E146071063D844DC896DD1B285351FC8BE5C7CB42

0 个答案:

没有答案