Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3.  
  4. int ModularExponential(int,int,int);
  5. int gcd(int,int,int&,int&);
  6. int calculateD(int);
  7. int getE(int,int);
  8.  
  9. int main()
  10. {
  11.         FILE *f;
  12.         int arr[5000];
  13.                 f = fopen("file.txt","r");
  14.                 if(f != NULL){
  15.                        
  16.                         for(int i = 0; i < 5000; ++i)
  17.                             fscanf(f,"%d", &arr[i]);
  18.                        
  19.                 }
  20.                 else
  21.                     std::cout << "No file";
  22.                int i,j;
  23.                int p = arr[std::rand()%1000];;
  24.                 int q = arr[std::rand()%1000];
  25.                int n = p * q;
  26.                 int Euler = (p-1)*(q-1);
  27.                 int d = calculateD(Euler);
  28.                 int e = getE(Euler,d);
  29.                 int g = gcd(e,Euler, i, j);
  30.                 int m = 2;
  31.                 int k = ModularExponential(m,e,n);
  32.                 int l = ModularExponential(m,e*d,n);
  33.                std::cout <<" p = " << p << " q = " <<q << " phi = " << Euler <<
  34.                    " e = " << e << " d = " << d << " k = " << k << " l = " << l;
  35.                 fclose(f);
  36.         system("pause");
  37.                 return 0;
  38. }
  39.  
  40. int ModularExponential(int x,int d ,int n){
  41.         x %= n;
  42.         int result = 1;
  43.         while(d > 0){
  44.                         if( d & 1)
  45.                                 result = (result * x) % n;
  46.                         x = (x*x) % n;
  47.                         d >>= 1;
  48.         }
  49.         return result;
  50. }
  51.  
  52. int gcd (int a, int b, int & x, int & y) {
  53.     if (a == 0) {
  54.         x = 0; y = 1;
  55.         return b;
  56.     }
  57.     int x1, y1;
  58.     int d = gcd (b%a, a, x1, y1);
  59.     x = y1 - (b / a) * x1;
  60.     y = x1;
  61.     return d;
  62. }
  63.  
  64. int calculateD(int phi){
  65.     int d_s = 0, d,x,y;
  66.     while(d_s != 1)
  67.     {
  68.         d = rand()%100;
  69.         d_s = gcd(d,phi,x,y);
  70.     }
  71.     return d_s;
  72. }
  73.  
  74. int getE(int phi,int d){
  75.     int e = 0, e_s = 0, x,y;
  76.     while(e_s != 1){
  77.         e += 1;
  78.         e_s = (e*d)%phi;
  79.     }
  80.     return e_s;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement