Assignment 1
Assignment 1
Problem to Solve:
Supposedly, Caesar (yes, that Caesar) used to “encrypt” (i.e., conceal in a reversible way) confidential messages
by shifting each letter therein by some number of places. For instance, he might write A as B, B as C, C as D, …,
and, wrapping around alphabetically, Z as A. And so, to say HELLO to someone, Caesar might write IFMMP
instead. Upon receiving such messages from Caesar, recipients would have to “decrypt” them by shifting letters
in the opposite direction by the same number of places.
The secrecy of this “cryptosystem” relied on only Caesar and the recipients knowing a secret, the number of
places by which Caesar had shifted his letters (e.g., 1). Not particularly secure by modern standards, but, hey, if
you’re perhaps the first in the world to do it, pretty secure!
Unencrypted text is generally called plaintext. Encrypted text is generally called ciphertext. And the secret used
is called a key.
To be clear, then, here’s how encrypting HELLO with a key of 1 yields IFMMP:
plaintext H E L L O
+ key 1 1 1 1 1
= ciphertext I F M M P
More formally, Caesar’s algorithm (i.e., cipher) encrypts messages by “rotating” each letter by k positions. More
formally, if p is some plaintext (i.e., an unencrypted message), pi is the ith character in p, and k is a secret key (i.e.,
a non-negative integer), then each letter, ci, in the ciphertext, c, is computed as
ci=(pi+k) % 26
wherein % 26 here means “remainder when dividing by 26.” This formula perhaps makes the cipher seem more
complicated than it is, but it’s really just a concise way of expressing the algorithm precisely. Indeed, for the sake
of discussion, think of A (or a) as 0, B (or b) as 1, …, H (or h) as 7, I (or i) as 8, …, and Z (or z) as 25. Suppose that
Caesar just wants to say Hi to someone confidentially using, this time, a key, k, of 3. And so his plaintext, p, is Hi,
in which case his plaintext’s first character, p0, is H (aka 7), and his plaintext’s second character, p1, is i (aka 8).
His ciphertext’s first character, c0, is thus K, and his ciphertext’s second character, c1, is thus L. Make sense?
Objective:
Implement a C++ program that encrypts a given plaintext using the Caesar cipher, a simple shift cipher where
each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. The program
will take input from the user and display the encrypted message.
Task Description:
Write a C++ program that:
1. Accepts an integer key from the user: The key represents the number of positions to shift each letter.
2. Accepts plaintext input from the user: This is the message that needs to be encrypted.
3. Encrypts the plaintext using the Caesar cipher.
4. Displays the resulting ciphertext.
Program Requirements:
1. User Input:
- The program should prompt the user to enter an integer key (a positive number).
- The program should prompt the user to enter a message (plaintext) to be encrypted.
2. Encryption Process:
- Each letter in the plaintext should be shifted forward by the key's value.
3. Output:
- Display the encrypted message (ciphertext) on the screen.
Example Execution:
Input:
Output:
Implementation Guidelines:
- Use C++ standard libraries such as:
- Ensure proper input validation for the key (it should be a positive integer).
Submission Guidelines:
- Submit a `.cpp` file containing your program code.