Vigenere Cipher

In cryptography, a Vigenère cipher is a method of encrypting alphabetic text by using a series of different Caesar ciphers based on the letters of a keyword. It is a simple form of polyalphabetic substitution.

The Vigenère cipher has been reinvented many times. The method was originally described by Giovan Battista Bellaso in his 1553 book La cifra del. Sig. Giovan Battista Bellaso; however, the scheme was later misattributed to Blaise de Vigenère in the 19th century, and is now widely known as the Vigenère cipher.

This scheme of cipher uses a text string (say, a word) as a key, which is then used for doing a number of shifts on the plaintext.

For example, let’s assume the key is 'cipher'. Each alphabet of the key is converted to its respective numeric value: In this case,

c → 2, i → 8, p → 15, h → 7, e → 4, r → 17.

Thus, the key is: 2 8 15 7 4 17.

 

Algorithm

  • The sender and the receiver decide on a key. Say 'cipher' is the key. Numeric representation of this key is '2 8 15 7 4 17'
  • The sender wants to encrypt the message, say 'cryptography'. He will arrange plaintext and numeric key as follows −
c r y p t o g r a p h y
2 8 15 7 4 17 2 8 15 7 4 17

 

  • He now shifts each plaintext alphabet by the number written below it to create ciphertext as shown below −
c r y p t o g r a p h y
2 8 15 7 4 17 2 8 15 7 4 17
e z n w x f i z p w l p

 

  • Here, each plaintext character has been shifted by a different amount – and that amount is determined by the key. The key must be less than or equal to the size of the message.
  • For decryption, the receiver uses the same key and shifts received ciphertext in reverse order to obtain the plaintext.
e z n w x f i z p w l p
2 8 15 7 4 17 2 8 15 7 4 17
c r y p t o g r a p h y

 

Security

Vigenere Cipher was designed by tweaking the standard Caesar cipher to reduce the effectiveness of cryptanalysis on the ciphertext and make a cryptosystem more robust. It is significantly more secure than a regular Caesar cipher.

In the history, it was regularly used for protecting sensitive political and military information. It was referred to as the unbreakable cipher due to the difficulty it posed to the cryptanalysis.



									Private Shared Function [Mod](a As Integer, b As Integer) As Integer
	Return (a Mod b + b) Mod b
End Function

Private Shared Function Cipher(input As String, key As String, encipher As Boolean) As String
	For i As Integer = 0 To key.Length - 1
		If Not Char.IsLetter(key(i)) Then
			Return Nothing ' Error
		End If
	Next

	Dim output As String = String.Empty
	Dim nonAlphaCharCount As Integer = 0

	For i As Integer = 0 To input.Length - 1
		If Char.IsLetter(input(i)) Then
			Dim cIsUpper As Boolean = Char.IsUpper(input(i))
			Dim offset As Integer = Convert.ToInt32(If(cIsUpper, "A"c, "a"c))
			Dim keyIndex As Integer = (i - nonAlphaCharCount) Mod key.Length
			Dim k As Integer = Convert.ToInt32(If(cIsUpper, Char.ToUpper(key(keyIndex)), Char.ToLower(key(keyIndex)))) - offset
			k = If(encipher, k, -k)
			Dim ch As Char = ChrW(([Mod](((Convert.ToInt32(input(i)) + k) - offset), 26)) + offset)
			output += ch
		Else
			output += input(i)
			nonAlphaCharCount += 1
		End If
	Next

	Return output
End Function

Public Shared Function Encipher(input As String, key As String) As String
	Return Cipher(input, key, True)
End Function

Public Shared Function Decipher(input As String, key As String) As String
	Return Cipher(input, key, False)
End Function
								


Example

									Dim text As String = "Hello, World!"
Dim cipherText As String = Encipher(text, "cipher")
Dim plainText As String = Decipher(cipherText, "cipher")
								


Output

									cipherText:	"Jmass, Nqzak!"
plainText:	"Hello, World!"