Vigenere Cipher

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
    C D E F G H I J K L M N O P Q R S T U V W X Y Z A B
    D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
    E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
    F G H I J K L M N O P Q R S T U V W X Y Z A B C D E
    G H I J K L M N O P Q R S T U V W X Y Z A B C D E F
    H I J K L M N O P Q R S T U V W X Y Z A B C D E F G
    I J K L M N O P Q R S T U V W X Y Z A B C D E F G H
    J K L M N O P Q R S T U V W X Y Z A B C D E F G H I
    K L M N O P Q R S T U V W X Y Z A B C D E F G H I J
    L M N O P Q R S T U V W X Y Z A B C D E F G H I J K
    M N O P Q R S T U V W X Y Z A B C D E F G H I J K L
    N O P Q R S T U V W X Y Z A B C D E F G H I J K L M
    O P Q R S T U V W X Y Z A B C D E F G H I J K L M N
    P Q R S T U V W X Y Z A B C D E F G H I J K L M N O
    Q R S T U V W X Y Z A B C D E F G H I J K L M N O P
    R S T U V W X Y Z A B C D E F G H I J K L M N O P Q
    S T U V W X Y Z A B C D E F G H I J K L M N O P Q R
    T U V W X Y Z A B C D E F G H I J K L M N O P Q R S
    U V W X Y Z A B C D E F G H I J K L M N O P Q R S T
    V W X Y Z A B C D E F G H I J K L M N O P Q R S T U
    W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
    X Y Z A B C D E F G H I J K L M N O P Q R S T U V W
    Y Z A B C D E F G H I J K L M N O P Q R S T U V W X
    Z A B C D E F G H I J K L M N O P Q R S T U V W X Y

This is known as the Vigenere Table, it consists of the alphabet written out 26 times on each line. At the start of each line the order of the alphabet shifts to the left and the last letter on the left starts over at the far right again. The cipher you could make out of this actually requires a keyword in order to encrypt and decrypt it. For example,

My secret message is: JAKASHTHREE

and my secret key is: FINAL

The encrypted text is the intersection of each character from the secret key with each character of the secret message. You navigate the intersection by seeking out the key characters on the first line, and the message characters on the first column. Now since the key is shorter than the message, we will have to pad it to get the same length so we can calculate the intersections.

So the key FINAL becomes: FINALFINALF

Now the encrypted text is from both the key and message is: OIXADMBURPJ

To finally fully understand this take a look at every character of the encrypted text. Now go to the first line of the table and find the first letter of the key on that line, which is F. Now that you are on the F column, move vertically down until you hit the first letter of the encrypted text, which is O. If you look to the left, you will notice that you are now on the line that starts with J, that is, the first letter of the real message: JAKASHTHREE. Just keep repeating this procedure incrementing the pointer of the key and the encrypted message and you will have all the letters of the decoded message figured out.

Here’s psuedocode that demonstrates the encryption:

Sub Main()

   string msg=input("Enter new message: ")
   string key=input("Enter new key: ")
   Print(NewLine)

   If (msg.length < key.length) Then
      PrintL("Message length cannot be less than key length.")
      Return 1
   EndIf

   PrintL("Encoded text: " & encode(msg,key))

End Sub

Function encode(string msg,string key)

   tbl = New Hashtable()
   tbl.Add("A","ABCDEFGHIJKLMNOPQRSTUVWXYZ")
   tbl.Add("B","BCDEFGHIJKLMNOPQRSTUVWXYZA")
   tbl.Add("C","CDEFGHIJKLMNOPQRSTUVWXYZAB")
   tbl.Add("D","DEFGHIJKLMNOPQRSTUVWXYZABC")
   tbl.Add("E","EFGHIJKLMNOPQRSTUVWXYZABCD")
   tbl.Add("F","FGHIJKLMNOPQRSTUVWXYZABCDE")
   tbl.Add("G","GHIJKLMNOPQRSTUVWXYZABCDEF")
   tbl.Add("H","HIJKLMNOPQRSTUVWXYZABCDEFG")
   tbl.Add("I","IJKLMNOPQRSTUVWXYZABCDEFGH")
   tbl.Add("J","JKLMNOPWRSTUVWXYZABCDEFGHI")
   tbl.Add("K","KLMNOPQRSTUVWXYZABCDEFGHIJ")
   tbl.Add("L","LMNOPQRSTUVWXYZABCDEFGHIJK")
   tbl.Add("M","MNOPQRSTUVWXYZABCDEFGHIJKL")
   tbl.Add("N","NOPQRSTUVWXYZABCDEFGHIJKLM")
   tbl.Add("O","OPQRSTUVWXYZABCDEFGHIJKLMN")
   tbl.Add("P","PQRSTUVWXYZABCDEFGHIJKLMNO")
   tbl.Add("Q","QRSTUVWXYZABCDEFGHIJKLMNOP")
   tbl.Add("R","RSTUVWXYZABCDEFGHIJKLMNOPQ")
   tbl.Add("S","STUVWXYZABCDEFGHIJKLMNOPQR")
   tbl.Add("T","TUVWXYZABCDEFGHIJKLMNOPQRS")
   tbl.Add("U","UVWXYZABCDEFGHIJKLMNOPQRST")
   tbl.Add("V","VWXYZABCDEFGHIJKLMNOPQRSTU")
   tbl.Add("W","WXYZABCDEFGHIJKLMNOPQRSTUV")
   tbl.Add("X","XYZABCDEFGHIJKLMNOPQRSTUVW")
   tbl.Add("Y","YZABCDEFGHIJKLMNOPQRSTUVWX")
   tbl.Add("Z","ZABCDEFGHIJKLMNOPQRSTUVWXY")

   If (key.length < msg.length) Then
      Int pointer = 0
      Do Until (key.length = msg.length)
         key += key.SubStr(pointer,1)
         pointer += 1
      Loop
   EndIf

   msg = msg.ToUpper()
   key = key.ToUpper()

   string encoded = ""

   For Each (string s) In (msg)
      string y = tbl["A"]
      int index = y.IndexOf(key.SubStr(0,1))
      string line = tbl[s]
      encoded += line.SubStr(index,1)
      key = key.Remove(0,1)
   Next

   Return encoded

End Function

One thought on “Vigenere Cipher

Leave a Reply

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

%d bloggers like this: