Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include "BigIntegerLibrary.hh"
  2. #include <vector>
  3. #include <math.h>
  4. #include <time.h>
  5. #include <iostream>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. BigInteger BigIntPow(const BigInteger &base, const BigInteger &exponent)
  11. {
  12.     BigInteger zero(0);
  13.     BigInteger result(1);
  14.  
  15.     if(exponent == zero)
  16.         return result;
  17.  
  18.     for (BigInteger power(0); power < exponent; power++)
  19.         result *= base;
  20.  
  21.     return result;
  22. }
  23.  
  24. BigInteger generatePrime()
  25. {
  26.     srand ( time(NULL) );
  27.  
  28.     int q = rand() % 16 + 11;
  29.     int notDivisible, i, n = 1;
  30.  
  31.     vector <int> knownPrimes;
  32.  
  33.     knownPrimes.push_back(2);
  34.  
  35.     while(knownPrimes.size() < q)
  36.     {
  37.         n += 2;
  38.         notDivisible = 0;
  39.         i = 0;
  40.  
  41.         while(knownPrimes[i] <= sqrt((float)n))
  42.         {
  43.             if(n % knownPrimes[i] != 0)
  44.                 notDivisible++;
  45.  
  46.             i++;
  47.         }
  48.  
  49.         if(i == notDivisible)
  50.             knownPrimes.push_back(n);
  51.     }
  52.  
  53.     BigInteger prime(knownPrimes[knownPrimes.size()-1]);
  54.     return prime;
  55. }
  56.  
  57. BigInteger encrypt()
  58. {
  59.     BigInteger p = 29;
  60.     BigInteger q = generatePrime();
  61.     BigInteger N = p*q;
  62.     BigInteger fi = (p-1)*(q-1);
  63.  
  64.     cout << "p = " << p << endl;
  65.     cout << "q = " << q << endl;
  66.     cout << "N = " << N << endl;
  67.     cout << "fi = " << fi << endl << endl;
  68.  
  69.     int intE;
  70.  
  71.     cout << "Adjuk meg 'e'-t: ";
  72.     cin >> intE;
  73.     cout << endl;
  74.  
  75.     BigInteger e(intE);
  76.     BigInteger d = 0;
  77.  
  78.     while((e*d)%fi != 1)
  79.         d++;
  80.  
  81.     cout << "d = " << d << endl << endl;
  82.  
  83.     char text[50];
  84.     int ascii;
  85.     BigInteger c;
  86.  
  87.     cout << "Type the text and press 'enter' to encrypt: ";
  88.     cin >> text;
  89.     cout << endl << "Encrypted text: " << endl;
  90.  
  91.     for(unsigned int i = 0; i<strlen(text); i++)
  92.     {
  93.         ascii = (int)text[i];
  94.         BigInteger m(ascii);
  95.         c = BigIntPow(m,e)%N;
  96.  
  97.         cout << c << " ";
  98.     }
  99.  
  100.     cout << endl << endl;
  101.     return 0;
  102. }
  103.  
  104. BigInteger decrypt()
  105. {
  106.     string str;
  107.  
  108.     cout << "Type the encrypted text" << endl;
  109.     cin.ignore(1024,'\n');
  110.     getline(cin,str);
  111.     cout << endl;
  112.  
  113.     stringstream ss;
  114.     ss.str(str);
  115.  
  116.     int code;
  117.     int secretKey;
  118.     int modulus;
  119.     char text;
  120.  
  121.     cout << "Type the value of 'd' (secret key): ";
  122.     cin >> secretKey;
  123.  
  124.     cout << "Type the value of 'N' (modulus): ";
  125.     cin >> modulus;
  126.     cout << endl;
  127.  
  128.     BigInteger d(secretKey);
  129.     BigInteger N(modulus);
  130.  
  131.     cout << "The decrypted text" << endl;
  132.  
  133.     while(!ss.eof())
  134.     {
  135.         ss >> code;
  136.         BigInteger c(code);
  137.         BigInteger m = BigIntPow(c,d)%N;
  138.  
  139.         text = (char)m.toInt();
  140.         cout << text;
  141.     }
  142.  
  143.     cout << endl << endl;
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement