Guest User

Untitled

a guest
May 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. """
  2. pubkey.py
  3.  
  4. simulates a public key transaction
  5. May 21, 2018
  6. """
  7.  
  8. import random
  9.  
  10. # arbitrary bounds
  11. keymin = 1
  12. keymax = 255
  13.  
  14. def nth_prime_number(n):
  15. # initial prime number list
  16. prime_list = [2]
  17. # first number to test if prime
  18. num = 3
  19. # keep generating primes until we get to the nth one
  20. while len(prime_list) < n:
  21.  
  22. # check if num is divisible by any prime before it
  23. for p in prime_list:
  24. # if there is no remainder dividing the number
  25. # then the number is not a prime
  26. if num % p == 0:
  27. # break to stop testing more numbers, we know it's not a prime
  28. break
  29.  
  30. # if it is a prime, then add it to the list
  31. # after a for loop, else runs if the "break" command has not been given
  32. else:
  33. # append to prime list
  34. prime_list.append(num)
  35.  
  36. # same optimization you had, don't check even numbers
  37. num += 2
  38.  
  39. # return the last prime number generated
  40. return prime_list[-1]
  41.  
  42. def main():
  43.  
  44. # get primes
  45. index = random.randint(keymin, keymax)
  46. gp = nth_prime_number(index)
  47. index = random.randint(keymin, keymax)
  48. pp = nth_prime_number(index)
  49. print("g = " + str(gp))
  50. print("P = " + str(pp))
  51.  
  52. alice_private = random.randint(keymin,keymax)
  53. print("Alice's private key = {{ %d }}"%(alice_private))
  54.  
  55. bob_private = random.randint(keymin,keymax)
  56. print("Bob's private key = {{ %d }}"%(bob_private))
  57.  
  58. ashared = pow(gp,alice_private)%pp
  59. print("Alice's sends to Bob: %d"%(ashared))
  60.  
  61. bshared = pow(gp,bob_private)%pp
  62. print("Bob's sends to Alice: %d"%(bshared))
  63.  
  64. asecret = pow(bshared,alice_private)%pp
  65. bsecret = pow(ashared,bob_private)%pp
  66. print("Bob and Alice both secretly use: {{ %d }} and {{ %d }}"%(asecret, bsecret))
  67.  
  68. if __name__ == '__main__':
  69. main()
Add Comment
Please, Sign In to add comment