maroph

rsa_keygen_enc_dec.py

Dec 7th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. import math
  2. import random
  3. import sympy
  4.  
  5. # extended_gcd, modinv:
  6. # https://rosettacode.org/wiki/Modular_inverse#Iteration_and_error-handling
  7.  
  8.  
  9. def extended_gcd(aa, bb):
  10.     lastremainder, remainder = abs(aa), abs(bb)
  11.     x, lastx, y, lasty = 0, 1, 1, 0
  12.     while remainder:
  13.         lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder)
  14.         x, lastx = lastx - quotient * x, x
  15.         y, lasty = lasty - quotient * y, y
  16.     return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1)
  17.  
  18.  
  19. def modinv(a, m):
  20.     g, x, y = extended_gcd(a, m)
  21.     if g != 1:
  22.         raise ValueError
  23.     return x % m
  24.  
  25.  
  26. # prime computing based on the following code
  27. # https://trinket.io/python3/2fb69ab246
  28.  
  29.  
  30. key_size = 16
  31.  
  32.  
  33. def get_primes():
  34.     prime1 = 0
  35.     prime2 = 0
  36.     while prime1 == prime2 or (prime1 * prime2) > 2**key_size:
  37.         prime1 = sympy.randprime(3, 2**key_size/2)
  38.         prime2 = sympy.randprime(3, 2**key_size/2)
  39.     return (prime1, prime2)
  40.  
  41.  
  42. # build a list of all pseudo primes
  43. def get_all_pseudo_primes():
  44.     pp = []
  45.     for n in range(3, phi - 1):
  46.         if math.gcd(phi, n) == 1:
  47.             pp.append(n)
  48.     return pp
  49.  
  50.  
  51. def encrypt(message):
  52.     ca = []
  53.     for c in message:
  54.        ca.append((ord(c)**e) % r)
  55.     return ca
  56.  
  57.  
  58. def decrypt(cipher_message):
  59.     message = ''
  60.     for c in cipher_message:
  61.         message += chr((c**d) % r)
  62.     return message
  63.  
  64.  
  65. def hexdump(cipher_array):
  66.     return ''.join(format(x, '02x') for x in cipher_array)
  67.  
  68.  
  69. # prime1, prime2
  70. p, q = get_primes()
  71.  
  72. # RSA modulus
  73. r = p * q
  74.  
  75. # Euler's totient
  76. phi = (p-1) * (q-1)
  77.  
  78. pseudo_primes = get_all_pseudo_primes()
  79. # print('number of pseudo primes :', len(pseudo_primes))
  80.  
  81. # choose one of the pseudo primes by random as public exponent
  82. e = random.choice(pseudo_primes)
  83. # compute the private exponent
  84. d = modinv(e, phi)
  85.  
  86. # print data in use
  87. print('prime1           (p)   :', p)
  88. print('prime2           (q)   :', q)
  89. print('RSA modulus      (r)   :', r)
  90. print("Euler's totient  (phi) :", phi)
  91. print('public exponent  (e)   :', e)
  92. print('private exponent (d)   :', d)
  93. print("Private Key            : (" + str(r) + "," + str(d) + ")")
  94. print("Public Key             : (" + str(r) + "," + str(e) + ")")
  95. print('')
  96.  
  97. plain_text = "Don't Panic!"
  98.  
  99. cipher_array = encrypt(plain_text)
  100. message = decrypt(cipher_array)
  101.  
  102. print('plain text :', plain_text)
  103. print('encrypt    :', hexdump(cipher_array))
  104. print('decrypt    :', message)
Advertisement
Add Comment
Please, Sign In to add comment