diff --git a/EncryptDecrypt.cpp b/EncryptDecrypt.cpp index a144bc09783ed2a78839c880567696f69b93d583..c07fde5793645345ef2b21817e3bbda64f1da91f 100644 --- a/EncryptDecrypt.cpp +++ b/EncryptDecrypt.cpp @@ -1,12 +1,13 @@ #include "EncryptDecrypt.h" -#include <iostream> -#include <fstream> +#include <iostream> // For input/output operations +#include <fstream> // For file reading/writing + // Caesar Cipher std::string caesarEncrypt(const std::string& text, int key) { std::string encrypted; for (char ch : text) { - encrypted += char(ch + key); + encrypted += char(ch + key); // Shift character by key positions } return encrypted; } @@ -14,16 +15,18 @@ std::string caesarEncrypt(const std::string& text, int key) { std::string caesarDecrypt(const std::string& text, int key) { std::string decrypted; for (char ch : text) { - decrypted += char(ch - key); + decrypted += char(ch - key); // Reverse the shift by key positions } return decrypted; } // XOR Cipher +// Encrypts/Decrypts a string using XOR cipher with the given single character key +// XOR is symmetric, so same function is used for both encryption and decryption std::string xorEncryptDecrypt(const std::string& text, char key) { std::string result; for (char ch : text) { - result += ch ^ key; + result += ch ^ key; // XOR each character with the key } return result; } @@ -32,6 +35,7 @@ std::string xorEncryptDecrypt(const std::string& text, char key) { std::string vigenereEncrypt(const std::string& text, const std::string& key) { std::string encrypted; for (size_t i = 0; i < text.size(); ++i) { + // Shift each character based on corresponding character in key encrypted += char(((text[i] + key[i % key.size()]) % 256)); } return encrypted; @@ -40,29 +44,33 @@ std::string vigenereEncrypt(const std::string& text, const std::string& key) { std::string vigenereDecrypt(const std::string& text, const std::string& key) { std::string decrypted; for (size_t i = 0; i < text.size(); ++i) { + // Reverse the shift to get the original character decrypted += char(((text[i] - key[i % key.size()] + 256) % 256)); } return decrypted; } // File operations + +// Reads the entire contents of a file and returns it as a string std::string readFile(const std::string& fileName) { - std::ifstream file(fileName); + std::ifstream file(fileName); // Open the file in read mode if (!file.is_open()) { std::cerr << "Error: Unable to open file for reading: " << fileName << "\n"; return ""; } + // Read file content into a string std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); file.close(); return content; } - +// Writes the given content string into a file void writeFile(const std::string& fileName, const std::string& content) { - std::ofstream file(fileName); + std::ofstream file(fileName); // Open the file in write mode if (!file.is_open()) { std::cerr << "Error: Unable to open file for writing: " << fileName << "\n"; return; } - file << content; - file.close(); + file << content; // Write the content to the file + file.close(); // Close the file }