Hi used the below coding for enrypting and decrypting a text. But with this i dont know how to save to a database, when i tried i am getting many errors. How to do this?

Private Sub cmdEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Encrypt
        MyText = Encrypt(textbox1.text, "aabc123a") ' Here aabc123a is the key and textbox1.text is the text which you want to encrypt
    End Sub

Private Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Try
            Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
            Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
            Dim des As New DESCryptoServiceProvider
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
            cs.Write(InputByteArray, 0, InputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Private Sub cmdDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MyText = Decrypt(TextBox2.Text, "aabc123a")
End Sub

Private Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Dim inputByteArray(strText.Length) As Byte
        Try
            Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
            Dim des As New DESCryptoServiceProvider
            inputByteArray = Convert.FromBase64String(strText)
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Remember to add top of import: Imports System.Security.Cryptography

Recommended Answers

All 2 Replies

What errors are you getting? What database are you using? Usually encrypting a string results in a longer string than the original input data so you may not have the columns defined wide enough in your database.

Encrypting and Decrypting Text, Saving and retrieving it from Database, Full Working Coding

Imports System.Security.Cryptography
Imports System.IO
Imports System.Data.OleDb
Partial Class _Default
    Inherits System.Web.UI.Page
    Private conn_str As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\db_encrypt_decrypt.mdb;Persist Security Info=False"
    Private DataConn As New OleDbConnection(conn_str)
    Private Sub cmdEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEncrypt.Click
        ' Encrypt
        Dim MyText As String
        MyText = Encrypt(TextBox1.Text, "bharani1") ' Here aabc123a is the key and textbox1.text is the text which you want to encrypt
        Label1.Text = MyText
        Dim cmd As New OleDbCommand("insert into tbl_encrypt_decrypt(Pwd) values (?)", DataConn)
        cmd.CommandType = Data.CommandType.Text
        cmd.Parameters.AddWithValue("Pwd", Encrypt(TextBox1.Text, "bharani1"))

        DataConn.Open()
        cmd.ExecuteNonQuery()
        DataConn.Close()
        Response.Write("Success")
    End Sub

    Private Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Try
            Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(strEncrKey)
            Dim InputByteArray() As Byte = System.Text.Encoding.UTF8.GetBytes(strText)
            Dim des As New DESCryptoServiceProvider
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write)
            cs.Write(InputByteArray, 0, InputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

    Private Sub cmdDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDecrypt.Click
        DataConn.Open()
        Dim cmd As New OleDbCommand("select Pwd from tbl_encrypt_decrypt where ID =7", DataConn)
        cmd.ExecuteNonQuery()
        Dim dr As OleDbDataReader
        dr = cmd.ExecuteReader
        While dr.Read()
            Label1.Text = Decrypt(dr("Pwd"), "bharani1").ToString()

        End While
            DataConn.Close()
    End Sub

    Private Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
        Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
        Dim inputByteArray(strText.Length) As Byte
        Try
            Dim byKey() As Byte = System.Text.Encoding.UTF8.GetBytes(sDecrKey)
            Dim des As New DESCryptoServiceProvider
            inputByteArray = Convert.FromBase64String(strText)
            Dim ms As New MemoryStream
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
            Return encoding.GetString(ms.ToArray())
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
    
End Class
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.