简体   繁体   中英

How do I decrypt a value before adding it to my Listview?

Good morning,

I have need to decrypt a value (ccNumber) being pulled from my MySQL database before it is assigned to my Listview. How do I do this?

I managed to figure out how to encrypt it before it's inserted to the database, but I'm struggling with the decryption. As shown below in my code, I'm attempting to call the decryption sub-routine (DecryptCard) just before I iterate through the datatable (dbTable) and assign its values to my Listview (ListViewCard). However, only the first record succeeds then I get an error message (see images below).

数据库值添加到LISTVIEW之前引发的异常

列表查看后 Note that the first record is decrypted successfully - but not the second.

My codes are as follows:

CODE TO QUERY DATABASE AND LOAD DATA INTO LISTVIEW:

    Private Sub loadCard()

    Try
        'FOR MySQL DATABASE USE
        Dim dbQuery As String = ""
        Dim dbCmd As New MySqlCommand
        Dim dbAdapter As New MySqlDataAdapter
        Dim dbTable As New DataTable
        Dim i As Integer

        If dbConn.State = ConnectionState.Closed Then
            dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
            dbConn.Open()
        End If

        dbQuery = "SELECT *" & _
                   "FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
                   "WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
        With dbCmd
            .CommandText = dbQuery
            .Connection = dbConn
        End With
        With dbAdapter
            .SelectCommand = dbCmd
            .Fill(dbTable)
        End With
        ListViewCard.Items.Clear()
        For i = 0 To dbTable.Rows.Count - 1
            Call DecryptCard()
            With ListViewCard
                .Items.Add(dbTable.Rows(i)("ccID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dbTable.Rows(i)("ccNumber"))
                    .Add(dbTable.Rows(i)("ccExpireMonth"))
                    .Add(dbTable.Rows(i)("ccExpireYear"))
                    .Add(dbTable.Rows(i)("ccCode"))
                    .Add(dbTable.Rows(i)("ccType"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
                    .Add(dbTable.Rows(i)("nameCOMPANY"))
                    .Add(dbTable.Rows(i)("nameSALUTATION"))
                    .Add(dbTable.Rows(i)("nameLAST"))
                    .Add(dbTable.Rows(i)("nameFIRST"))
                End With
            End With
        Next
    Catch ex As MySqlException
        MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                    vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
    End Try
    dbConn.Close()

End Sub

CODE THAT SHOULD DECRYPT THE VALUE (ccNumber):

    Public Sub DecryptCard()
    Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
    Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
    Try
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor
        Dim Buffer As Byte() = Convert.FromBase64String(dbTable.Rows(0)("ccNumber").ToString())
        dbTable.Rows(0)("ccNumber") = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch ex As Exception
        MessageBox.Show("The following error(s) have occurred: " & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

HERE'S THE ENCRYPTION CODE

    Public Sub encryptCard()
    Try
        Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
        Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encryptedCard As String
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateEncryptor
        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBoxCard.Text)
        encryptedCard = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch ex As Exception
        MessageBox.Show("The following error(s) have occurred: " & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Good morning,

I have need to decrypt a value (ccNumber) being pulled from my MySQL database before it is assigned to my Listview. How do I do this?

I managed to figure out how to encrypt it before it's inserted to the database, but I'm struggling with the decryption. As shown below in my code, I'm attempting to call the decryption sub-routine (DecryptCard) just before I iterate through the datatable (dbTable) and assign its values to my Listview (ListViewCard). However, only the first record succeeds then I get an error message (see images below).

数据库值添加到LISTVIEW之前引发的异常

列表查看后 Note that the first record is decrypted successfully - but not the second.

My codes are as follows:

CODE TO QUERY DATABASE AND LOAD DATA INTO LISTVIEW:

    Private Sub loadCard()

    Try
        'FOR MySQL DATABASE USE
        Dim dbQuery As String = ""
        Dim dbCmd As New MySqlCommand
        Dim dbAdapter As New MySqlDataAdapter
        Dim dbTable As New DataTable
        Dim i As Integer

        If dbConn.State = ConnectionState.Closed Then
            dbConn.ConnectionString = String.Format("Server={0};Port={1};Uid={2};Password={3};Database=accounting", FormLogin.ComboBoxServerIP.SelectedItem, My.Settings.DB_Port, My.Settings.DB_UserID, My.Settings.DB_Password)
            dbConn.Open()
        End If

        dbQuery = "SELECT *" & _
                   "FROM cc_master INNER JOIN customer ON customer.accountNumber = cc_master.customer_accountNumber " & _
                   "WHERE customer.accountNumber = '" & TextBoxAccount.Text & "'"
        With dbCmd
            .CommandText = dbQuery
            .Connection = dbConn
        End With
        With dbAdapter
            .SelectCommand = dbCmd
            .Fill(dbTable)
        End With
        ListViewCard.Items.Clear()
        For i = 0 To dbTable.Rows.Count - 1
            Call DecryptCard()
            With ListViewCard
                .Items.Add(dbTable.Rows(i)("ccID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dbTable.Rows(i)("ccNumber"))
                    .Add(dbTable.Rows(i)("ccExpireMonth"))
                    .Add(dbTable.Rows(i)("ccExpireYear"))
                    .Add(dbTable.Rows(i)("ccCode"))
                    .Add(dbTable.Rows(i)("ccType"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
                    .Add(dbTable.Rows(i)("nameCOMPANY"))
                    .Add(dbTable.Rows(i)("nameSALUTATION"))
                    .Add(dbTable.Rows(i)("nameLAST"))
                    .Add(dbTable.Rows(i)("nameFIRST"))
                End With
            End With
        Next
    Catch ex As MySqlException
        MessageBox.Show("A DATABASE ERROR HAS OCCURED" & vbCrLf & vbCrLf & ex.Message & vbCrLf & _
                    vbCrLf + "Please report this to the IT/Systems Helpdesk at Ext 131.")
    End Try
    dbConn.Close()

End Sub

CODE THAT SHOULD DECRYPT THE VALUE (ccNumber):

    Public Sub DecryptCard()
    Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
    Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
    Try
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESDecrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateDecryptor
        Dim Buffer As Byte() = Convert.FromBase64String(dbTable.Rows(0)("ccNumber").ToString())
        dbTable.Rows(0)("ccNumber") = System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch ex As Exception
        MessageBox.Show("The following error(s) have occurred: " & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

HERE'S THE ENCRYPTION CODE

    Public Sub encryptCard()
    Try
        Dim DES As New System.Security.Cryptography.TripleDESCryptoServiceProvider
        Dim Hash As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim encryptedCard As String
        DES.Key = Hash.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(My.Settings.Key))
        DES.Mode = System.Security.Cryptography.CipherMode.ECB
        Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = DES.CreateEncryptor
        Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(TextBoxCard.Text)
        encryptedCard = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch ex As Exception
        MessageBox.Show("The following error(s) have occurred: " & ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM