Advertisement
Guest User

Untitled

a guest
May 20th, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.52 KB | None | 0 0
  1. /**
  2. =========================================
  3. Author         :  Eyad Hossam Al-Den,   |  Sarah Abou-Senna
  4. e-mail         :  eyadhossamm@gmail.com |  sara_usa_1999@hotmail.com
  5. ID             :  20176007              |  20175006
  6. program        :  Software Engineering
  7. task#          :  Assignment 4
  8.  
  9. preferable/necessary to compile in C++11 or later.
  10. =========================================
  11. */
  12.  
  13. #include <iostream>
  14. #include <string>                                                           //added for compatibility for anything older than C++11
  15. #include <sstream>
  16. #include <limits>
  17. #include <fstream>
  18. #include <vector>
  19.  
  20. using namespace std;
  21.  
  22. struct COURSE
  23. {
  24.     string courseName;
  25.     int grade;
  26. };
  27.  
  28. struct Student
  29. {
  30.     string name;
  31.     int id;
  32.     string email;
  33.     COURSE courses[3];
  34. };
  35.  
  36. vector<Student> students;
  37. ifstream file;
  38. void ReadFile();
  39.  
  40. /**==============BASIC SHIFTING ENCRYPTION/DECRYPTION FUNCTIONS==============*/
  41. //Shifting Encryption
  42. string shiftEncryption(string input, int shifts)
  43. {
  44.     for(int i=0; i<input.length(); i++) {
  45.         if (input[i] != ' ')
  46.             input[i] = static_cast<char>(input[i] + shifts);                    //changes the i-th character to its (i+shifts)-th character.
  47.     }
  48.     return input;
  49. }
  50. //Shifting Decryption
  51. string shiftDecryption(string input, int shifts)
  52. {
  53.     for(int i=0; i<input.length(); i++) {
  54.         if (input[i] != ' ')
  55.             input[i] = static_cast<char>(input[i] - shifts);                    //changes the i-th character to its (i-shifts)-th character.
  56.     }
  57.     return input;
  58. }
  59. /**==============END OF SHIFTING ENCRYPTION/DECRYPTION==============*/
  60.  
  61.  
  62. /**==============BONUS ENCRYPTION/DECRYPTION TECHNIQUES==============*/
  63. //Transposition Encryption/Decryption, the same function does both tasks because it's essentially the same exact thing.
  64. string transposition(string input)
  65. {
  66.     string output = ""; int flag = 0;
  67.     for(int i=0; i<=input.length(); i++)
  68.     {
  69.         string temp = "";
  70.         if(!isalpha(input[i]))
  71.         {
  72.             temp = input.substr(flag, i-flag);
  73.             flag = i+1;
  74.             for(int j=0; j<temp.length(); j++)
  75.             {
  76.                 output += temp[temp.length() - j - 1];
  77.             }
  78.             output += input[i];
  79.         }
  80.     }
  81.     return output;
  82. }
  83.  
  84.  
  85. /**==============RAIL FENCE CIPHER==============*/
  86. //Rail Fence Encryption technique
  87. string railFenceEncryption(string input, int depth)
  88. {
  89.     string encryptedOutput = ""; bool moveDown = false;
  90.     int row = 0, column = 0;
  91.     char rail[depth][input.length()];                                       //initialize the depth*len(input)
  92.  
  93.     for(int i=0; i<depth; i++)
  94.         for(int j=0; j<input.length(); j++)
  95.             rail[i][j] = '#';                                               //assign '#' to each of the depth*len(input) matrix's elements, basically the "fence".
  96.  
  97.     for(int i=0; i<input.length(); i++)
  98.     {
  99.         if(row == 0 || row == depth - 1)                                    //check if you are at either the topmost or bottommost row
  100.             moveDown = !moveDown;                                           //go in the other direction if you are at either of those two
  101.  
  102.         rail[row][column++] = input[i];                                     //move and replace elements along the diagonal with input[i]
  103.  
  104.         if(moveDown) row++;                                                 //if you are moving down, increase rows, if not, decrease rows (moving up)
  105.         else row--;
  106.     }
  107.  
  108.     for(int i=0; i<depth; i++)
  109.         for(int j=0; j<input.length(); j++)
  110.             if(rail[i][j] != '#')                                           //basically checks if you are on a diagonal or not
  111.                 encryptedOutput += rail[i][j];                              //if you are, append the character of the element you are at to the output string
  112.  
  113.     return encryptedOutput;
  114. }
  115. //Rail Fence Decryption technique
  116. string railFenceDecryption(string input, const int depth)                         //most code is repeated from the encryption function, code is somewhat self-explanatory.
  117. {
  118.     string decryptedOutput = ""; bool moveDown = false;
  119.     int row = 0, column = 0, pos = 0;
  120.     char rail[depth][input.length()];
  121.  
  122.     for(int i=0; i<depth; i++)
  123.         for(int j=0; j<input.length(); j++)
  124.             rail[i][j] = '#';                                               //same as before, construct the 'fence'
  125.  
  126.     for(int i=0; i<input.length(); i++)
  127.     {
  128.         if(row == depth - 1) moveDown = false;
  129.         else if(row == 0) moveDown = true;
  130.  
  131.         rail[row][column++] = '.';                                          //mark indexes with a period '.'
  132.  
  133.         if(moveDown) row++;
  134.         else row--;
  135.     }
  136.  
  137.  
  138.     for(int i=0; i<depth; i++)
  139.         for(int j=0; j<input.length(); j++)
  140.             if(rail[i][j] == '.' && pos<input.length())                     //replace rail[i][j] with input[pos++] if (i,j) are on a rail
  141.                 rail[i][j] = input[pos++];
  142.  
  143.     row = 0, column = 0;
  144.     for(int i=0; i<input.length(); i++)
  145.     {
  146.         if(row == depth - 1) moveDown = false;
  147.         else if(row == 0) moveDown = true;
  148.  
  149.         if(rail[row][column] != '.')
  150.             decryptedOutput += rail[row][column++];
  151.  
  152.         if(moveDown) row++;
  153.         else row--;
  154.     }
  155.  
  156.     return decryptedOutput;
  157. }
  158. /**==============END OF RAIL-FENCE CIPHER==============*/
  159. /**===================MENU FUNCTIONS===================*/
  160. void showEncryptionMethods()
  161. {
  162.     cout << "Input file location (e.g. : D:\\example.txt):" << endl;
  163.     string fileLocation = "";
  164.     cin >> fileLocation;
  165.     file.open(fileLocation);
  166.     if(file.fail())
  167.     {
  168.         cerr << "Opening file failed. Check entered path and try again." << endl;
  169.         return;
  170.     }else ReadFile();
  171.  
  172.     ofstream outFile;
  173.     outFile.open("EncryptedFile.txt");
  174.  
  175.     string encryptionMethod = "", input = ""; int key = 0;
  176.     cout << "Choose a method of encryption to encrypt this string with [shifting/transposition/rail-fence]: " << endl;
  177.     cin >> encryptionMethod;
  178.     if(encryptionMethod == "shifting")
  179.     {
  180.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  181.         cout << "Enter number of shifts: " << endl;
  182.         cin >> key;
  183.         for(int i = 0; i < students.size(); i++)
  184.         {
  185.             outFile << shiftEncryption(students[i].name, key) << endl;
  186.             outFile << students[i].id << endl;
  187.             outFile << shiftEncryption(students[i].email, key) << endl;
  188.             for(int j = 0; j < 3; j++)
  189.                 outFile << shiftEncryption(students[i].courses[j].courseName, key) << ' ' << students[i].courses[j].grade << endl;
  190.         }
  191.         outFile.close();
  192.     }
  193.     else if(encryptionMethod == "transposition")
  194.     {
  195.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  196.         for(int i = 0; i < students.size(); i++)
  197.         {
  198.             outFile << transposition(students[i].name) << endl;
  199.             outFile << students[i].id << endl;
  200.             outFile << transposition(students[i].email) << endl;
  201.             for(int j = 0; j < 3; j++)
  202.                 outFile << transposition(students[i].courses[j].courseName) << students[i].courses[j].grade << endl;
  203.         }
  204.     }
  205.     else if(encryptionMethod == "rail-fence")
  206.     {
  207.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  208.         cout << "Enter depth of fence: " << endl;
  209.         cin >> key;
  210.         for(int i = 0; i < students.size(); i++)
  211.         {
  212.             outFile << railFenceEncryption(students[i].name, key) << endl;
  213.             outFile << students[i].id << endl;
  214.             outFile << railFenceEncryption(students[i].email, key) << endl;
  215.             for(int j = 0; j < 3; j++)
  216.                 outFile << railFenceEncryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
  217.         }
  218.     }
  219.     else
  220.     {
  221.         cout << "Invalid input, try again." << endl;
  222.     }
  223.     outFile.close();
  224. }
  225. void showDecryptionMethods()
  226. {
  227.     cout << "Input file location (e.g. : D:\\example.txt):" << endl;
  228.     string fileLocation = "";
  229.     cin >> fileLocation;
  230.     file.open(fileLocation);
  231.     if(file.fail())
  232.     {
  233.         cerr << "Opening file failed. Check entered path and try again." << endl;
  234.         return;
  235.     }else ReadFile();
  236.  
  237.     ofstream outFile;
  238.     outFile.open("DecryptedFile.txt");
  239.  
  240.     string decryptionMethod = "", input = ""; int key = 0;
  241.     cout << "Choose a method of decryption to decrypt this string with [shifting/transposition/rail-fence]: " << endl;
  242.     cin >> decryptionMethod;
  243.     if(decryptionMethod == "shifting")
  244.     {
  245.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  246.         cout << "Enter number of shifts: " << endl;
  247.         cin >> key;
  248.         for(int i = 0; i < students.size(); i++)
  249.         {
  250.             outFile << shiftDecryption(students[i].name, key) << endl;
  251.             outFile << students[i].id << endl;
  252.             outFile << shiftDecryption(students[i].email, key) << endl;
  253.             for(int j = 0; j < 3; j++)
  254.                 outFile << shiftDecryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
  255.         }
  256.     }
  257.     else if(decryptionMethod == "transposition")
  258.     {
  259.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  260.         for(int i = 0; i < students.size(); i++)
  261.         {
  262.             outFile << transposition(students[i].name) << endl;
  263.             outFile << students[i].id << endl;
  264.             outFile << transposition(students[i].email) << endl;
  265.             for(int j = 0; j < 3; j++)
  266.                 outFile << transposition(students[i].courses[j].courseName) << students[i].courses[j].grade << endl;
  267.         }
  268.     }
  269.     else if(decryptionMethod == "rail-fence")
  270.     {
  271.         cin.ignore(numeric_limits<streamsize>::max(), '\n' );
  272.         cout << "Enter depth of fence: " << endl;
  273.         cin >> key;
  274.         for(int i = 0; i < students.size(); i++)
  275.         {
  276.             outFile << railFenceDecryption(students[i].name, key) << endl;
  277.             outFile << students[i].id << endl;
  278.             outFile << railFenceDecryption(students[i].email, key) << endl;
  279.             for(int j = 0; j < 3; j++)
  280.                 outFile << railFenceDecryption(students[i].courses[j].courseName, key) << students[i].courses[j].grade << endl;
  281.         }
  282.     }
  283.     else
  284.     {
  285.         cout << "Invalid input, try again." << endl;
  286.     }
  287.     outFile.close();
  288. }
  289.  
  290. /**==============END OF MENU FUNCTIONS==============*/
  291. int main()
  292. {
  293.     string command = "";
  294.     while(true)
  295.     {
  296.         cout << "choose (write) a function to perform [encrypt/decrypt/exit]: " << endl;
  297.         cin >> command;
  298.         for(int i=0; i<command.length(); i++)
  299.             command[i] = tolower(command[i]);
  300.         if(command == "exit") break;
  301.         else if(command == "encrypt") showEncryptionMethods();
  302.         else if(command == "decrypt") showDecryptionMethods();
  303.         else cout << "invalid input, try again." << endl;
  304.  
  305.         cout << endl;
  306.     }
  307.     return 0;
  308. }
  309.  
  310. void ReadFile()
  311. {
  312.     string currStr = "";
  313.     while(file >> currStr)
  314.     {
  315.         string name = currStr;
  316.         file >> currStr;
  317.         name += " " + currStr;
  318.         Student tempStudent;
  319.         tempStudent.name = name;
  320.         file >> tempStudent.id >> tempStudent.email;
  321.         for (int i = 0; i < 3; i++)
  322.         {
  323.             file >> currStr;
  324.             tempStudent.courses[i].courseName = currStr;
  325.             file >> tempStudent.courses[i].grade;
  326.         }
  327.         students.push_back(tempStudent);
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement