我已经实现了在这个答案的方法加密和解密字符串System.Security.Cryptography.CryptographicException:'要解密的数据的长度无效。'字符串双空格
这似乎是加密和解密对大多数串直到字符串中有两个或两个以上空间。
I.e.
- '巴兹' - 加密/解密细(缓冲液/长度= 16)
- '奥尔德林' - 加密/解密细(缓冲液/长度= 16)
- '奥尔德林宇航员' - 加密细/解密错误(缓冲器/长度= 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 ==:巴斯Aldrin
抛出的异常:mscorlib.dll中的'System.Security.Cryptography.CryptographicException'
要解密的数据的长度无效。
我已经添加了一个示例 – Kanky
请注意,密码应该是散列,*不*加密 – Plutonix
对不起,我的坏^ _ ^我对Base64没有太多了解 –