0

良い一日、

リストビュー コントロール (ListViewRecords) に項目を割り当てる前に、DataTable (dbTable) を反復処理し、特定のフィールド (ccNumber) を復号​​化する方法について助けが必要です。

私はすでに、Textbox のプロジェクトの他の場所で以下の復号化コードを使用して成功していますが、DataTable でそれを行う方法がわかりません。どうぞよろしくお願いいたします。

解読コードは次のとおりです。

    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

DB を照会してリストビューを埋めるコードは次のとおりです。

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
4

1 に答える 1

1

復号化を関数に入れます。

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

次に、次のようにテーブルをループして、値を変更します。

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

私が完全にオフでない場合、これはうまくいくはずです。

于 2012-11-26T18:37:17.300 に答える