/////////////////////////////////////////////////////////////////////////////// // This program takes a RSA encrypted message in a text file, // by given parameters, and decrypts it. After which it outputs to console. /////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <math.h> /////////////////////////////////////////////////////////////////////////////// void fileRead(std::ifstream &inFile, std::vector &fileBuff); void decrypt(std::vector<int> cipherText, std::vector<char> &plaintText, int pq, int d); // decrypts passed cipher text char toChar(int i); // change to char, a-z = 0 - 26 int residue(int number, int power, int modulo); int residualPowTwo(int number, int power, int modulo); int BaseTwoConv(int a, int b[]); /////////////////////////////////////////////////////////////////////////////// int main(void) { std::string filePath; std::ifstream inFile; int pq = 323, d = 107, e = 35; // provided decryption parameters std::vector<int> cipherText; // vector to hold encrypted message std::vector<char> plainText; // vector to hold decrypted message std::cout << "Enter filepath: "; std::cin >> filePath; inFile.open(filePath.c_str()); while(!(inFile.is_open())) { std::cout << "\nFile not found\nEnter filepath: "; std::cin >> filePath; inFile.open(filePath.c_str()); // cast to c string to open } fileRead(inFile, cipherText); // read from file into vector decrypt(cipherText, plainText, pq, d); // decrypt the ciphertext std::cout << "Decrypted Message: "; for(auto it : plainText) // iterate through and print decrypted message { std::cout << it; } std::cout << std::endl; // endline at end looks better return 0; } /////////////////////////////////////////////////////////////////////////////// void fileRead(std::ifstream &inFile, std::vector<int> &fileBuff) { std::string ln; // string to buffer input while(std::getline(inFile, ln)) // while reading by line { std::istringstream iss(ln); // use stringstream to parse int buff; // declare int for easy type casting w/ iss while(iss >> buff) // while stringstreaming and casting to int { fileBuff.push_back(buff); // pushback to vector } } return; } /////////////////////////////////////////////////////////////////////////////// void decrypt(std::vector<int> cipherText, std::vector<char> &plainText, int pq, int d) { int ctBuff; // ciphertext buff for(auto it : cipherText) // iterate through ciphertext ints { ctBuff = residue(it, d, pq); // calculate residue plainText.push_back(toChar(ctBuff)); // convert to char, push } return; } /////////////////////////////////////////////////////////////////////////////// char toChar(int i) { if(i > 0 && i < 27) // if it is an uppercase letter { return (char)(i + 64); } else if(i == 27) // if it is a space { return ' '; } else { std::cout << "\nErroneous output detected"; exit(1); } } /////////////////////////////////////////////////////////////////////////////// int residue(int number, int power, int modulo) { int b[100], digits, i; long long result; result = 1; digits = BaseTwoConv(power, b); for(i=0; i < digits; i++) { if(b[i] == 1) { result = result * residualPowTwo(number, pow(2,i), modulo); } } return(result % modulo); } /////////////////////////////////////////////////////////////////////////////// int residualPowTwo(int number, int power, int modulo) { int result = number % modulo; for (int i = 2; i <= power; i = (2*i)) { result = (result * result) % modulo; } return(result); } /////////////////////////////////////////////////////////////////////////////// int BaseTwoConv(int a, int b[]) { int i = 0; while (a>0) { b[i] = (a%2); a /= 2; i++; } return i; } ///////////////////////////////////////////////////////////////////////////////