0

Ok if I do a simple datareader and print each row in my test column (encrypted) I get the value as expected.

What I am looking to do is simply take the returned value, pass it to the Decrypt function of the TripleDES class and print the decrypted value. I encrypted some random text into my "TEST" column.
This is the error I receive:

Unable to cast object of type 'System.String' to type 'System.Byte[]'.
I could use CByte() but that converts it to a byte and not an array of bytes.

'Decrypt Info
    Sub Decrypt()
        'Connection variables
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("messengerConnectionString").ConnectionString
        Dim sqlConnection As New MySqlConnection(strConnection)
        Dim SQLQuery As String = "SELECT * from testing"
        Dim command As New MySqlCommand(SQLQuery, sqlConnection)
        Dim dr As MySqlDataReader

        'Encryption variable
        Dim TDES As New TripleDES()

        'Open DB Connection & read
        sqlConnection.Open()
        dr = command.ExecuteReader(CommandBehavior.CloseConnection)


        Dim decrypt As String = Nothing

        If dr.HasRows Then
            While dr.Read()
                decrypt = TDES.Decrypt(dr.Item("TEST"))
                MsgBox(decrypt)
            End While
        End If

    End Sub

Edited by bmiles2001: n/a

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by bmiles2001
0

Read - http://msdn.microsoft.com/en-us/library/system.security.cryptography.tripledes.aspx

Here is a sample,

Dim ts As New TripleDESCryptoServiceProvider()
        Dim trans As ICryptoTransform

        Dim plainText As String = "Hello"
        Dim bArry() As Byte = System.Text.Encoding.UTF8.GetBytes(plainText)
        Dim eArry() As Byte

        trans = ts.CreateEncryptor
        eArry = trans.TransformFinalBlock(bArry, 0, bArry.Length)

        Dim EncText As String = System.Convert.ToBase64String(eArry)
        Console.WriteLine(EncText)

        'Decrypt
        eArry = Convert.FromBase64String(EncText)
        Dim dArry() As Byte

        trans = ts.CreateDecryptor()
        dArry = trans.TransformFinalBlock(eArry, 0, eArry.Length)

        Dim origText As String
        origText = System.Text.Encoding.UTF8.GetString(dArry)
        Console.WriteLine(origText)

Edited by __avd: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.