Abstract
This article is about encrypting text using the Vigenère cipher. The encryption can be
considered as an extension of the famous Caesar cipher. The Vigenère encoding uses a keyword or keyphrase
for polyalphabetical substitution whereas the Ceasar cipher uses a simple shift, e.g., an offset of 5 letters.
Although the Vigenère encryption is simple, it can be considered undecipherable without knowledge of the keyword or -prase.
Wikipedia: Vigenère cipher
Wikipedia: Polyalphabetical cipher
Starting Position
A give text should be encrypted as easy and as safe as possible.
Solution
The solution is to shift (rotate) text within a given alphabet (e.g., UTF-8) while using a different shift
for each letter of the message derived from the given key.
Type some text into the input box, enter a keyword or phrase (at least 16 characters long; best is to use a
key that is as long as the message itself) and click on encrypt.
Respectively you can paste encrypted code into the input box and decrypt it to clear text with a given key phrase.
Keyphrase
Input
Output
Source Code
Protected Sub btnEncrypt_Click(sender As Object, e As EventArgs) Handles btnEncrypt.Click
Me.txtOutput.Text = VigenereCipher(Me.txtKey.Text, Me.txtInput.Text, False, 1000)
End Sub
Protected Sub btnDecrypt_Click(sender As Object, e As EventArgs) Handles btnDecrypt.Click
Me.txtOutput.Text = VigenereCipher(Me.txtKey.Text, Me.txtInput.Text, True, 1000)
End Sub
Private Function VigenereCipher(ByVal strKey As String, ByVal strInput As String, _
ByVal IsDecrypt As Boolean, _
Optional ByVal intSalt As Integer = 1) As String
Dim strChar As String = ""
Dim lngShift As Long = 0
Dim strSubst As String = ""
Dim strCipher As String = ""
Dim i As Long
Const ALPHABET As Long = 32768
For i = 1 To Len(strInput)
lngShift = CLng(AscW(Mid(strKey, (i - 1)Mod Len(strKey) + 1, 1))) * intSalt
strChar = Mid(strInput, i,1)
If IsDecrypt Then lngShift = lngShift* -1
If (AscW(strChar) + lngShift) ModALPHABET < 0 Then
strSubst= ChrW(ALPHABET+ ((AscW(strChar) + lngShift) ModALPHABET))
Else
strSubst= ChrW((AscW(strChar) + lngShift) Mod ALPHABET)
End If
strCipher = strCipher & strSubst
Next
Return strCipher
End Function
Sources
-
Vigenère cipher
-
Vigenère-Verschlüsselung
-
One-time pad
-
Zufallszahlengenerator