Guest User

Untitled

a guest
Jan 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. p = 1234567891
  2. q = 987654323
  3. e = 127
  4. m = 14152019010605
  5.  
  6.  
  7. n = p * q
  8. print('n = ', n)
  9.  
  10. c = (m ** e) % n
  11. print('cyphertext = ', c)
  12.  
  13. a = p-1
  14. b = q-1
  15. phi = a*b
  16.  
  17. # extended Euclidian algorithm to find x and y
  18. # for ax + bx = gcd(a,b)
  19. def egcd(a,b):
  20. x,y, u,v = 0,1, 1,0
  21. while a != 0:
  22. q,r = b//a, b%a
  23. m, n = x-u*q, y-v*q
  24. b,a, x,y, u,v = a,r, u,v, m,n
  25. gcd = b
  26.  
  27. return x, y
  28.  
  29. print('x, y = ', egcd(phi,e))
  30.  
  31.  
  32. # find greatest common divisor between 2 number
  33. def gcd(a,b):
  34. while b != 0:
  35. a, b = b, a % b
  36. return a
  37.  
  38. print('gcd = ', gcd(a,b))
  39.  
  40.  
  41. # find d, given e and phi
  42. def multiplicative_inverse(e,phi):
  43. d = 0
  44. x1 = 0
  45. x2 = 1
  46. y1 = 1
  47. temp_phi = phi
  48.  
  49. while e > 0:
  50. temp1 = temp_phi//e
  51. temp2 = temp_phi - temp1 * e
  52. temp_phi = e
  53. e = temp2
  54.  
  55. x = x2 - temp1 * x1
  56. y = d - temp1 * y1
  57.  
  58. x2 = x1
  59. x1 = x
  60. d = y1
  61. y1 = y
  62.  
  63. if temp_phi == 1:
  64. return d + phi
  65.  
  66. print('d = ', multiplicative_inverse(e,phi))
  67.  
  68. print('private key is ', multiplicative_inverse(e,phi),',', n)
  69. print('public key is ', e,',',n)
Add Comment
Please, Sign In to add comment