CPSC170A
Fundamentals of Computer Science II

Lab 10

Strings

Vigenere Cipher

Create the C++ function encode_vigenere(std::string plaintext, std::string key) that encodes text using the vigenere cipher. Both parameters plaintext and key are strings of lowercase characters. The function should return a string of lowercase characters, which is equivalent to the plaintext with each character shifted according to the key.

The Vigenere cipher is similar to the Caesar cipher. Recall, in the Caesar cipher every character is shifted 3 characters to right in the alphabet. So, ‘a’ becomes ‘d’, ‘m’ becomes ‘p’, and ‘z’ becomes ‘b’. In the Vigenere cipher, letters may be shifted different amounts according to the chosen key. For each character in the plaintext, you use the corresponding letter in the key to determine how much to shift that character by.

Consider the plaintext “hello”, with a key of “hi”. This means to encrypt the “h” in hello, you use the letter “h” as the shift (Which is a shift of 7, since h is the 7th character of the alphabet). Then, for the letter “e” in hello, you would use the letter “i” as the shift (which is a shift of 8). But, now we have run out of letters in the key. In this case, you wrap back around to the beginning of the key string, using “h” as the key for the first “l” in hello.