Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / Visual Basic

.NET Encryption Simplified

Rate me:
Please Sign up or sign in to vote.
4.90/5 (209 votes)
28 Jan 2007CPOL10 min read 1.8M   23.8K   586  
A simple, string-oriented class for symmetric encryption, asymmetric encryption, and hashing.
Imports EncryptionClassLibrary

''' <summary>
''' Console demonstration app for Encryption class
''' </summary>
''' <remarks>
'''   Jeff Atwood
'''   http://www.codinghorror.com/
''' </remarks>
Module Module1

    Sub Main()
        DemoSymmetric()
        DemoAsymmetric()
        DemoHash()
    End Sub


    ''' <summary>
    ''' Demonstration of various Encryption.Symmetric methods
    ''' </summary>
    Sub DemoSymmetric()
        Dim Secret As String = "Pack my box with five dozen liquor jugs!"
        Dim Key As String = "MyKey"
        Dim p As Encryption.Symmetric.Provider = Encryption.Symmetric.Provider.TripleDES

        Console.WriteLine("our secret string")
        Console.WriteLine("  " & Secret)
        Console.WriteLine()

        Dim sym As New Encryption.Symmetric(p)
        sym.Key.Text = Key

        Console.WriteLine("Symmetric encryption algorithm")
        Console.WriteLine("  " & p.ToString)
        Console.WriteLine("Current key size")
        Console.WriteLine("  " & sym.KeySizeBits & " bits")
        Console.WriteLine("Possible key sizes")
        Console.WriteLine("  " & sym.Key.MinBits & " bits to " & sym.Key.MaxBits & " bits, in increments of " & sym.Key.StepBits & " bits")
        Console.WriteLine("Initialization Vector size")
        Console.WriteLine("  " & sym.IntializationVector.Bytes.Length * 8 & " bits")
        Console.WriteLine()

        Console.WriteLine("Key")
        Console.WriteLine("  Text    " & sym.Key.Text)
        Console.WriteLine("  Hex     " & sym.Key.Hex)
        Console.WriteLine("  Base64  " & sym.Key.Base64)
        Console.WriteLine()

        Dim encryptedData As Encryption.Data
        encryptedData = sym.Encrypt(New Encryption.Data(Secret))

        Console.WriteLine("Encrypted String")
        Console.WriteLine("  Hex     " & encryptedData.Hex)
        Console.WriteLine("  Base64  " & encryptedData.Base64)
        Console.WriteLine()

        Dim decryptedData As Encryption.Data
        Dim sym2 As New Encryption.Symmetric(p)
        sym2.Key.Text = sym.Key.Text
        decryptedData = sym2.Decrypt(encryptedData)
        Console.WriteLine("decrypted string")
        Console.WriteLine("  " & decryptedData.Text)

        Console.WriteLine("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    ''' <summary>
    ''' Demonstration of various Encryption.Asymmetric methods
    ''' </summary>
    Sub DemoAsymmetric()
        Dim asym As New Encryption.Asymmetric
        Dim pubkey As New Encryption.Asymmetric.PublicKey
        Dim privkey As New Encryption.Asymmetric.PrivateKey

        asym.GenerateNewKeyset(pubkey, privkey)
        Console.WriteLine("Current key size: " & asym.KeySizeBits & " bits")
        Console.WriteLine("Maximum key size: " & asym.KeySizeMaxBits & " bits")
        Console.WriteLine("Minimum key size: " & asym.KeySizeMinBits & " bits")
        Console.WriteLine("key step size:    " & asym.KeySizeStepBits & " bits")
        Console.WriteLine(Environment.NewLine)

        Dim Secret As String = "Pack my box with five dozen liquor jugs!"

        Dim EncryptedData As Encryption.Data
        EncryptedData = asym.Encrypt(New Encryption.Data(Secret), pubkey)

        Console.WriteLine("public key:")
        Console.WriteLine(pubkey.ToXml)

        Console.WriteLine("encrypted Base64:")
        Console.WriteLine(EncryptedData.Base64)

        Console.WriteLine("encrypted Hex:")
        Console.WriteLine(EncryptedData.Hex)

        Dim DecryptedData As Encryption.Data
        Dim asym2 As New Encryption.Asymmetric
        DecryptedData = asym2.Decrypt(EncryptedData, privkey)
        Console.WriteLine("decrypted secret:")
        Console.WriteLine(DecryptedData.Text)
        Console.WriteLine(Environment.NewLine)

        Console.WriteLine("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    ''' <summary>
    ''' Demonstration of various Encryption.Hash methods
    ''' </summary>
    Sub DemoHash()

        Dim d As New Encryption.Data( _
            "{ts '2004-10-09 08:10:04'}The world is beautiful and needs caring by its children")

        Dim hash As New Encryption.Hash(Encryption.Hash.Provider.SHA1)
        Dim hash2 As New Encryption.Hash(Encryption.Hash.Provider.SHA256)
        Dim hash3 As New Encryption.Hash(Encryption.Hash.Provider.SHA384)
        Dim hash4 As New Encryption.Hash(Encryption.Hash.Provider.SHA512)
        Dim hash5 As New Encryption.Hash(Encryption.Hash.Provider.MD5)
        Dim hash6 As New Encryption.Hash(Encryption.Hash.Provider.CRC32)

        hash.Calculate(d)
        hash2.Calculate(d)
        hash3.Calculate(d)
        hash4.Calculate(d)
        hash5.Calculate(d)

        Console.WriteLine("SHA1:   " & hash.Value.Hex)
        Console.WriteLine("SHA256: " & hash2.Value.Hex)
        Console.WriteLine("SHA384: " & hash3.Value.Hex)
        Console.WriteLine("SHA512: " & hash4.Value.Hex)
        Console.WriteLine("MD5:    " & hash5.Value.Hex)
        Console.WriteLine("CRC32:  " & hash6.Calculate(d).Hex)
        Console.WriteLine()

        Dim salt As New Encryption.Data("salty!")
        Console.WriteLine("Salted CRC32:  " & hash6.Calculate(d, salt).Hex)

        Console.WriteLine("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
My name is Jeff Atwood. I live in Berkeley, CA with my wife, two cats, and far more computers than I care to mention. My first computer was the Texas Instruments TI-99/4a. I've been a Microsoft Windows developer since 1992; primarily in VB. I am particularly interested in best practices and human factors in software development, as represented in my recommended developer reading list. I also have a coding and human factors related blog at www.codinghorror.com.

Comments and Discussions