Vigenere Cipher in VB.NET

There are many ciphers out there which you can use whenever you want to encrypt something. Encryption is nothing but sending some data in some coded form so that even if anybody hacks into the data won’t know what was being sent. It is decrypted at the other end with a key so that the original data can be retrieved. I will describe (and show) how to use the Vigenere cipher in your programs.

Vigenere cipher shifts the characters in a string by some places. Now those some places are determined by the characters that are present in the key string. What my code does is:

  1. Retrieves the unicode position of the first character in the input string.
  2. Retrieves the unicode position of the first character in the key string.
  3. It shifts forward the first characters by the number of positions which was retrieved in point 2.
  4. It does the same for the second, third, etc. characters until the input string gets over.
  5. If the key string ends, it repeats the key string again from the first character.
  6. For decryption, the same process, just reversed. The characters are shifted backwards and the original string is obtained back.

The Code:

Imports Microsoft.VisualBasic

Public Class VigenereCipher

  ''' <summary>
  ''' Encrypts the provided string with Vigenere Cipher
  ''' </summary>
  ''' <param name="cipherTxt">The text that is to be encrypted</param>
  ''' <param name="key">The key to be used for encrpytion</param>
  ''' <returns>The string containing the encrypted text</returns>
  ''' <remarks></remarks>
  Public Shared Function Encrypt(ByVal cipherTxt As String, ByVal key As String)
    Dim encryptedText As String = ""
    For i As Integer = 1 To cipherTxt.Length
      Dim temp As Integer = AscW(GetChar(cipherTxt, i)) + AscW(GetChar(key, i Mod key.Length + 1))
      encryptedText += ChrW(temp)
    Next
    Return encryptedText
  End Function

  ''' <summary>
  ''' Decrypts the provided string with Vigenere Cipher
  ''' </summary>
  ''' <param name="cipherTxt">The text that is to be decrypted</param>
  ''' <param name="key">The key to be used for decrpytion</param>
  ''' <returns>The string containing the decrypted text</returns>
  ''' <remarks></remarks>
  Public Shared Function Decrypt(ByVal cipherTxt As String, ByVal key As String)
    Dim decryptedText As String = ""
    For i As Integer = 1 To cipherTxt.Length
      Dim temp As Integer = AscW(GetChar(cipherTxt, i)) - AscW(GetChar(key, i Mod key.Length + 1))
      decryptedText += ChrW(temp)
    Next
    Return decryptedText
  End Function
End Class

Don’t forget to import the Microsoft.VisualBasic namespace because it holds all the functions that do the retrieving of unicode positions. The AscW function returns the character code corresponding the character and the ChrW returns the character corresponding to the character code. The XML lines which are written above the functions are its documentation. Visual Studio uses those to show help in its Intellisense.

Usage:

To use the function write anywhere in you code:
VigenereCipher.Encrypt(inputStr, keyStr)
And you’re done. If any doubts, ask me. To download the source file, click here.

Advertisements

19 thoughts on “Vigenere Cipher in VB.NET

  1. can you explain the code in detail please im trying to learn new languages for university thanks ! πŸ˜€ also can you explain in the ascii method thanks

    Like

    1. Hi Ahmed,

      I’m glad that you found this post helpful and I’d be happy to help.

      What the code does is pretty simple. I get the character code for a single character using the function AscW, shift it some positions ahead which is decided by the key value and then convert it back to a character using the ChrW function. You can learn the encryption and decryption algorithms on the Wikipedia page here.

      If you want to use ASCII, use the functions Asc and Chr instead of AscW and ChrW respectively.

      Regards,
      TurboBlaze

      Like

    1. I haven’t provided any user interface with this code. It is just the class and function implementation. You can use it anywhere.

      Design a user interface with three textboxes (one for input, one for output and one for the key input) and a button. Then add this class and write some code and your work is done.

      These functions can take any character or even sentences. They will encrypt everything within the Unicode range. And since Unicode is universal you can assume that it can encrypt (or decrypt) anything.

      Like

    1. Hi Milly,

      Write a program to accept input from a text file. Then add this class and encrypt all the input text and write everything to another file. This way, you’ll get your encrypted text file.

      To read and write files you can follow this post.

      Regards,
      TurboBlaze

      Like

    1. Hi Alfredo,

      If you want to output in ASCII characters, just change the function AscW and ChrW with Asc and Chr. Just remove the W in both the functions and you are done.

      Happy Coding,
      TurboBlaze

      Like

        1. Hi again,

          Here is something I have thought of but it is untested (I don’t have enough time to test) so you try it out and tell if it works or not. Change line number 15 to:
          Dim temp As Integer = AscW ( (GetChar(cipherTxt, i)) + AscW(GetChar(key, i Mod key.Length + 1)) Mod (AscW("Z") - AscW("A")) )

          But this would only work if all your characters in the input string and the key are uppercase. For lowercase, replace “Z” and “A” by “z” and “a”. If you want to keep both uppercase and lowercase, then write for uppercase and lowercase in an if-else statement by checking whether the character to be encrypted is upper or lower case that too in both functions. It is too complicated. You would end up with spaghetti code. It is better to stick with the original code rather than doing all that.

          Happy Coding,
          TurboBlaze

          Like

Your Thoughts -

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s