Thursday, January 23, 2014

An Application Package on Data Encryption and Decryption ~~ VisualBasic Code Snippet + GUI Design

In cryptography, a substitution cipher is a method of  encoding by which units of plaintext are replaced with ciphertext, according to a regular system; the "units" may be single letters (the most common), pairs of letters, triplets of letters, mixtures of the above, and so forth. The receiver deciphers the text by performing an inverse substitution.
There are a number of different types of substitution cipher. If the cipher operates on single letters, it is termed a simple substitution cipher; a cipher that operates on larger groups of letters is termed polygraphic. A monoalphabetic cipher uses fixed substitution over the entire message, whereas a polyalphabetic cipher uses a number of substitutions at different positions in the message, where a unit from the plaintext is mapped to one of several possibilities in the ciphertext and vice versa. http://en.wikipedia.org/wiki/Substitution_cipher

** Click here for 3DES (Data Encryption & Decryption)

Shown below is the screen shot and source code for each module respectively of a data encryption and decryption system using simple substitution cipher.

Screen shot for 'Start Page'
-----------------------------

















Source code for 'Start Page'
-----------------------------
Public Class Start_Page

    Private Sub encrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles encrypt.Click
        frmEncrypt.ShowDialog()
    End Sub

    Private Sub Start_Page_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Height = 357
        Me.Width = 634
    End Sub

    Private Sub decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles decrypt.Click
        frmDecrypt.ShowDialog()
    End Sub

    Private Sub exitt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitt.Click
        Me.Close()
    End Sub


End Class


Screen shot for 'frmEncrypt'
-----------------------------
















Source code for 'frmEncrypt'
-----------------------------
Public Class frmEncrypt

    Private Sub cmdopen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click
        ofd.ShowDialog()
        txtfilename.Text = ofd.FileName

        Try
            txtoriginal.Text = My.Computer.FileSystem.ReadAllText(ofd.FileName)
        Catch ex As Exception  ' Bad practice (catching exception)
            Dim a As String
            a = MessageBox.Show("File not found, Check the file name and try again", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            cmdopen.Focus()
            txtfilename.Text = String.Empty
        End Try
    End Sub

    Private Sub cmdsaveencrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsaveencrypt.Click
        If txtEncrypted.Text = String.Empty Then
            Dim a As String
            a = MessageBox.Show("File not found; Check to make sure the file has been encrypted", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            cmdopen.Focus()
        Else

            Dim inn As String
            inn = InputBox("Enter file name")

            If inn = "" Then

            Else
                System.IO.File.WriteAllText("'" & inn & " ' ", txtEncrypted.Text)

                Dim haha As String
                haha = MessageBox.Show("File saved", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtEncrypted.Text = String.Empty
                txtoriginal.Text = String.Empty
                txtfilename.Text = ""

            End If
          End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdencrypt.Click
        Try
            txtEncrypted.Text = My.Computer.FileSystem.ReadAllText(ofd.FileName).ToLower.Replace("a", "/").Replace("b", "\").Replace("c", "*").Replace("d", "-").Replace("e", "+").Replace("f", "^").Replace("g", "<").Replace("h", ">").Replace("i", "!").Replace("j", "~").Replace("k", "@").Replace("l", "#").Replace("m", "$").Replace("n", "%").Replace("o", "&").Replace("p", "(").Replace("q", ")").Replace("r", "{").Replace("s", "}").Replace("t", "[").Replace("u", "]").Replace("v", "=").Replace("w", "'").Replace("x", "`").Replace("y", ";").Replace("z", ":").Replace("1", "|").Replace("2", "_")

            '-------substitution area--a.k.a replacement cipher------
       
        Catch ex As Exception
            Dim w As String
            w = MessageBox.Show("Check to make sure a file is selected", "Encryption", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cmdopen.Focus()
        End Try
    End Sub
  
    Private Sub cmdclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclose.Click
        Me.Close()
    End Sub

    Private Sub frmEncrypt_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Height = 534
        Me.Width = 970
    End Sub

    Private Sub cmdclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdclear.Click
        txtoriginal.Text = ""
        txtEncrypted.Text = ""
        txtfilename.Text = ""
        cmdopen.Focus()
    End Sub

    Private Sub frmEncrypt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

Screen shot for 'frmDecrypt'
-----------------------------

















Source code  for 'frmDecrypt'
-----------------------------
Public Class frmDecrypt

    Private Sub cmdopen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdopen.Click

        Try
            OpenFileDialog1.ShowDialog()
            txtfilename.Text = OpenFileDialog1.FileName
            txtoriginal.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
        Catch ju As Exception
            Dim viva As String
            viva = MessageBox.Show("No file selected", "Decryption", MessageBoxButtons.OK, MessageBoxIcon.Information)
            cmdopen.Focus()
            txtfilename.Clear()

        End Try
    End Sub

    Private Sub cmddecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddecrypt.Click
        If txtoriginal.Text = "" Then
            Dim viv As String
            viv = MessageBox.Show("No file selected for decryption", "Decryption", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Else
            txtdecrypted.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName).ToLower.Replace("/", "a").Replace("\", "b").Replace("*", "c").Replace("-", "d").Replace("+", "e").Replace("^", "f").Replace("<", "g").Replace(">", "h").Replace("!", "i").Replace("~", "j").Replace("@", "k").Replace("#", "l").Replace("$", "m").Replace("%", "n").Replace("&", "o").Replace("(", "p").Replace(")", "q").Replace("{", "r").Replace("}", "s").Replace("[", "t").Replace("]", "u").Replace("=", "v").Replace("'", "w").Replace("`", "x").Replace(";", "y").Replace(":", "z").Replace("|", "1").Replace("_", "2")
            '--.....decryption arena....don't touch--------------------------------
        End If
    End Sub

    Private Sub cmdexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdexit.Click
        Me.Close()
    End Sub

    Private Sub frmDecrypt_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Height = 534
        Me.Width = 970
    End Sub

    Private Sub cmdsavedecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsavedecrypt.Click
        If txtdecrypted.Text = String.Empty Then
            Dim a As String
            a = MessageBox.Show("File not found", "Decryption", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            cmdopen.Focus()
        Else
            Dim oops As String
            oops = InputBox("Enter file name")

            If oops = "" Then
' Do nothing
            Else
                System.IO.File.WriteAllText("'" & oops & " ' ", txtdecrypted.Text)

                Dim ha As String
                ha = MessageBox.Show("File saved", "Decryption", MessageBoxButtons.OK, MessageBoxIcon.Information)
                txtdecrypted.Text = String.Empty
                txtoriginal.Text = String.Empty
                txtfilename.Text = ""
                'GoTo k
            End If
        End If
    End Sub

    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        txtoriginal.Text = ""
        txtdecrypted.Text = ""
        txtfilename.Text = ""
        cmdopen.Focus()
    End Sub
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Click here for Tripple DES (Data Encryption & Decryption)
============================================