(E) Encryption¶
Goal: I will learn to use both strings and characters in C++.
Instructions:
Create a new project: student/02/encryption/
.
Create a program that is able to encrypt text using the all-time favourite encryption from the primary school, slightly similar to the Caesar Cipher. The point of the encryption used in our program is a simple character replacement that is performed according to an encryption key given to the program. The main principle of the encryption will be explained further below.
Reading and checking the input¶
First, the program will use the prompt: "Enter the encryption key: "
to ask for the encryption key used for the text.
After that, it will read the given string and use it as the encryption key.
Next, the program will check whether the given string is a valid encryption key. The string will go through the following checks in the following order:
- The string needs to be 26 characters long.
If it is not, the program will print an error message:
"Error! The encryption key must contain 26 characters."
- The string may include only English lower-case letters, which means
the characters
'a'
-'z'
. If the string includes anything else, the program will print the error message:"Error! The encryption key must contain only lower case characters."
- The string must include all the English lower-case letters.
If it does not, the program will print the error message:
"Error! The encryption key must contain all alphabets a-z."
The program will exit with the return value EXIT_FAILURE immediately after printing an error message. This means that during one execution, the program can only print one of the error messages.
After the program has finished reading and checking the key, it will ask
the user for the word they want to encrypt, by using the prompt:
"Enter the text to be encrypted: "
,
and then reads one string as the text to be encrypted.
You need to check that the string to be encrypted includes only
English lower-case letters.
If the string contains anything else, it will print the error message:
"Error! The encryption key must contain only lower case characters."
and the execution of the program is finished with the return
value EXIT_FAILURE.
Encryption¶
The program will encrypt the letters with a simple replacement method, and
the replacing characters are marked in the encryption key.
The way the encryption key is read is that its first character replaces
the letter a
in the text that needs to be encrypted, its second one
replaces the letter b
and the last one, the 26th character, replaces the
letter z
.
For example, a text encrypted with the
key abcdefghijklmnopqrstuvwxyz will look identical to the
original, because the letter a
will be replaced with the letter a
,
and the letter b
with another letter b
, etc.
If you use the encryption key abcdefghijklmnopqrstuvwzyx,
it will change all the occurrences of the letter x
into the letter z
and occurrences of the letter z
into the letter x
, without
touching the rest of the letters.
(This encryption key has been used in the first example execution below.)
Examples¶
Examples of executing the program:
Enter the encryption key: abcdefghijklmnopqrstuvwzyx
Enter the text to be encrypted: thismessageisnotreallyencryptedtoomuch
Encrypted text: thismessageisnotreallyencryptedtoomuch
Enter the encryption key: qwertyuiopasdfghjklzxcvbnm
Enter the text to be encrypted: quickbrownfoxjumpsoverthelazydog
Encrypted text: jxoeawkgvfygbpxdhlgctkzitsqmnrgu
Enter the encryption key: aaa
Error! The encryption key must contain 26 characters.
Enter the encryption key: aabcdefghijklmnopqrstuvwzy
Error! The encryption key must contain all alphabets a-z.
Enter the encryption key: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Error! The encryption key must contain only lower case characters.
Tips for completing the assignment:
- It’s useful to go through the previous
short material concerning the data type
char
. Finding out the ASCII value of the letter will help in implementing this encryption. That way, you don’t have to save a string where the letters are in alphabetical order. - The earlier programs were so small that you might have written each of them with just one commit and push. There is a bit more to do in this exercise, so please remember to do it in several parts and to perform the intermediate commits to the version control (i.e. do the checks needed for the feeds and the encryption algorithm separately).
A+ presents the exercise submission form here.