Vb.net System.Security.Cryptography.CryptographyException:&x27;要解密的数据长度无效;字符串双空格
我已经实现了这个答案中对字符串进行加密和解密的方法 在字符串有两个或更多空格之前,对大多数字符串进行加密和解密似乎都很好 即Vb.net System.Security.Cryptography.CryptographyException:&x27;要解密的数据长度无效;字符串双空格,vb.net,encryption,Vb.net,Encryption,我已经实现了这个答案中对字符串进行加密和解密的方法 在字符串有两个或更多空格之前,对大多数字符串进行加密和解密似乎都很好 即 “嗡嗡声”-加密/解密良好(缓冲区/长度=16) “Buzz Aldrin”-加密/解密精细(缓冲区/长度=16) “Buzz Aldrin宇航员”-加密精细/解密错误(缓冲区/长度=31) System.Security.Cryptography.CryptographyException:“要解密的数据长度无效。” 你知道我做错了什么,或者我该如何改正吗 更新示
- “嗡嗡声”-加密/解密良好(缓冲区/长度=16)
- “Buzz Aldrin”-加密/解密精细(缓冲区/长度=16)
- “Buzz Aldrin宇航员”-加密精细/解密错误(缓冲区/长度=31)
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==:嗡嗡声
Buzz-Aldrin:80fmd0z57r8jmmckhhcsxg==dixi7bqheBzKhXcT1UEpWQ==:Buzz-Aldrin
引发异常:mscorlib.dll中的“System.Security.Cryptography.CryptographyException” 要解密的数据长度无效 Buzz Aldrin宇航员:/1RInYgi/XPCpKYKxCCQLg==ngtahaolzmtyrkqg5d3xdwbtp3o782hoyg7jp6vvaa= 这就是我运行您的示例得到的结果 最后一个
字符串
仅以一个=
结尾,因此此行不正确并生成此错误
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))
这应该运行得很好
希望这有帮助。用于分割IV和密文的代码实际上通过总是附加
=
来破坏密文。这会导致Base64编码被破坏,出于某种原因,VB.Net对此没有问题
加
之后
这一行修复了Base64编码。您也可以更改我的实现,这样加密算法会将IV与密文连接起来,中间有一个#字符,解密会将其拆分并删除#。它应该对每个人都更方便。很抱歉给您带来的最初不便。我添加了一个示例请注意,密码应该是散列的,而不是加密的对不起,我的错误^我对Base64不太了解
Dim ivct = ciphertext.Split({"=="}, StringSplitOptions.None)
iv = ivct(0) & "=="
ciphertext = If(ivct.Length = 3, ivct(1) & "==", ivct(1))
ciphertext = ciphertext.Substring(0, ciphertext.Length - ciphertext.Length Mod 4)
ciphertext = ivct(2) & "=="