0% found this document useful (0 votes)
6 views

Assignment 1

The document outlines an assignment to implement a Caesar Cipher in C++. The program should encrypt a user-provided plaintext by shifting each letter by a specified integer key, while preserving the case of letters and leaving non-alphabetic characters unchanged. Additionally, the document includes requirements for user input, encryption process, output, implementation guidelines, and submission instructions.

Uploaded by

ali71803j
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Assignment 1

The document outlines an assignment to implement a Caesar Cipher in C++. The program should encrypt a user-provided plaintext by shifting each letter by a specified integer key, while preserving the case of letters and leaving non-alphabetic characters unchanged. Additionally, the document includes requirements for user input, encryption process, output, implementation guidelines, and submission instructions.

Uploaded by

ali71803j
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment No 1: Implementing the Caesar Cipher in C++

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.

- Uppercase letters should remain uppercase.

- Lowercase letters should remain lowercase.

- Non-alphabetic characters (such as spaces, digits, punctuation) should remain unchanged.

3. Output:
- Display the encrypted message (ciphertext) on the screen.

Example Execution:
Input:

Enter the key: 3


Enter the plaintext: Hello, World!

Output:

Ciphertext: Khoor, Zruog!

Implementation Guidelines:
- Use C++ standard libraries such as:

• - <iostream> for input/output operations.


• - <cctype> for character handling functions.
• - <string> for string manipulation.

- Use a loop to process each character of the string.

- Preserve the case of letters while shifting.

- Ensure proper input validation for the key (it should be a positive integer).

Submission Guidelines:
- Submit a `.cpp` file containing your program code.

- Ensure your code is well-commented, explaining key sections and logic.

- Include test cases in your submission to verify your program’s correctness.

- Your code should be properly formatted and easy to read.

Bonus Challenge (Optional):


Modify your program to allow decryption of the ciphertext by shifting letters backward using the same key.

You might also like