Wednesday, August 20, 2014

An Application Package on, 3DES, Data Encryption & Decryption ~~ VisualBasic Code Snippet + GUI Design

                                  File Menu { --New  --Open  --Save  --Exit  

                                                                        
 Operation Menu--Encrypt file/Data --Decrypt file/Data


Encrypting Data/File (plain text) Using Encryption Key (9090)
** You 're to use your own key **


            Encrypted Data/File


Decrypting Data/File (cipher text) Using  Key (9090)


Decrypted Data/File


                           Format Menu { --Font  --Fore-Color --BackGround-Color
             

**SOURCE CODE "Snippet" ** 


Imports System.Security.Cryptography

Public Class Index   ' Class Begins

    Sub Encryption()

   Dim plainText As String = txtPlain_Cipher_Text.Text    'InputBox("Enter the plain text:")
   Dim EncryptionKey As String = InputBox("Enter Encryption Key")

        Dim container As New Main(EncryptionKey)
        Dim cipherText As String = container.EncryptData(plainText) 
        txtPlain_Cipher_Text.Text = cipherText 
    End Sub
'==================================================
    Sub Decryption()

        If txtPlain_Cipher_Text.Text = String.Empty Then

            Try

                OpenFileDialog.ShowDialog()
                txtPlain_Cipher_Text.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog.FileName)

            Catch Ooops As IO.FileNotFoundException
                'Do nothing
            End Try

        Else

            Dim DecryptionKey As String = InputBox("Enter Decryption Key:")
            Dim container As New Main(DecryptionKey)

            Try
                Dim plainText As String = container.DecryptData(txtPlain_Cipher_Text.Text)
                txtPlain_Cipher_Text.Text = plainText

            Catch ex As System.Security.Cryptography.CryptographicException
                MsgBox("Invalid Key.", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
            End Try

        End If
    End Sub
 '==================================================
    Private Sub EncryptToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EncryptToolStripMenuItem.Click

        Encryption()

    End Sub
 '==================================================
    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

        txtPlain_Cipher_Text.Text = String.Empty

    End Sub
 '==================================================
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        If txtPlain_Cipher_Text.Text = String.Empty Then
            MsgBox("No text found", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
        Else
            SaveFileDialog.ShowDialog()
            Dim path As String = SaveFileDialog.FileName
            Try
                System.IO.File.WriteAllText(path, txtPlain_Cipher_Text.Text)
            Catch ae As ArgumentException
                MsgBox("File not saved; No file name selected", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
            End Try
        End If
    End Sub
 '==================================================
    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

        Try
            OpenFileDialog.ShowDialog()
            txtPlain_Cipher_Text.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog.FileName)
        Catch ex As IO.FileNotFoundException ' Exception caught when closing the Dialog box without making any selection
            'do nothing
        End Try
    End Sub
 '==================================================
    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        FontDialog.ShowDialog()

        txtPlain_Cipher_Text.Font = FontDialog.Font

    End Sub
 '==================================================
    Private Sub ForeColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ForeColorToolStripMenuItem.Click

        ColorDialog.ShowDialog()
        txtPlain_Cipher_Text.ForeColor = ColorDialog.Color

    End Sub
 '==================================================
    Private Sub BackGroundColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackGroundColorToolStripMenuItem.Click

        ColorDialog.ShowDialog()
        txtPlain_Cipher_Text.BackColor = ColorDialog.Color

    End Sub
 '==================================================
   
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

        Me.Close()

    End Sub
 '==================================================
    Private Sub DecryptToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DecryptToolStripMenuItem.Click

        Decryption()

    End Sub 

End Class  ' Class Ends Here
 '==================================================
   

No comments: