Advertisement
Aveneid

Untitled

Dec 7th, 2020
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. def GCD(n, m):
  2.     while m!= 0:
  3.         print(" "+str(n)+","+str(m))
  4.         t=m
  5.         m=n%m
  6.         n=t
  7.     return n
  8.  
  9. def not_mod(n,m):
  10.     c=0
  11.     d=1
  12.     a=n
  13.     b=m
  14.     e=b/a
  15.     f=b%a
  16.     while f > 0:
  17.         t = c - e * d
  18.         if t>= 0:
  19.           t = t % m
  20.         else:
  21.           t = m - ((-t) % m)
  22.         c=d,d=t
  23.         b=a,a=f
  24.         e=b/a
  25.         f=b%a
  26.     return d
  27.  
  28. def powmod(n,m,o):
  29.     t=n
  30.     u=1
  31.     v=m
  32.     while v>0:
  33.         if v%2:
  34.             u=(u*t)%o
  35.         t=(t*t)%o
  36.         v=v/2
  37.     return u
  38.  
  39. def rsaKeys(p,q):
  40.     phi = (p-1)*(q-1)
  41.     N = p*q
  42.     e=3
  43.     while GCD(e,phi) !=1:
  44.         d=not_mod(e,phi)
  45.         e=+2
  46.     print("pub key: (e,n) "+e+","+N)
  47.     print("priv key: (d,n) "+d+","+N)
  48.  
  49. def RSA(q,p,m):
  50.     print(powmod(m,q,p))
  51.  
  52.  
  53. print("Genreate RSA key pair - 1")
  54. print("Encrypt message - 2")
  55. choice = int(input("What do you want? "))
  56. if choice:
  57.     if choice == 1:
  58.         print("q=?")
  59.         q=int(input())
  60.         print("p=?")
  61.         p=int(input())
  62.         rsaKeys(p,q)
  63.     if choice == 2:
  64.         q=int(input("q=? "))
  65.         p=int(input("p=? "))
  66.         m=int(input("message=? "))
  67.         RSA(q,p,m)
  68.  
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement