Encrypt DataSet to File in vb.net

Hope everyone had a great weekend. Myself, I used a sledgehammer to break bricks that previous owners of my home had left for some unknown reason. Today I am sore, but feeling like I had accomplished something.

Anyway there are times we need to save data (i.e. a dataset) to the local machine but not necessarily expose the data within to the end user outside of the application. Here is what I came up with.

Imports System.Text
Imports System.Security.Cryptography
Imports System.Xml.Serialization
Imports System.IO
Public Class Form1
    Function EncryptString(ByVal strKey As String, ByVal strValue As String) As String
        Dim intputVector() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
        If strKey.Length <> 8 Then Throw New Exception("Key length must equal 8.")
        If (strValue.Length <= 0) Then Throw New Exception("Length of string to encrypt cannot be zero.")
        Dim MS As New MemoryStream
        Dim DES As New DESCryptoServiceProvider
        Dim CS As CryptoStream = Nothing
        Try
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strValue)
            Dim keyByteArray() As Byte = Encoding.UTF8.GetBytes(strKey)
            CS = New CryptoStream(MS, DES.CreateEncryptor(keyByteArray, intputVector), CryptoStreamMode.Write)
            CS.Write(inputByteArray, 0, inputByteArray.Length)
            CS.FlushFinalBlock()
        Catch ex As Exception
        Finally
            CS.Close()
            MS.Close()
        End Try
        Return Convert.ToBase64String(MS.ToArray())
    End Function
    Function DecryptString(ByVal strKey As String, ByVal strValue As String) As String
        Dim intputVector() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
        If strKey.Length <> 8 Then Throw New Exception("Key length must equal 8.")
        If (strValue.Length <= 0) Then Throw New Exception("Length of string to encrypt cannot be zero.")
        Dim MS As New MemoryStream
        Dim DES As New DESCryptoServiceProvider
        Dim CS As CryptoStream = Nothing
        Try
            Dim inputByteArray() As Byte = Convert.FromBase64String(strValue)
            Dim keyByteArray() As Byte = Encoding.UTF8.GetBytes(strKey)
            CS = New CryptoStream(MS, DES.CreateDecryptor(keyByteArray, intputVector), CryptoStreamMode.Write)
            CS.Write(inputByteArray, 0, inputByteArray.Length)
            CS.FlushFinalBlock()
        Catch ex As Exception
        Finally
            CS.Close()
            MS.Close()
        End Try
        Return Encoding.UTF8.GetString(MS.ToArray())
    End Function
    Private Sub btnEncryptDataSetToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncryptDataSetToFile.Click
        Dim DS As New DataSet
        Dim Row As DataRow
        Dim tblTables As DataTable
        tblTables = DS.Tables.Add("Table1")
        tblTables.Columns.Add("Field1")
        Row = tblTables.Rows.Add("Entry1")
        Dim DataString As New StringBuilder
        Dim serializer As New XmlSerializer(GetType(DataSet))
        Using myStringWriter = New StringWriter(DataString)
            serializer.Serialize(myStringWriter, DS)
        End Using
        Using myStreamWriter = New StreamWriter("c:\newEnc.txt")
            myStreamWriter.Write(EncryptString("12345678", DataString.ToString))
        End Using
        MsgBox("Done")
    End Sub
    Private Sub btnPopulateDSFromEncryptedFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulateDSFromEncryptedFile.Click
        Dim serializer As New XmlSerializer(GetType(DataSet))
        Dim decryptedData As New StringBuilder
        Using myStreamReader As New StreamReader("c:\newEnc.txt")
            decryptedData.Append(DecryptString("12345678", myStreamReader.ReadToEnd()))
        End Using
        Dim DS As New DataSet
        Using myStringReader As New StringReader(decryptedData.ToString)
            DS = serializer.Deserialize(myStringReader)
        End Using
        ‘——- optional – to test results
        DS.WriteXml("c:\decoded.txt", XmlWriteMode.WriteSchema)
        ‘——-
        MsgBox("Done")
    End Sub
End Class

About these ads
  1. #1 by Travis on April 20, 2010 - 12:40 pm

    what does all this stuff mean? How do I encrypt my email, in english?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: