Advertisement
Guest User

RSAalgo

a guest
Mar 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. std::vector<int> binaryVec;
  5.  
  6. void binaryConvert(int number) {
  7.  
  8. if (number > 0) {
  9. int bin = number % 2;
  10. number /= 2;
  11. binaryConvert(number);
  12. binaryVec.push_back(bin);
  13. }
  14. }
  15.  
  16. // P( e, n ) -> public key
  17. // S( d, n ) -> secret key
  18.  
  19. int main() {
  20.  
  21. const int e = 11; // e is a number which is made by: biggest common divisor(e,f), where e and f are relative prime numbers, and f = (p-1) * (q-1)
  22. const int n = 899; // n is p*q, where p and q are prime numbers
  23. const int d = 611; // is the secret variable of the secret key
  24. long int M;
  25. long int result;
  26.  
  27. std::cout << "give me a number" << std::endl;
  28. std::cin >> M;
  29.  
  30. binaryConvert(M);
  31.  
  32. int c = 1;
  33.  
  34. for (int i = 0; i < binaryVec.size(); i++) {
  35. if (binaryVec[i] == 1) {
  36. c = (c*c) % n;
  37. c = (100 * c) % n;
  38. }
  39. else {
  40. c = (c*c) % n;
  41. }
  42. result = c;
  43. }
  44.  
  45. std::cout << "result is " << result << std::endl;
  46.  
  47. system("PAUSE");
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement