【问题标题】:System.Security.Cryptography.CryptographicException: 'Length of the data to decrypt is invalid.' String double spacesSystem.Security.Cryptography.CryptographicException:“要解密的数据长度无效。”字符串双空格
【发布时间】:2017-08-22 10:25:19
【问题描述】:

我已经在这个答案中实现了 encryptdecrypt 字符串

的方法

AES Encrypt String in VB.NET

对于大多数字符串来说,这似乎可以很好地加密和解密,直到字符串有两个或更多空格。

  • 'Buzz' - 加密/解密很好(缓冲区/长度 = 16)
  • 'Buzz Aldrin' - 加密/解密很好(缓冲区/长度 = 16)
  • 'Buzz Aldrin Astronaut' - 加密正常/解密错误(缓冲区/长度 = 31)

System.Security.Cryptography.CryptographicException: '要解密的数据长度无效。'

 Public Shared Function AES_Decrypt(ByVal ciphertext As String, ByVal key As String) As String
 Dim AES As New System.Security.Cryptography.RijndaelManaged
            Dim SHA256 As New System.Security.Cryptography.SHA256Cng
            Dim plaintext As String = ""
            Dim iv As String = ""
            Try
                Dim ivct = ciphertext.Split(CChar("="))
                iv = ivct(0) & "=="
                ciphertext = ivct(2) & "=="

                AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))
                AES.IV = Convert.FromBase64String(iv)
                AES.Mode = Security.Cryptography.CipherMode.CBC
                Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
                Dim Buffer As Byte() = Convert.FromBase64String(ciphertext)

Exception  ---->   plaintext = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
                Return plaintext
Catch ex As system.Exception
                Return ex.Message
            End Try
        End Function

任何想法我做错了什么或我可以如何纠正它?

示例更新

Try
        Dim s1, s2, s3 As String
        s1 = Crypto.AES_Encrypt("Buzz", "Password")
        s2 = Crypto.AES_Encrypt("Buzz Aldrin", "Password")
        s3 = Crypto.AES_Encrypt("Buzz Aldrin Astronaut", "Password")
        Debug.Print("Buzz : " & s1 & " : " & Crypto.AES_Decrypt(s1, "Password"))
        Debug.Print("Buzz Aldrin : " & s2 & " : " & Crypto.AES_Decrypt(s2, "Password"))
        Debug.Print("Buzz Aldrin Astronaut : " & s3 & " : " & Crypto.AES_Decrypt(s3, "Password"))
    Catch ex As System.Exception
        Debug.Print(ex.Message.ToString())
    End Try

Debug.Print 输出
嗡嗡声:aTBh1U0OFqW7+266LiC7Vg==GC6bUY5pK10L2KgQzpAtgg==:嗡嗡声
巴兹奥尔德林:80fmD0z57R8jmmCkKhCsXg==dixi7bqheBzKhXcT1UEpWQ==:巴兹奥尔德林
抛出异常:mscorlib.dll 中的“System.Security.Cryptography.CryptographicException”
要解密的数据长度无效。

【问题讨论】:

  • 我添加了一个例子
  • 注意密码应该是散列的,加密
  • 对不起我的坏^_^我对Base64了解不多

标签: vb.net encryption


【解决方案1】:

巴兹奥尔德林宇航员:/1RInYgi/XPCpKYKxCCQLg==NgtahaolZmtyRKqG5d3XdWbnTP3o782hoyg7jp6VVAA=

这就是我运行您的示例的内容。

您的最后一个 String 仅以一个 = 结尾,因此该行不正确并生成此错误

ciphertext = ivct(2) & "=="

替换下面几行

Dim ivct = ciphertext.Split(CChar("="))
iv = ivct(0) & "=="
ciphertext = ivct(2) & "=="

通过此代码

Dim ivct = ciphertext.Split({"=="}, StringSplitOptions.None)
iv = ivct(0) & "=="
ciphertext = If(ivct.Length = 3, ivct(1) & "==", ivct(1))

这应该运行得很好。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    用于拆分 IV 和密文的代码实际上通过始终附加 == 来破坏密文。这会导致损坏的 Base64 编码,VB.Net 出于某种原因没有问题。

    添加

    ciphertext = ciphertext.Substring(0, ciphertext.Length - ciphertext.Length Mod 4)
    

    之后

    ciphertext = ivct(2) & "=="
    

    这一行修复了 Base64 编码。

    【讨论】:

      【解决方案3】:

      您还可以更改我的实现,以便加密算法将 IV 与密文连接起来,中间有一个 # 字符,解密将从那里拆分并删除 #。它应该对每个人都更方便。对最初的不便深表歉意。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-19
        • 1970-01-01
        • 2011-12-05
        • 2010-12-22
        相关资源
        最近更新 更多