Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ ONLY The Vigenere cipher is a type of polyalphabetic cipher. It is somewhat similar to caesar cipher i.e., each alphabet is substituted by a

C++ ONLY

The Vigenere cipher is a type of polyalphabetic cipher. It is somewhat similar to caesar cipher i.e., each alphabet is substituted by a new alphabet based on the shift. The difference being, in vigenere, the shift is calculated based on a letter from the keyword (explained below) rather than a fixed number. And, the letter from the keyword is chosen in a dynamic fashion based on the position of the input letter to be encrypted.

Lets look at the encryption algorithm through an example. There are two components to this type of encryption, keyword and the input text. If the keyword is TIGER and the input text we are trying to encode is BATMAN, encryption happens as follows:

The first letter B is encrypted using the rules:

The encrypted alphabet is a regular alphabet just shifted to start from T (first letter of the keyword). Therefore, the letter B becomes U on encryption.

The second letter A is encrypted using the rules:

Again, the encrypted alphabet is a regular alphabet just shifted to start from I (second letter of the keyword). Hence, the letter A becomes I on encryption.

The third letter T is encrypted using the rules:

The letter T becomes Z on encryption.

The fourth letter M is encrypted using the rules:

The letter M becomes Q on encryption.

The fifth letter A is encrypted using the rules:

The letter A becomes R on encryption.

The sixth letter N is encrypted using the rules:

The letter N becomes G on encryption.

We can summarize this process in three steps:

  1. For each input letter find the corresponding letter from the keyword. This is done by looking at the position of the letter in the input sequence, then, locating the letter from the keyword at the same position. Note: if the keyword is shorter than input text we need to loop back to the start. In the above example we see N from BATMAN follows the same rules as B. This is because TIGER is shorter than BATMAN.

  2. Create a new alphabet sequence shifted to start from the above computed letter.

  3. Find the encrypted letter for the input letter from the above created alphabet sequence.

  • Your function MUST be named vigenereCipher.

  • Your function takes three input arguments:

    • message - a string in all capital letters with spaces. Unencoded messages will be strings such as I LIKE CHOCOLATE or HELLO WORLD, and encoded messages will be strings such as B TOOV VPUGFEIZI or AMRPF PWXPU.

    • key - a string which is used to encrypt the message. It should be all capital letters. For e.g TIGER, CATS.

    • flag - a bool variable that controls whether your function will be encrypting or decrypting the given message. A value of true will mean your function is encrypting the given message, and a value of false will mean your function is decrypting it.

  • Your function should not print anything.

  • Your function should return the encrypted/decrypted message: a string value.

Examples:

  • If the input arguments are vigenereCipher(UNICORNS, TIGER, true), the function should return NVOGFKVY as the encrypted message.

  • If the input arguments are vigenereCipher(UNI CORNS, TIGER, true), the function should return NVO GFKVY as the encrypted message.

  • If the input arguments are vigenereCipher(NVOGFKVY, TIGER, false), the function should return UNICORNS as the encrypted message.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions