对于大多数字符串,似乎可以很好地进行加密和解密,直到字符串具有两个或多个空格为止.即嗡嗡声"-可以很好地加密/解密(缓冲区/长度= 16)'Buzz Aldrin'-加密/解密良好(缓冲区/长度= 16)"Buzz Aldrin宇航员"-加密精细/解密错误(缓冲区/长度= 31) System.Security.Cryptography.CryptographicException:要解密的数据长度无效." 公共共享函数AES_Decrypt(ByVal密文作为字符串,ByVal密钥作为字符串)作为字符串昏暗的AES作为新系统.安全性.密码学.Rijndael将SHA256变暗为新的System.Security.Cryptography.SHA256Cng昏暗的纯文字为String ="Dim iv As String ="尝试Dim ivct = ciphertext.Split(CChar("="))iv = ivct(0)&"=="密文= ivct(2)&"=="AES.Key = SHA256.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))AES.IV = Convert.FromBase64String(iv)AES.Mode =安全性.密码学.CipherMode.CBC暗淡的DESDecrypter作为System.Security.Cryptography.ICryptoTransform = AES.CreateDecryptor昏暗缓冲区为Byte()= Convert.FromBase64String(密文)例外---->plaintext = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer,0,Buffer.Length))返回纯文本捕获为系统异常返回ex.Message结束尝试结束功能 有什么主意我在做错什么或如何纠正? 示例更新 尝试Dim s1,s2,s3作为字符串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宇航员:"& s3&:"&Crypto.AES_Decrypt(s3,"Password"))捕获为System.ExceptionDebug.Print(例如Message.ToString())结束尝试 调试打印输出 嗡嗡声:aTBh1U0OFqW7 + 266LiC7Vg == GC6bUY5pK10L2KgQzpAtgg ==:嗡嗡声Buzz Aldrin:80fmD0z57R8jmmCkKhCsXg == dixi7bqheBzKhXcT1UEpWQ ==:Buzz Aldrin 引发异常:mscorlib.dll中的'System.Security.Cryptography.CryptographicException'要解密的数据长度无效.解决方案 Buzz Aldrin宇航员:/1RInYgi/XPCpKYKxCCQLg == NgtahaolZmtyRKqG5d3XdWbnTP3o782hoyg7jp6VVAA = 这就是我运行您的示例的原因.最后一个 String 仅以一个 = 结尾,因此此行不正确并生成此错误 密文= ivct(2)&"==" 替换以下几行 Dim ivct = ciphertext.Split(CChar("="))iv = ivct(0)&"=="密文= ivct(2)&"==" 通过此代码 Dim ivct = ciphertext.Split({"=="},StringSplitOptions.None)iv = ivct(0)&"=="密文= If(ivct.Length = 3,ivct(1)&"==",ivct(1)) 这应该可以正常运行.希望这会有所帮助.I have implemented the methods in this answer to encrypt and decrypt a stringAES Encrypt String in VB.NETThis appears to be encrypt and decrypt fine for most strings until the string has two or more spaces.I.e.'Buzz' - encrypt / decrypt fine (Buffer /Length = 16)'Buzz Aldrin' - encrypt / decrypt fine (Buffer /Length = 16)'Buzz Aldrin Astronaut' - encrypt fine / decrypt error (Buffer /Length = 31) System.Security.Cryptography.CryptographicException: 'Length of the data to decrypt is invalid.' 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 plaintextCatch ex As system.Exception Return ex.Message End Try End FunctionAny ideas what I am doing wrong or how I might correct it?Example UpdateTry 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 Output Buzz : aTBh1U0OFqW7+266LiC7Vg==GC6bUY5pK10L2KgQzpAtgg== : Buzz Buzz Aldrin : 80fmD0z57R8jmmCkKhCsXg==dixi7bqheBzKhXcT1UEpWQ== : Buzz Aldrin Exception thrown: 'System.Security.Cryptography.CryptographicException' in mscorlib.dll Length of the data to decrypt is invalid. 解决方案 Buzz Aldrin Astronaut : /1RInYgi/XPCpKYKxCCQLg==NgtahaolZmtyRKqG5d3XdWbnTP3o782hoyg7jp6VVAA=This is what I get running your example.Your last String end with only one = so this line isn't correct and generates this errorciphertext = ivct(2) & "=="Replace the following linesDim ivct = ciphertext.Split(CChar("="))iv = ivct(0) & "=="ciphertext = ivct(2) & "=="by this codeDim ivct = ciphertext.Split({"=="}, StringSplitOptions.None)iv = ivct(0) & "=="ciphertext = If(ivct.Length = 3, ivct(1) & "==", ivct(1))and this should run just fine.Hope this helps. 这篇关于System.Security.Cryptography.CryptographicException:'要解密的数据长度无效.'字符串双倍空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-12 02:01