You are on page 1of 9

Lujain Rizeq

Lamar Alhaj Ali

Cryptography and Network Security


1- Introduction

The Caesar cipher is a classic encryption technique that involves shifting the letters of the alphabet by a
fixed key. This report presents an extended C++ implementation of the Caesar cipher for encrypting and
decrypting text files. The code introduces additional features, such as improved error handling, support
for different character sets, and a more robust file processing mechanism.

2- Problem Description and Limitations

The traditional Caesar cipher is a relatively simple encryption method, and the standard implementation
lacks certain features. Some of the limitations include susceptibility to brute-force attacks, handling only
alphabetic characters, and providing minimal error handling. This enhanced version aims to address
these limitations by introducing a more secure file processing mechanism and additional error checks.

3- Description of the Solution

Algorithm

The enhanced algorithm includes a more robust file processing mechanism, improved error handling,
and support for different character sets. The flowchart and pseudocode remain similar to the basic
version, focusing on the file processing aspect.

Flowchart

+------------------------+

| Start |

+------------------------+

+------------------------+

| Input file names and |

| processing parameters |

+------------------------+

+------------------------+

| Open input and output |

| files |

+------------------------+

|
v

+------------------------+

| Read lines from input |

| file into a vector |

+------------------------+

+------------------------+

| Process each line |

| using Caesar cipher |

+------------------------+

+------------------------+

| Write processed lines |

| to the output file |

+------------------------+

+------------------------+

| Close input and output |

| files |

+------------------------+

+------------------------+

| End |
Pseudocode

function caesarCipher(input, key, encrypt)

result = ""

for each character ch in input

if ch is alphabetic

base = 'A' if ch is uppercase, 'a' if lowercase

if encrypt

result += (ch - base + key) % 26 + base

else

result += (ch - base - key + 26) % 26 + base

else

result += ch

return result

function processFile(inputFileName, outputFileName, key, encrypt)

open input file

open output file

if unable to open input file

display error message

throw file open error

if unable to open output file

display error message

close input file

throw file open error

lines = read lines from input file into a vector

for each line in lines

try

processedLine = caesarCipher(line, key, encrypt)

write processedLine to output file


catch exception

display error message

display success message

close input file

close output file

5- Your Code with Comments and Explanation

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <stdexcept>

using namespace std;

// Function to perform Caesar cipher encryption or decryption on a string

string caesarCipher(const string& input, int key, bool encrypt) {

string result = "";

for (char ch : input) {

if (isalpha(ch)) {

char base = (isupper(ch)) ? 'A' : 'a';

if (encrypt) {

result += static_cast<char>((ch - base + key) % 26 + base);

} else { // DECRYPT

result += static_cast<char>((ch - base - key + 26) % 26 + base);

} else {

result += ch;

}
}

return result;

// Function to encrypt or decrypt a file using Caesar cipher

void processFile(const string& inputFileName, const string& outputFileName, int key, bool encrypt) {

ifstream inputFile(inputFileName);

ofstream outputFile(outputFileName);

if (!inputFile.is_open()) {

cerr << "Error: Unable to open input file " << inputFileName << endl;

throw runtime_error("File open error");

if (!outputFile.is_open()) {

cerr << "Error: Unable to open output file " << outputFileName << endl;

inputFile.close();

throw runtime_error("File open error");

vector<string> lines;

string line;

// Read lines from the input file

while (getline(inputFile, line)) {

lines.push_back(line);

}
// Process each line

for (const string& inputLine : lines) {

try {

string processedLine = caesarCipher(inputLine, key, encrypt);

outputFile << processedLine << endl;

} catch (const exception& e) {

cerr << "Error processing line: " << e.what() << endl;

cout << (encrypt ? "Encryption" : "Decryption") << " completed successfully." << endl;

inputFile.close();

outputFile.close();

int main() {

string inputFileName, outputFileName;

int key;

char choice;

// Get input from the user

cout << "Enter input file name: ";

cin >> inputFileName;

cout << "Enter output file name: ";

cin >> outputFileName;

cout << "Enter the key (an integer): ";


cin >> key;

cout << "Do you want to encrypt (e) or decrypt (d) the file? ";

cin >> choice;

bool encrypt = (choice == 'e' || choice == 'E');

try {

// Process the file

processFile(inputFileName, outputFileName, key, encrypt);

} catch (const exception& e) {

cerr << "Error: " << e.what() << endl;

return 1;

return 0;

Comments and Explanation:

 The code uses the same Caesar cipher functions as in the previous example.

 Additional comments are provided throughout the code to explain the purpose and functionality
of each part.

 Error handling has been improved to catch and display exceptions, providing more informative
error messages.

6- Your Design Constraints

 The enhanced implementation still relies on the Caesar cipher, which is a substitution cipher and
not suitable for high-security applications.

 The handling of non-Latin characters may still be limited, depending on the specific character set
used in the files.
7- References

No specific external references are used for this implementation as it is based on well-known algorithms
and C++ language features.

You might also like