Locations of visitors to this page
Onteora Software - July 2006

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Tampa Code Camp

Tampa Code Camp



I did a session at the Tampa code camp on the datagridview. I covered sorting, changing column types, cell format event, sql dependency, and exporting to spread sheets.

Download the powerpoint and sample code.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Camp | DataGridView
Posted by Ken Tucker on Saturday, July 15, 2006 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Encrypting Data

Encrypting Data



Here is a vb2005 example on how to encrypt and decrypt data. I am storing the encryption key as a base64 string in the programs settings. The settings name is Key and its starting string value was Unknown. In a production application I would store it in a more secure location.





Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Class Form1
    Public Key() As Byte
    Public IV() As Byte

    Public Function Encrypt(ByVal strData As String) As Byte()
        Dim data() As Byte = ASCIIEncoding.ASCII.GetBytes(strData)
        Dim tdes As TripleDESCryptoServiceProvider = _
                New TripleDESCryptoServiceProvider
        If Key Is Nothing Then
            tdes.GenerateKey()
            tdes.GenerateIV()
            Key = tdes.Key
            IV = tdes.IV
        Else
            tdes.Key = Key
            tdes.IV = IV
        End If
        Dim encryptor As ICryptoTransform = tdes.CreateEncryptor()
        Dim ms As New MemoryStream
        Dim cs As CryptoStream = _
                New CryptoStream(ms, encryptor, CryptoStreamMode.Write)
        cs.Write(data, 0, data.Length)
        cs.FlushFinalBlock()
        ms.Position = 0
        Dim result(Convert.ToInt32(ms.Length - 1)) As Byte
        ms.Read(result, 0, Convert.ToInt32(ms.Length))
        cs.Close()
        Return result
    End Function

    Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
        Dim data() As Byte
        data = System.Text.Encoding.Default.GetBytes(TextBox1.Text)
        Dim result() As Byte

        Dim sha As New SHA1Managed
        ' This is one implementation of the abstract class SHA1.
        result = sha.ComputeHash(data)
        lblEncrypted.Text = Convert.ToBase64String(Encrypt(TextBox1.Text))
        lblDecrypted.Text = Decrypt(Convert.FromBase64String(lblEncrypted.Text))
    End Sub

    Public Function GetEncryptedData(ByVal Data As String) As String
        Dim shaM As New SHA1Managed
        Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data)))
        Dim eNC_data() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(Data)
        Dim eNC_str As String = Convert.ToBase64String(eNC_data)
        GetEncryptedData = eNC_str
    End Function

    Public Function GetDecryptedData(ByVal Data As String) As String
        Dim dEC_data() As Byte = Convert.FromBase64String(Data)
        Dim dEC_Str As String = System.Text.ASCIIEncoding.ASCII.GetString(dEC_data)
        GetDecryptedData = dEC_Str
    End Function
    Public Function Decrypt(ByVal data() As Byte) As String
        Dim tdes As TripleDESCryptoServiceProvider = _
                New TripleDESCryptoServiceProvider
        tdes.Key = Key
        tdes.IV = IV
        Dim decryptor As ICryptoTransform = tdes.CreateDecryptor()
        Dim ms As New MemoryStream
        Dim cs As CryptoStream = New CryptoStream(ms, decryptor, CryptoStreamMode.Write)
        cs.Write(data, 0, data.Length)
        cs.FlushFinalBlock()
        ms.Position = 0
        Dim result(Convert.ToInt32(ms.Length - 1)) As Byte
        ms.Read(result, 0, Convert.ToInt32(ms.Length))
        cs.Close()
        Return ASCIIEncoding.ASCII.GetString(result)
    End Function

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            My.Settings.Key = Convert.ToBase64String(Key)
            My.Settings.IV = Convert.ToBase64String(IV)
        Catch
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.Key <> "Unknown" Then
            Key = Convert.FromBase64String(My.Settings.Key)
            IV = Convert.FromBase64String(My.Settings.IV)
        End If

    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General | VB
Posted by Ken Tucker on Saturday, July 01, 2006 10:12 PM
Permalink | Comments (0) | Post RSSRSS comment feed