Click here to Skip to main content
11,813,557 members (43,787 online)
Click here to Skip to main content
Add your own
alternative version

.NET Encryption Simplified

, 28 Jan 2007 1.2M 20.3K 570
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
''' </remarks>
Module Module1

    Sub Main()
    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)

        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("  Text    " & sym.Key.Text)
        Console.WriteLine("  Hex     " & sym.Key.Hex)
        Console.WriteLine("  Base64  " & sym.Key.Base64)

        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)

        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...")
    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")

        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("encrypted Base64:")

        Console.WriteLine("encrypted Hex:")

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

        Console.WriteLine("Press ENTER to continue...")
    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)


        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)

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

        Console.WriteLine("Press ENTER to continue...")
    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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

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

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 29 Jan 2007
Article Copyright 2005 by wumpus1
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid