How to Create a Ceaser Cipher Text Encrypter in Visual Basic
Submitted by GeePee on Wednesday, April 22, 2015 - 22:48.
Introduction:
Welcome to a tutorial on how to create a simple Ceaser Cipher text encrypter using Visual Basic.
Notes:
In this tutorial I only did three letters as an example, you will want to do the same technique for every letter of the alphabet to avoid messages getting muddled.
Steps of Creation:
Step 1:
First you want to create a form with;
textbox1 - Message to encrypt
textbox2 - Message to decrypt
Button1 - Encrypt
Button2 - Decrypt
Step 2:
We will be using one function to deal with all the encrypting and decrypting so lets do the button functions first, we simply reset the text of the appropriate textbox to the encrypted/decrypted one...
Step 3:
Now for the custom function. We first check whether it is encryption or decryption which is wanted (parsed through the "encrypt" boolean input parameter), afterwards we do the appropriate action to the s String...
As you see, I have only done this for three letters as an example. This means certain messages will get muddled up if a given letter (encryptable) is used in a standard work, this will give the word an incorrect letter. To avoid this you MUST do the Ceaser Cipher technique to all the alphabet letters.
Project Complete!
That's it! Below is the full source code and download to the project files.
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- TextBox1.Text = ceaser(True, TextBox1.Text)
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- TextBox2.Text = ceaser(False, TextBox2.Text)
- End Sub
- Private Function ceaser(ByVal encrypt As Boolean, ByVal s As String)
- Dim r As String = Nothing
- If (encrypt) Then
- For Each c As Char In s
- If (c = "a") Then r &= "m"
- If (c = "s") Then r &= "r"
- If (c = "v") Then r &= "i"
- If (Not c = "a" And Not c = "s" And Not c = "v") Then r &= c
- Next
- Else
- For Each c As Char In s
- If (c = "m") Then r &= "a"
- If (c = "r") Then r &= "s"
- If (c = "i") Then r &= "v"
- If (Not c = "m" And Not c = "i" And Not c = "r") Then r &= c
- Next
- End If
- Return r
- End Function
- Public Class Form1
- Private Function ceaser(ByVal encrypt As Boolean, ByVal s As String)
- Dim r As String = Nothing
- If (encrypt) Then
- For Each c As Char In s
- If (c = "a") Then r &= "m"
- If (c = "s") Then r &= "r"
- If (c = "v") Then r &= "i"
- If (Not c = "a" And Not c = "s" And Not c = "v") Then r &= c
- Next
- Else
- For Each c As Char In s
- If (c = "m") Then r &= "a"
- If (c = "r") Then r &= "s"
- If (c = "i") Then r &= "v"
- If (Not c = "m" And Not c = "i" And Not c = "r") Then r &= c
- Next
- End If
- Return r
- End Function
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- TextBox1.Text = ceaser(True, TextBox1.Text)
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- TextBox2.Text = ceaser(False, TextBox2.Text)
- End Sub
- End Class
Add new comment
- 267 views