Skip to main content

How to Create a Ceaser Cipher Text Encrypter in Visual Basic


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

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. TextBox1.Text = ceaser(True, TextBox1.Text)
  3. End Sub
  4.  
  5. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  6. TextBox2.Text = ceaser(False, TextBox2.Text)
  7. End Sub

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

  1. Private Function ceaser(ByVal encrypt As Boolean, ByVal s As String)
  2. Dim r As String = Nothing
  3. If (encrypt) Then
  4. For Each c As Char In s
  5. If (c = "a") Then r &= "m"
  6. If (c = "s") Then r &= "r"
  7. If (c = "v") Then r &= "i"
  8. If (Not c = "a" And Not c = "s" And Not c = "v") Then r &= c
  9. Next
  10. Else
  11. For Each c As Char In s
  12. If (c = "m") Then r &= "a"
  13. If (c = "r") Then r &= "s"
  14. If (c = "i") Then r &= "v"
  15. If (Not c = "m" And Not c = "i" And Not c = "r") Then r &= c
  16. Next
  17. End If
  18. Return r
  19. End Function

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.

  1. Public Class Form1
  2. Private Function ceaser(ByVal encrypt As Boolean, ByVal s As String)
  3. Dim r As String = Nothing
  4. If (encrypt) Then
  5. For Each c As Char In s
  6. If (c = "a") Then r &= "m"
  7. If (c = "s") Then r &= "r"
  8. If (c = "v") Then r &= "i"
  9. If (Not c = "a" And Not c = "s" And Not c = "v") Then r &= c
  10. Next
  11. Else
  12. For Each c As Char In s
  13. If (c = "m") Then r &= "a"
  14. If (c = "r") Then r &= "s"
  15. If (c = "i") Then r &= "v"
  16. If (Not c = "m" And Not c = "i" And Not c = "r") Then r &= c
  17. Next
  18. End If
  19. Return r
  20. End Function
  21. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  22. TextBox1.Text = ceaser(True, TextBox1.Text)
  23. End Sub
  24.  
  25. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  26. TextBox2.Text = ceaser(False, TextBox2.Text)
  27. End Sub
  28. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.