Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- =========================================
- Author : Eyad Hossam Al-Den, | Sarah Abou-Senna
- e-mail : eyadhossamm@gmail.com | sara_usa_1999@hotmail.com
- ID : 20176007 | 20175006
- program : Software Engineering
- task# : Assignment 4
- preferable/necessary to compile in C++11 or later.
- =========================================
- */
- #include <iostream>
- #include <string> //added for compatibility for anything older than C++11
- #include <sstream>
- #include <limits>
- #include <fstream>
- #include <vector>
- using namespace std;
- struct COURSE
- {
- string courseName;
- int grade;
- };
- struct Student
- {
- string name;
- int id;
- string email;
- COURSE courses[3];
- };
- vector<Student> students;
- ifstream file;
- void ReadFile();
- /**==============BASIC SHIFTING ENCRYPTION/DECRYPTION FUNCTIONS==============*/
- //Shifting Encryption
- string shiftEncryption(string input, int shifts)
- {
- for(int i=0; i<input.length(); i++) {
- if (input[i] != ' ')
- input[i] = static_cast<char>(input[i] + shifts); //changes the i-th character to its (i+shifts)-th character.
- }
- return input;
- }
- //Shifting Decryption
- string shiftDecryption(string input, int shifts)
- {
- for(int i=0; i<input.length(); i++) {
- if (input[i] != ' ')
- input[i] = static_cast<char>(input[i] - shifts); //changes the i-th character to its (i-shifts)-th character.
- }
- return input;
- }
- /**==============END OF SHIFTING ENCRYPTION/DECRYPTION==============*/
- /**==============BONUS ENCRYPTION/DECRYPTION TECHNIQUES==============*/
- //Transposition Encryption/Decryption, the same function does both tasks because it's essentially the same exact thing.
- string transposition(string input)
- {
- string output = ""; int flag = 0;
- for(int i=0; i<=input.length(); i++)
- {
- string temp = "";
- if(!isalpha(input[i]))
- {
- temp = input.substr(flag, i-flag);
- flag = i+1;
- for(int j=0; j<temp.length(); j++)
- {
- output += temp[temp.length() - j - 1];
- }
- output += input[i];
- }
- }
- return output;
- }
- /**==============RAIL FENCE CIPHER==============*/
- //Rail Fence Encryption technique
- string railFenceEncryption(string input, int depth)
- {
- string encryptedOutput = ""; bool moveDown = false;
- int row = 0, column = 0;
- char rail[depth][input.length()]; //initialize the depth*len(input)
- for(int i=0; i<depth; i++)
- for(int j=0; j<input.length(); j++)
- rail[i][j] = '#'; //assign '#' to each of the depth*len(input) matrix's elements, basically the "fence".
- for(int i=0; i<input.length(); i++)
- {
- if(row == 0 || row == depth - 1) //check if you are at either the topmost or bottommost row
- moveDown = !moveDown; //go in the other direction if you are at either of those two
- rail[row][column++] = input[i]; //move and replace elements along the diagonal with input[i]
- if(moveDown) row++; //if you are moving down, increase rows, if not, decrease rows (moving up)
- else row--;
- }
- for(int i=0; i<depth; i++)
- for(int j=0; j<input.length(); j++)
- if(rail[i][j] != '#') //basically checks if you are on a diagonal or not
- encryptedOutput += rail[i][j]; //if you are, append the character of the element you are at to the output string
- return encryptedOutput;
- }
- //Rail Fence Decryption technique
- string railFenceDecryption(string input, const int depth) //most code is repeated from the encryption function, code is somewhat self-explanatory.
- {
- string decryptedOutput = ""; bool moveDown = false;
- int row = 0, column = 0, pos = 0;
- char rail[depth][input.length()];
- for(int i=0; i<depth; i++)
- for(int j=0; j<input.length(); j++)
- rail[i][j] = '#'; //same as before, construct the 'fence'
- for(int i=0; i<input.length(); i++)
- {
- if(row == depth - 1) moveDown = false;
- else if(row == 0) moveDown = true;
- rail[row][column++] = '.'; //mark indexes with a period '.'
- if(moveDown) row++;
- else row--;
- }
- for(int i=0; i<depth; i++)
- for(int j=0; j<input.length(); j++)
- if(rail[i][j] == '.' && pos<input.length()) //replace rail[i][j] with input[pos++] if (i,j) are on a rail
- rail[i][j] = input[pos++];
- row = 0, column = 0;
- for(int i=0; i<input.length(); i++)
- {
- if(row == depth - 1) moveDown = false;
- else if(row == 0) moveDown = true;
- if(rail[row][column] != '.')
- decryptedOutput += rail[row][column++];
- if(moveDown) row++;
- else row--;
- }
- return decryptedOutput;
- }
- /**==============END OF RAIL-FENCE CIPHER==============*/
- /**===================MENU FUNCTIONS===================*/
- void showEncryptionMethods()
- {
- cout << "Input file location (e.g. : D:\\example.txt):" << endl;
- string fileLocation = "";
- cin >> fileLocation;
- file.open(fileLocation);
- if(file.fail())
- {
- cerr << "Opening file failed. Check entered path and try again." << endl;
- return;
- }else ReadFile();
- ofstream outFile;
- outFile.open("EncryptedFile.txt");
- string encryptionMethod = "", input = ""; int key = 0;
- cout << "Choose a method of encryption to encrypt this string with [shifting/transposition/rail-fence]: " << endl;
- cin >> encryptionMethod;
- if(encryptionMethod == "shifting")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- cout << "Enter number of shifts: " << endl;
- cin >> key;
- for(int i = 0; i < students.size(); i++)
- {
- outFile << shiftEncryption(students[i].name, key) << endl;
- outFile << students[i].id << endl;
- outFile << shiftEncryption(students[i].email, key) << endl;
- for(int j = 0; j < 3; j++)
- outFile << shiftEncryption(students[i].courses[j].courseName, key) << ' ' << students[i].courses[j].grade << endl;
- }
- outFile.close();
- }
- else if(encryptionMethod == "transposition")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- for(int i = 0; i < students.size(); i++)
- {
- outFile << transposition(students[i].name) << endl;
- outFile << students[i].id << endl;
- outFile << transposition(students[i].email) << endl;
- for(int j = 0; j < 3; j++)
- outFile << transposition(students[i].courses[j].courseName) << students[i].courses[j].grade << endl;
- }
- }
- else if(encryptionMethod == "rail-fence")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- cout << "Enter depth of fence: " << endl;
- cin >> key;
- for(int i = 0; i < students.size(); i++)
- {
- outFile << railFenceEncryption(students[i].name, key) << endl;
- outFile << students[i].id << endl;
- outFile << railFenceEncryption(students[i].email, key) << endl;
- for(int j = 0; j < 3; j++)
- outFile << railFenceEncryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
- }
- }
- else
- {
- cout << "Invalid input, try again." << endl;
- }
- outFile.close();
- }
- void showDecryptionMethods()
- {
- cout << "Input file location (e.g. : D:\\example.txt):" << endl;
- string fileLocation = "";
- cin >> fileLocation;
- file.open(fileLocation);
- if(file.fail())
- {
- cerr << "Opening file failed. Check entered path and try again." << endl;
- return;
- }else ReadFile();
- ofstream outFile;
- outFile.open("DecryptedFile.txt");
- string decryptionMethod = "", input = ""; int key = 0;
- cout << "Choose a method of decryption to decrypt this string with [shifting/transposition/rail-fence]: " << endl;
- cin >> decryptionMethod;
- if(decryptionMethod == "shifting")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- cout << "Enter number of shifts: " << endl;
- cin >> key;
- for(int i = 0; i < students.size(); i++)
- {
- outFile << shiftDecryption(students[i].name, key) << endl;
- outFile << students[i].id << endl;
- outFile << shiftDecryption(students[i].email, key) << endl;
- for(int j = 0; j < 3; j++)
- outFile << shiftDecryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
- }
- }
- else if(decryptionMethod == "transposition")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- for(int i = 0; i < students.size(); i++)
- {
- outFile << transposition(students[i].name) << endl;
- outFile << students[i].id << endl;
- outFile << transposition(students[i].email) << endl;
- for(int j = 0; j < 3; j++)
- outFile << transposition(students[i].courses[j].courseName) << students[i].courses[j].grade << endl;
- }
- }
- else if(decryptionMethod == "rail-fence")
- {
- cin.ignore(numeric_limits<streamsize>::max(), '\n' );
- cout << "Enter depth of fence: " << endl;
- cin >> key;
- for(int i = 0; i < students.size(); i++)
- {
- outFile << railFenceDecryption(students[i].name, key) << endl;
- outFile << students[i].id << endl;
- outFile << railFenceDecryption(students[i].email, key) << endl;
- for(int j = 0; j < 3; j++)
- outFile << railFenceDecryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
- }
- }
- else
- {
- cout << "Invalid input, try again." << endl;
- }
- outFile.close();
- }
- /**==============END OF MENU FUNCTIONS==============*/
- int main()
- {
- string command = "";
- while(true)
- {
- cout << "choose (write) a function to perform [encrypt/decrypt/exit]: " << endl;
- cin >> command;
- for(int i=0; i<command.length(); i++)
- command[i] = tolower(command[i]);
- if(command == "exit") break;
- else if(command == "encrypt") showEncryptionMethods();
- else if(command == "decrypt") showDecryptionMethods();
- else cout << "invalid input, try again." << endl;
- cout << endl;
- }
- return 0;
- }
- void ReadFile()
- {
- string currStr = "";
- while(file >> currStr)
- {
- string name = currStr;
- file >> currStr;
- name += " " + currStr;
- Student tempStudent;
- tempStudent.name = name;
- file >> tempStudent.id >> tempStudent.email;
- for (int i = 0; i < 3; i++)
- {
- file >> currStr;
- tempStudent.courses[i].courseName = currStr;
- file >> tempStudent.courses[i].grade;
- }
- students.push_back(tempStudent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement