如何遍历 DataTable 并解密字段? [英] How do I Iterate Through DataTable and Decrypt a field?

查看:31
本文介绍了如何遍历 DataTable 并解密字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在将特定字段 (ccNumber) 的项目分配给 Listview 控件 (ListViewRecords) 之前,我需要帮助它遍历 DataTable (dbTable) 和解密特定字段.

I need help with iterating through a DataTable (dbTable) and decrypting a particular field (ccNumber) before assigning its items to a Listview control (ListViewRecords).

我已经成功地在我的项目中的文本框上的其他地方使用了下面的解密代码,但无法弄清楚如何使用 DataTable 进行解密.非常感谢您的帮助,谢谢.

I've already used the decryption code below elsewhere in my project on a Textbox with success, but can't figure out how to do it with the DataTable. Your assistance would be greatly appreciated, thanks.

这是解密代码:

    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(TextBoxCard.Text)
        TextBoxCard.Text = 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

这是查询数据库和填充列表视图的代码:

Private Sub loadRecords()
    'FOR MySQL DATABASE USE
    Dim dbConn As New MySqlConnection
    Dim dbTable As New DataTable
    Dim dbQuery As String = ""
    Dim dbCmd As New MySqlCommand
    Dim dbAdapter As New MySqlDataAdapter
    dbTable.Clear()

    Try

        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 " & _
                   "ORDER BY nameCOMPANY ASC"
        With dbCmd
            .CommandText = dbQuery
            .Connection = dbConn
        End With
        With dbAdapter
            .SelectCommand = dbCmd
            .Fill(dbTable)
        End With
        ListViewRecords.Items.Clear()
        For i = 0 To dbTable.Rows.Count - 1
            With ListViewRecords
                .Items.Add(dbTable.Rows(i)("ccID"))
                With .Items(.Items.Count - 1).SubItems
                    .Add(dbTable.Rows(i)("nameCOMPANY"))
                    .Add(dbTable.Rows(i)("ccNumber"))
                    .Add(dbTable.Rows(i)("ccExpireMonth"))
                    .Add(dbTable.Rows(i)("ccExpireYear"))
                    .Add(dbTable.Rows(i)("ccType"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseStart"))
                    .Add(dbTable.Rows(i)("ccAuthorizedUseEnd"))
                    .Add(dbTable.Rows(i)("ccLocation"))
                    .Add(dbTable.Rows(i)("cardholderSalutation"))
                    .Add(dbTable.Rows(i)("cardholderLastname"))
                    .Add(dbTable.Rows(i)("cardholderFirstname"))
                    .Add(dbTable.Rows(i)("ccZipcode"))
                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

推荐答案

把解密的东西放在一个函数里:

Put the decryption thing in a function:

Function Decrypt(ByVal ToDecrypt) as String
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(ToDecrypt)
            return  System.Text.ASCIIEncoding.ASCII.GetString(DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
        Catch ex As Exception
            return "Whatever failed message you want"
        End Try
End Function

然后像这样循环遍历您的表并更改值:

Then loop through your table like this and change the value:

for i as Integer = 0 to dbTable.Rows.Count - 1
dbTable.Rows(i)("ccNumber")) = Decrypt(dbTable.Rows(i)("ccNumber"))

如果我没有完全离开,这应该可以工作.

This should work if I'm not totally off.

这篇关于如何遍历 DataTable 并解密字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆