The Vigenere cipher is a method of encrypting or decrypting alphabetic text by using a series of interwoven Caesar ciphers (Caesar Shift), based on the letters of a keyword. In cryptography, Caesar shift is one of the simplest known encryption techniques. It employs a form of polyalphabetic substitution, which is using multiple substitution alphabets. The Enigma machine is one of the more complex examples polyalphabetic substitution cipher.

Similarly there are Base64 encoding and decoding schemes which are commonly used to encode binary data.

In this C++ source code, we show classes capable of encoding and decoding messages according to the Vigenere cipher. Both classes i.e. VigenereEncrypt and VigenereDecrypt inherit from the EncryptedFileWriter and EncryptedFileReader classes respectively. Both these classes override the encrypt function of EncryptedFileWriter and decrypt function of EncryptedFileReader to achieve the desired results.

Table of Contents

Both constructors of encrypt and decrypt classes accept two strings i.e. a file name and a code. The file name to initializes the superclass EncryptedFileWriter or EncryptedFileReader, while the code is stored.

Vigenere Encryption

The Vigenere cipher works by “shifting” the characters of the plain text based on the code. A code letter “A” will not change the text, while code letter “B” encrypts “A” as “B”, “B” as “C”, etc., “Y” as “Z”, and “Z” as “A”, shifting all letters by 1. Code letter “C” would shift by 2, and so forth, and code letter “Z” shifts by 25 (same as shifting 1 backwards). After using the first letter of the code to encrypt the first letter of the text, the cipher uses the second letter of the code for the second letter of the text, and so forth, starting over with the first letter of the code if the text is longer than the code.

For example, if we encrypt the text “HELLO” using the code “AB”, the result will be “HFLMO”, as we use the “A” to encode “H”, the first “L”, and “O” and “B” to encode “E” and the second “L”.

The encrypt() function returns the encrypted string, in all caps. Also, the non-letter characters in the input appears as unchanged in the output.

Vigenere Decryption

The decryption works in similar fashion but shifts characters in the opposite direction. For example, code letter “B” would decrypt “B” as “A”, “C” as “B”, etc., and “A” as “Z”. Put another way, “B” would shift the input letter by 25 (or 1 backwards), “C” would shift by 24 (2 backwards), etc., and “Z” shifts by 1 (25 backwards).

The decrypt() function returns the decrypted string, in all lowercase. As with encrypt, non-letter characters in the input appears as unchanged in the output.

Assumptions

We assume that for both C++ classes VigenereEncrypt and VigenereDecrypt, code word consists entirely of letters, but the text might contain non-letter characters. Both the code word and the text could contain capital or lowercase letters.

Some built in function of cctype library are useful to convert letter case and check for numeric characters. Some of them are: isalpha(), toupper(), and tolower(). The modulus operator (%) in C++ is very helpful for encryption and decryption. The modulus operator returns the remainder of a division of one number by another.

C++ Encryption Program

The program consists of a encryption-driver.cpp file that defines main() function. The encryption.cpp and .h files demonstrates EncryptedFileWriter and Reader classes to read and write encrypted text to file system. Finally vigenere.cpp and .h files show implementation of VigenereEncrypt and VigenereDecrypt classes.

To run the program:

encrypt [input file] [output file] [password]

encrypt [input file] [output file] [password]

The Example.txt input file uses “password” as the key to encrypt and output file should be same as EncryptedText.txt

Source Code

  Vigenere Encryption and Decryption in C++ (9.0 KiB, 3,742 hits)