The Vigenère cipher (see en.wikipedia.org/wiki/Vigenere_cipher) is a 16th century variant of the Caesar cipher. For this challenge, you will write a program to decipher text using a keyword.
Before describing the Vigenère cipher, allow us to reframe the Caesar cipher, which you’ve already worked with. With the Caesar cipher, a plain text message is ciphered by shifting each letter ahead by three. The direction is reversed to decipher the resulting message.
Assign each English letter a numeric value, where A = 0, B = 1, all the way to Z = 25. With this in mind, a shift by 3 can be represented by the letter D (D = 3).
To decipher the text in table 11.1, start with the letter L and shift it by D. Because L = 11 and D = 3, the result of 11–3 is 8, or the letter I. Should you need to decipher the letter A, it should wrap around to become X, as you saw in lesson 9.
L | F | D | P | H | L | V | D | Z | L | F | R | Q | T | X | H | U | H | G |
D | D | D | D | D | D | D | D | D | D | D | D | D | D | D | D | D | D | D |
The Caesar cipher and ROT13 are susceptible to what’s called frequency analysis. Letters that occur frequently in the English language, such as E, will occur frequently in the ciphered text as well. By looking for patterns in the ciphered text, the code can be cracked.
To thwart would-be code crackers, the Vigenère cipher shifts each letter based on a repeating keyword, rather than a constant like 3 or 13. The keyword repeats until the end of the message, as shown for the keyword GOLANG in table 11.2.
C | S | O | I | T | E | U | I | W | U | I | Z | N | S | R | O | C | N | K | F | D |
G | O | L | A | N | G | G | O | L | A | N | G | G | O | L | A | N | G | G | O | L |
Now that you know what the Vigenère cipher is, you may notice that Vigenère with the keyword D is equivalent to the Caesar cipher. Likewise, ROT13 has a keyword of N (N = 13). Longer keywords are needed to be of any benefit.
Write a program to decipher the ciphered text shown in table 11.2. To keep it simple, all characters are uppercase English letters for both the text and keyword:
cipherText := "CSOITEUIWUIZNSROCNKFD" keyword := "GOLANG"
You can only perform operations on values of the same type, but you can convert one type to the other (string, byte, rune).
If you recall, modulus gives the remainder of dividing two numbers. For example, 27 % 26 is 1, keeping numbers within the 0–25 range. Be careful with negative numbers, though, as -3 % 26 is still -3.
After you complete the exercise, take a look at our solution in the appendix. How do they compare? Use the Go Playground’s Share button and post a link to your solution in the Get Programming with Go forum.
Ciphering text with Vigenère isn’t any more difficult than deciphering text. Just add letters of the keyword to letters of a plain text message instead of subtracting.
To send ciphered messages, write a program that ciphers plain text using a keyword:
plainText := "your message goes here" keyword := "GOLANG"
Bonus: rather than write your plain text message in uppercase letters with no spaces, use the strings.Replace and strings.ToUpper functions to remove spaces and uppercase the string before you cipher it.
Once you’ve ciphered a plain text message, check your work by deciphering the ciphered text with the same keyword.
Use the keyword "GOLANG" to cipher a message and post it to the forums for Get Programming with Go at forums.manning.com/forums/get-programming-with-go.
Disclaimer: Vigenère cipher is all in good fun, but don’t use it for important secrets. There are more secure ways to send messages in the 21st century.