0% found this document useful (0 votes)
16 views6 pages

Caeser Cipher

The document presents an assignment on the Caesar Cipher, submitted by Noor Fatima to Dr. Qaiser Javaid. It includes a flow chart and C++ code for encrypting and decrypting text and digits using a specified numeric key. The program prompts the user for input and allows them to choose between encryption and decryption operations.

Uploaded by

fatimaraja1258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Caeser Cipher

The document presents an assignment on the Caesar Cipher, submitted by Noor Fatima to Dr. Qaiser Javaid. It includes a flow chart and C++ code for encrypting and decrypting text and digits using a specified numeric key. The program prompts the user for input and allows them to choose between encryption and decryption operations.

Uploaded by

fatimaraja1258
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Submitted by: Noor Fatima

Submitted to: Dr Qaiser Javaid


Reg No. 4678-FOC/BSCS/F22
Course: Information Security

Assignment # 2
Caesar Cipher

1
Flow Chart:

Code:
#include <iostream>
#include <string>

using namespace std;

2
// Function to encrypt text (including digits)
string encrypt(string text, int key) {
string result = "";
for (char c : text) {
if (isalpha(c)) { // Encrypt letters (A-Z, a-z)
char base = isupper(c) ? 'A' : 'a';
result += char((c - base + key) % 26 + base);
}
else if (isdigit(c)) { // Encrypt digits (0-9)
result += char((c - '0' + key) % 10 + '0');
}
else { // Keep special characters unchanged
result += c;
}
}
return result;
}

// Function to decrypt text (including digits)


string decrypt(string text, int key) {
string result = "";
for (char c : text) {
if (isalpha(c)) { // Decrypt letters (A-Z, a-z)
char base = isupper(c) ? 'A' : 'a';
result += char((c - base - key + 26) % 26 + base);
}
else if (isdigit(c)) { // Decrypt digits (0-9)

3
result += char((c - '0' - key + 10) % 10 + '0');
}
else { // Keep special characters unchanged
result += c;
}
}
return result;
}

int main() {
string text;
int key;
char choice;

cout << "\n=====================================\n";


cout << " Caesar Cipher Program \n";
cout << "=====================================\n";

cin.ignore();
cout << "\nEnter a string: ";
getline(cin, text);

cout << "Enter a numeric key: ";


cin >> key;

cout << "\nChoose an operation: \n";

4
cout << " [E] Encrypt Text \n";
cout << " [D] Decrypt Text \n";
cout << "Your choice: ";
cin >> choice;

cout << "\n=====================================\n";

choice = toupper(choice);

if (choice == 'E') {
cout << "Encrypted Text: " << encrypt(text, key) << "\n";
}
else if (choice == 'D') {
cout << "Decrypted Text: " << decrypt(text, key) << "\n";
}
else {
cout << "Invalid choice! Please enter E or D.\n";
}

cout << "=====================================\n";

return 0;
}

Output:

5
6

You might also like