the_baahubali

Untitled

Mar 31st, 2022
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. import random
  2. import math
  3.  
  4.  
  5.  
  6. def isPrime(number):
  7.  
  8.     # number must be greater than 1
  9.     if (number <= 1):
  10.         return False
  11.  
  12.     # check if number has no divisor other than itself and 1
  13.     for i in range(2, number):  # int(math.sqrt(number)+1) i > 2 3 4 5 6 7 8 9
  14.         if number % i == 0:
  15.             return False
  16.        
  17.     return True
  18.  
  19.  
  20. # STEP 1: SELECT DIFFERENT PRIME NUMBERS FOR p AND q
  21.  
  22. # create a list of prime numbers
  23. # pick two prime numbers from the list
  24. primes = [ i for i in range(0, 500) if isPrime(i) ]
  25.  
  26.  
  27. p = random.choice(primes)
  28. q = random.choice(primes)
  29.  
  30. # check that the two numbers are not equal
  31. while p == q:
  32.     q = random.choice(primes)
  33.    
  34. # STEP 1 ENDS
  35.  
  36.  
  37. # STEP 2 MULTIPLY p AND q
  38. n = p*q
  39. # STEP 2 ENDS
  40.  
  41. # STEP 3 phi = (p-1) x (q-1)
  42. phi = (p-1) * (q-1)
  43. # STEP 3 ENDS
  44.  
  45.  
  46. # STEP 4
  47.  
  48. # set/find gcd
  49. gcd = 2
  50.  
  51. while gcd != 1:  # when gcd is 1 the two are prime to each other
  52.     e = random.randrange(1, phi)
  53.     gcd = math.gcd(e, phi)
  54.     # set/fnid modulo de % phi
  55.    
  56. # STEP 4 ENDS
  57.  
  58. # STEP 5
  59. modDE = 2  
  60.  
  61. while modDE != 1:
  62.     d = random.randrange(1, phi)
  63.     de = d * e
  64.     modDE = de % phi
  65.  
  66. # STEP 5 ENDS
  67.  
  68. # print keys
  69. print(f'publicKey: { {e}, {n} }')
  70. print(f'privateKey: { {d}, {n} }')
  71.  
Advertisement
Add Comment
Please, Sign In to add comment