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

Recommended Answers

All 2 Replies

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)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.