Advertisement
sheyshya1

rsa

Mar 28th, 2022
1,314
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 1
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. //to find gcd
  5. int gcd(int a, int h)
  6. {
  7.     int temp;
  8.     while(1)
  9.     {
  10.         temp = a%h;
  11.         if(temp==0)
  12.         return h;
  13.         a = h;
  14.         h = temp;
  15.     }
  16. }
  17.  
  18. int main()
  19. {
  20.     //2 random prime numbers
  21.     double p = 3;
  22.     double q = 7;
  23.     double n=p*q;
  24.     double count;
  25.     double totient = (p-1)*(q-1);
  26.  
  27.     //public key
  28.     //e stands for encrypt
  29.     double e=2;
  30.  
  31.     //for checking co-prime which satisfies e>1
  32.     while(e<totient){
  33.     count = gcd(e,totient);
  34.     if(count==1)
  35.         break;
  36.     else
  37.         e++;
  38.     }
  39.  
  40.     //private key
  41.     //d stands for decrypt
  42.     double d;
  43.  
  44.     //k can be any arbitrary value
  45.     double k = 2;
  46.  
  47.     //choosing d such that it satisfies d*e = 1 + k * totient
  48.     d = (1 + (k*totient))/e;
  49.     double msg = 12;
  50.     double c = pow(msg,e);
  51.     double m = pow(c,d);
  52.     c=fmod(c,n);
  53.     m=fmod(m,n);
  54.  
  55.     printf("Message data = %lf",msg);
  56.     printf("\np = %lf",p);
  57.     printf("\nq = %lf",q);
  58.     printf("\nn = pq = %lf",n);
  59.     printf("\ntotient = %lf",totient);
  60.     printf("\ne = %lf",e);
  61.     printf("\nd = %lf",d);
  62.     printf("\nEncrypted data = %lf",c);
  63.     printf("\nOriginal Message Sent = %lf",m);
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement