Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Cryptography 101 for the .NET Framework

, 22 Dec 2006 CPOL
An introduction to key cryptographic concepts supported by the .NET Framework.
cryptohelper.zip
CryptoHelper
bin
Release
CryptoHelper.dll
My Project
Application.myapp
Settings.settings
SimpleEncryptor
bin
Release
CryptoHelper.dll
SimpleEncryptor.exe
SimpleEncryptor.vshost.exe
My Project
Application.myapp
Settings.settings
SimpleEncryptor.vbproj.user
Imports System.Security.Cryptography
Imports System.Text.Encoding
Imports System.text
Imports System.io

'This is our helper class
Imports TobyEmden.Security.Encryption

Public Class Form1

    Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        If rdoEncrypt.Checked AndAlso tbSourceText.Text.Length.Equals(0) Then
            MessageBox.Show("You must enter some source text")
            Exit Sub
        End If

        If rdoDecrypt.Checked AndAlso tbOutputText.Text.Length.Equals(0) Then
            MessageBox.Show("You must provide an encrypted string")
            Exit Sub
        End If

        Dim plainText As String = String.Empty
        Dim encText As String = String.Empty
        Dim key As String = String.Empty
        Dim alg As Crypto.Algorithm = cbAlgorithm.SelectedIndex

        plainText = tbSourceText.Text
        encText = tbOutputText.Text
        key = tbKey.Text

        If rdoEncrypt.Checked Then
            Crypto.EncryptionAlgorithm = alg

            If rdoHex.Checked Then
                Crypto.Encoding = Crypto.EncodingType.HEX
            Else
                Crypto.Encoding = Crypto.EncodingType.BASE_64
            End If

            If Crypto.IsHashAlgorithm Then
                If Crypto.GenerateHash(plainText) Then
                    tbOutputText.Text = Crypto.Content
                Else
                    tbOutputText.Text = Crypto.CryptoException.Message
                End If
            Else
                Crypto.Key = key

                If Crypto.EncryptString(plainText) Then
                    tbOutputText.Text = Crypto.Content
                Else
                    tbOutputText.Text = Crypto.CryptoException.Message
                End If
            End If

            Crypto.Clear()
        Else
            Crypto.EncryptionAlgorithm = alg

            If Crypto.IsHashAlgorithm Then
                tbOutputText.Text = "Cannot decipher a one-way hash"
                Crypto.Clear()
                Exit Sub
            End If

            Crypto.Key = key

            If rdoHex.Checked Then
                Crypto.Encoding = Crypto.EncodingType.HEX
            Else
                Crypto.Encoding = Crypto.EncodingType.BASE_64
            End If

            Crypto.Content = tbOutputText.Text

            If Crypto.DecryptString Then
                tbOutputText.Text = Crypto.Content
            Else
                tbOutputText.Text = Crypto.CryptoException.Message
            End If

            Crypto.Clear()
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Initialize form by selecting the first item in the dropdowns
        cbAlgorithm.SelectedIndex = 0
        cbAlgorithmFile.SelectedIndex = 0
    End Sub

    Private Sub btnInputFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInputFile.Click
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.CheckFileExists = True
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            tbInputFile.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub btnOutputFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOutputFile.Click
        SaveFileDialog1.CheckPathExists = True
        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            tbOutputFile.Text = SaveFileDialog1.FileName
        End If
    End Sub

    Private Sub btnFileExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFileExecute.Click
        Dim alg As Crypto.Algorithm = cbAlgorithmFile.SelectedIndex
        Dim key As String = tbKeyFile.Text

        Crypto.EncryptionAlgorithm = alg
        Crypto.Key = key

        If rdoHex.Checked Then
            Crypto.Encoding = Crypto.EncodingType.HEX
        Else
            Crypto.Encoding = Crypto.EncodingType.BASE_64
        End If

        If rdoEncryptFile.Checked Then
            If Crypto.EncryptFile(tbInputFile.Text, tbOutputFile.Text) Then
                MessageBox.Show("File Encrypted")
            Else
                MessageBox.Show(Crypto.CryptoException.Message)
            End If
            Crypto.Clear()
        Else
            If Crypto.DecryptFile(tbInputFile.Text, tbOutputFile.Text) Then
                MessageBox.Show("File Decrypted")
            Else
                MessageBox.Show(Crypto.CryptoException.Message)
            End If
            Crypto.Clear()
        End If
    End Sub

    
End Class

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)

Share

About the Author

Toby Emden
Architect
United States United States
Toby Emden has been coding since the early days of 8-bit home computers, when developers had to write pure assembly to get anything worthwhile done. As a mere ankle-biter, his first coding projects included several games written for the ZX Spectrum in pure Z80 assembly code. Nowadays, his languages of choice are C++, C# and Java.
 
A member of the IEEE Computer Society and Worldwide Institute of Software Architects, Toby has spent ten years as an I.T. security executive, delivering enterprise security solutions for Fortune 100 organizations.
 
When not boring people at parties with jokes about cryptography and polymorphism, he enjoys writing, traveling and spending quality time with his wife and three cats. He still hasn't figured out how to cure cancer with .NET, but figures world peace can be solved with a Java wrapper class.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Dec 2006
Article Copyright 2006 by Toby Emden
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid