vb.net - System.Security.Cryptography.CryptographicException:'错误数据。' 三重 DES 加密 vb.net ms 访问
问题描述
加密和解密
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
Public Class EncDecCls
Private tripleDES As New TripleDESCryptoServiceProvider
'Public Sub New(_KEY As String)
' KeyStr = _KEY
'End Sub
'Private KeyValue As String
'Public Property KeyStr() As String
' Get
' Return KeyValue
' End Get
' Set(value As String)
' KeyValue = value
' End Set
'End Property
Function Encryption(ByVal datafile As String) As String
Dim input As Byte() = System.Text.Encoding.Unicode.GetBytes(datafile)
Dim ms As New System.IO.MemoryStream
Dim encstream As New CryptoStream(ms, tripleDES.CreateEncryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
encstream.Write(input, 0, input.Length)
encstream.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray)
End Function
Function Decryption(ByVal encryptedfile As String) As String
Dim output() As Byte = System.Text.Encoding.Unicode.GetBytes(encryptedfile)
Dim ms As New System.IO.MemoryStream
Dim decstream As New CryptoStream(ms, tripleDES.CreateDecryptor(),
System.Security.Cryptography.CryptoStreamMode.Write)
decstream.Write(output, 0, output.Length)
decstream.FlushFinalBlock()
Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
End Function
End Class
从数据库中检索数据并将其解密,然后显示在 datagridview(DGVSV) 上的代码
Dim cmd1 As New OleDbCommand("SELECT * FROM pwdmgr WHERE username = @uname", conn)
cmd1.Parameters.Add("@uname", OleDbType.VarChar).Value = x
conn.Open()
dr = cmd1.ExecuteReader
dr.Read()
DGVSV.Rows.Add(dr.Item("id").ToString,
EncryptDecryptFiles.Decryption(dr.Item("email").ToString),
EncryptDecryptFiles.Decryption(dr.Item("pwd").ToString))
conn.Close()
需要检索几行数据并将其显示到 datagridview(DGVSV) 中。每次当我在上面运行这个函数时,它都会显示这个错误System.Security.Cryptography.CryptographicException: 'Bad Data. '
有人可以帮我吗?
解决方案
推荐阅读
- sql - 在 bigquery 中从 unixtime 获取日期
- python - 数组的动态循环
- c - 使用静态常量变量时,C 中的“初始化程序元素不是常量”错误 - 有时 - 编译器设置?
- elasticsearch - 未找到 [@timestamp] 的映射以便对 logstash 进行排序
- amazon-web-services - 创建 sparkUI 历史服务器的 CF 模板失败
- html - CSS中的伪焦点不适用于所有对象
- reactjs - 错误是传播不可迭代实例的无效尝试。为了可迭代,非数组对象必须有一个 [Symbol.iterator]() 方法
- android - 如何更改TextInputLayout内edittext的边框颜色
- javascript - 如果时间少于所选时间,如何在议程周中更改颜色
- python - 在列表中搜索 - 是否有更有效的方法来编写此代码?