Advertisement
ONEMUNEEB

Untitled

Oct 22nd, 2021
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     int e;
  9.     int n;
  10.     cin >> e;
  11.     cin >> n;
  12.  
  13.     //array of the 25 primes between 1 and 100
  14.     int primes[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
  15.  
  16.     //Find P & Q
  17.     int p = 0;
  18.     int q = 0;
  19.     for (int i = 0; i < 25; i++) {
  20.         if (n % primes[i] == 0) {
  21.             if (p == 0) {
  22.                 p = primes[i];
  23.             }
  24.             else {
  25.                 q = primes[i];
  26.             }
  27.         }
  28.     }
  29.  
  30.     //if input was invalid, the program will quit.
  31.     if (p == 0 || q == 0) {
  32.         cout << "Public key not valid!";
  33.         exit(0);
  34.     }
  35.  
  36.     //Insert file with encoded message
  37.     string fileName;
  38.     cin >> fileName;
  39.     ifstream encryptedFile (fileName);
  40.     int encryptedInput;
  41.     vector<int> encryptedNums;
  42.     while (encryptedFile >> encryptedInput) {
  43.         encryptedNums.push_back(encryptedInput);
  44.     }
  45.  
  46.     //finds phi(n) totient value
  47.     int totient;
  48.     totient = (p - 1) * (q - 1);
  49.  
  50.     //finds d = (inverse of e) mod phi(n)
  51.     int d = 0;
  52.     for (int i = 0; i < totient; i++) {
  53.         if (((e % totient) * (i % totient)) % totient == 1) {
  54.             d = i;
  55.         }
  56.     }
  57.     //outputting all found values
  58.     cout << p << " " << q << " " << totient << " " << d << "\n";
  59.  
  60.     //decryption step
  61.     vector<int> decryptedNums;
  62.     for (int i = 0; i < 13; i++) {
  63.         int tempd = d;
  64.         //breaking down (C^d mod n) and computing since C^d is too large
  65.         int modTemp = 1;
  66.         while (tempd >= 3) {
  67.             modTemp *= ((encryptedNums[i] * encryptedNums[i]) % n);
  68.             tempd -= 2;
  69.             modTemp %= n;
  70.         }
  71.  
  72.         if (tempd == 2) {
  73.             modTemp *= (encryptedNums[i] * encryptedNums[i]);
  74.             modTemp = modTemp % n;
  75.         }
  76.         else if (tempd == 1) {
  77.  
  78.             modTemp *= encryptedNums[i];
  79.             modTemp = modTemp % n;
  80.             cout << modTemp << " ";
  81.         }
  82.         else {
  83.             modTemp = modTemp % n;
  84.             cout << modTemp << " ";
  85.         }
  86.         decryptedNums.push_back(modTemp);
  87.     }
  88.     //convert decryped ints to chars
  89.     string decryptedEnglish;
  90.     for (int i = 0; i < 13; i++) {
  91.         if (decryptedNums[i] == 3) {
  92.             decryptedEnglish += "A";
  93.         }
  94.         else if (decryptedNums[i] == 4) {
  95.             decryptedEnglish += "B";
  96.         }
  97.         else if (decryptedNums[i] == 5) {
  98.             decryptedEnglish += "C";
  99.         }
  100.         else if (decryptedNums[i] == 6) {
  101.             decryptedEnglish += "D";
  102.         }
  103.         else if (decryptedNums[i] == 7) {
  104.             decryptedEnglish += "E";
  105.         }
  106.         else if (decryptedNums[i] == 8) {
  107.             decryptedEnglish += "F";
  108.         }
  109.         else if (decryptedNums[i] == 9) {
  110.             decryptedEnglish += "G";
  111.         }
  112.         else if (decryptedNums[i] == 10) {
  113.             decryptedEnglish += "H";
  114.         }
  115.         else if (decryptedNums[i] == 11) {
  116.             decryptedEnglish += "I";
  117.         }
  118.         else if (decryptedNums[i] == 12) {
  119.             decryptedEnglish += "J";
  120.         }
  121.         else if (decryptedNums[i] == 13) {
  122.             decryptedEnglish += "K";
  123.         }
  124.         else if (decryptedNums[i] == 14) {
  125.             decryptedEnglish += "L";
  126.         }
  127.         else if (decryptedNums[i] == 15) {
  128.             decryptedEnglish += "M";
  129.         }
  130.         else if (decryptedNums[i] == 16) {
  131.             decryptedEnglish += "N";
  132.         }
  133.         else if (decryptedNums[i] == 17) {
  134.             decryptedEnglish += "O";
  135.         }
  136.         else if (decryptedNums[i] == 18) {
  137.             decryptedEnglish += "P";
  138.         }
  139.         else if (decryptedNums[i] == 19) {
  140.             decryptedEnglish += "Q";
  141.         }
  142.         else if (decryptedNums[i] == 20) {
  143.             decryptedEnglish += "R";
  144.         }
  145.         else if (decryptedNums[i] == 21) {
  146.             decryptedEnglish += "S";
  147.         }
  148.         else if (decryptedNums[i] == 22) {
  149.             decryptedEnglish += "T";
  150.         }
  151.         else if (decryptedNums[i] == 23) {
  152.             decryptedEnglish += "U";
  153.         }
  154.         else if (decryptedNums[i] == 24) {
  155.             decryptedEnglish += "V";
  156.         }
  157.         else if (decryptedNums[i] == 25) {
  158.             decryptedEnglish += "W";
  159.         }
  160.         else if (decryptedNums[i] == 26) {
  161.             decryptedEnglish += "X";
  162.         }
  163.         else if (decryptedNums[i] == 27) {
  164.             decryptedEnglish += "Y";
  165.         }
  166.         else if (decryptedNums[i] == 28) {
  167.             decryptedEnglish += "Z";
  168.         }
  169.         else if (decryptedNums[i] == 29) {
  170.             decryptedEnglish += " ";
  171.         }
  172.         else if (decryptedNums[i] == 30) {
  173.             decryptedEnglish += "\"";
  174.         }
  175.         else if (decryptedNums[i] == 31) {
  176.             decryptedEnglish += ".";
  177.         }
  178.         else if (decryptedNums[i] == 32) {
  179.             decryptedEnglish += ",";
  180.         }
  181.         else if (decryptedNums[i] == 33) {
  182.             decryptedEnglish += "'";
  183.         }
  184.     }
  185.     cout << "\n" << decryptedEnglish;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement