Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BigIntegerLibrary.hh"
- #include <vector>
- #include <math.h>
- #include <time.h>
- #include <iostream>
- #include <sstream>
- using namespace std;
- BigInteger BigIntPow(const BigInteger &base, const BigInteger &exponent)
- {
- BigInteger zero(0);
- BigInteger result(1);
- if(exponent == zero)
- return result;
- for (BigInteger power(0); power < exponent; power++)
- result *= base;
- return result;
- }
- BigInteger generatePrime()
- {
- srand ( time(NULL) );
- int q = rand() % 16 + 11;
- int notDivisible, i, n = 1;
- vector <int> knownPrimes;
- knownPrimes.push_back(2);
- while(knownPrimes.size() < q)
- {
- n += 2;
- notDivisible = 0;
- i = 0;
- while(knownPrimes[i] <= sqrt((float)n))
- {
- if(n % knownPrimes[i] != 0)
- notDivisible++;
- i++;
- }
- if(i == notDivisible)
- knownPrimes.push_back(n);
- }
- BigInteger prime(knownPrimes[knownPrimes.size()-1]);
- return prime;
- }
- BigInteger encrypt()
- {
- BigInteger p = 29;
- BigInteger q = generatePrime();
- BigInteger N = p*q;
- BigInteger fi = (p-1)*(q-1);
- cout << "p = " << p << endl;
- cout << "q = " << q << endl;
- cout << "N = " << N << endl;
- cout << "fi = " << fi << endl << endl;
- int intE;
- cout << "Adjuk meg 'e'-t: ";
- cin >> intE;
- cout << endl;
- BigInteger e(intE);
- BigInteger d = 0;
- while((e*d)%fi != 1)
- d++;
- cout << "d = " << d << endl << endl;
- char text[50];
- int ascii;
- BigInteger c;
- cout << "Type the text and press 'enter' to encrypt: ";
- cin >> text;
- cout << endl << "Encrypted text: " << endl;
- for(unsigned int i = 0; i<strlen(text); i++)
- {
- ascii = (int)text[i];
- BigInteger m(ascii);
- c = BigIntPow(m,e)%N;
- cout << c << " ";
- }
- cout << endl << endl;
- return 0;
- }
- BigInteger decrypt()
- {
- string str;
- cout << "Type the encrypted text" << endl;
- cin.ignore(1024,'\n');
- getline(cin,str);
- cout << endl;
- stringstream ss;
- ss.str(str);
- int code;
- int secretKey;
- int modulus;
- char text;
- cout << "Type the value of 'd' (secret key): ";
- cin >> secretKey;
- cout << "Type the value of 'N' (modulus): ";
- cin >> modulus;
- cout << endl;
- BigInteger d(secretKey);
- BigInteger N(modulus);
- cout << "The decrypted text" << endl;
- while(!ss.eof())
- {
- ss >> code;
- BigInteger c(code);
- BigInteger m = BigIntPow(c,d)%N;
- text = (char)m.toInt();
- cout << text;
- }
- cout << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement