首页 > 解决方案 > 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. '

有人可以帮我吗?

标签: vb.netms-accessencryptiontripledes

解决方案


推荐阅读