System.Security.Cryptography.CryptographicException:'要解密的数据长度无效。'字符串双空格

时间:2017-08-22 10:25:20

标签: vb.net encryption

我已经在加密解密字符串

的答案中实施了这些方法

AES Encrypt String in VB.NET

对于大多数字符串,这似乎是加密和解密的,直到字符串有两个或更多空格。

  • '巴斯' - 加密/解密(缓冲区/长度= 16)
  • ' Buzz Aldrin' - 加密/解密(缓冲区/长度= 16)
  • ' Buzz Aldrin宇航员' - 加密精细/解密错误(缓冲区/长度= 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输出
  Buzz:aTBh1U0OFqW7 + 266LiC7Vg == GC6bUY5pK10L2KgQzpAtgg ==:Buzz
  Buzz Aldrin:80fmD0z57R8jmmCkKhCsXg == dixi7bqheBzKhXcT1UEpWQ ==:Buzz Aldrin
  抛出异常:' System.Security.Cryptography.CryptographicException'在mscorlib.dll中   要解密的数据长度无效。

3 个答案:

答案 0 :(得分:1)

  

Buzz Aldrin宇航员:/ 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))

这应该运行得很好。

希望这有帮助。

答案 1 :(得分:0)

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

添加

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

ciphertext = ivct(2) & "=="

此行修复了Base64编码。

答案 2 :(得分:0)

您还可以更改我的实现,以便加密算法将IV与密文连接在一起,并且解密将从那里拆分并删除#。对每个人来说都应该更方便。抱歉,最初的不便之处。