1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#include <iostream> #include <string> using namespace std; // Function to encrypt a message using Caesar cipher string encrypt(string plaintext, int shift) { string ciphertext = ""; for (char c : plaintext) { if (isalpha(c)) { // Check if the character is a letter char base = islower(c) ? 'a' : 'A'; char encryptedChar = (c - base + shift) % 26 + base; ciphertext += encryptedChar; } else { ciphertext += c; // Non-alphabetic characters remain unchanged } } return ciphertext; } // Function to decrypt a message using Caesar cipher string decrypt(string ciphertext, int shift) { return encrypt(ciphertext, 26 - shift); // Decryption is just encryption with the inverse shift } int main() { string text; int shift; // Input plaintext and shift value cout << "Enter the text: "; getline(cin, text); cout << "Enter shift value (1-25): "; cin >> shift; // Validate shift value if (shift < 1 || shift > 25) { cerr << "Error: Shift value must be between 1 and 25." << endl; return 1; } // Encrypt the text string encryptedText = encrypt(text, shift); cout << "Encrypted text: " << encryptedText << endl; // Decrypt the text string decryptedText = decrypt(encryptedText, shift); cout << "Decrypted text: " << decryptedText << endl; return 0; } |
Explanation
- Include Libraries:
#include <iostream>
: For input and output operations.#include <string>
: For usingstd::string
.
- Encrypt Function:
string encrypt(string plaintext, int shift)
Advertisementplaintext
: The input string to be encrypted.shift
: The number of positions each letter in the plaintext is shifted.- Iterates over each character in
plaintext
:- Checks if the character is a letter (
isalpha(c)
Advertisement - Determines if it is uppercase or lowercase to set the base (
'A'
or'a'
). - Applies the shift using modular arithmetic to ensure the result wraps around the alphabet.
- Appends the encrypted character to
ciphertext
. - Non-alphabetic characters are added to
ciphertext
unchanged.
- Checks if the character is a letter (
- Decrypt Function:
string decrypt(string ciphertext, int shift)
: Decrypts the ciphertext by using the inverse shift.- Calls the
encrypt
function with26 - shift
, as decryption in Caesar cipher is essentially shifting backwards by the same amount.
- Calls the
- Main Function (
main
):- Prompts the user to enter text and shift value.
- Validates that the shift value is between 1 and 25.
- Encrypts the input text using the
encrypt
Advertisement - Decrypts the encrypted text using the
decrypt
function and prints the result.
Notes:
- Shift Value: The Caesar cipher typically uses a shift between 1 and 25 to avoid trivial cases where the shift is 0 or 26, which would not change the text.
- Modular Arithmetic: Ensures that the shift wraps around the alphabet correctly.
- Non-Alphabetic Characters: Are not altered by the cipher and are included as-is in the output.