Advertisement
Guest User

Untitled

a guest
Oct 1st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. #python3
  2. from math import gcd
  3. import binascii
  4. # use https://www.alpertron.com.ar/ECM.HTM to factor n
  5.  
  6. # Cipher text and private key values
  7. c = 14233215171999431620670742801992653887466654474564774567565482115708994971815998692805257101560625714845217078751863031453530789139353946667912836198369580283828831512520753040759574661583289733067194603349961285220341733595404870252973575776007626137207539918299185290666626201682475925787500697075273517928342450904756899125972158778923306466
  8.  
  9. n = 17190751765871320929594511788715040293755178677970494040999935752481499432324081420804535569049828358872534054083287610758506232432273611461779363808286315449034509728493030687881521253447668499524454596411931335376554928809260061677941571447357490867956927337940685369932675855590823386769800936470170609619423644569934236531433566627693968207
  10.  
  11. e = 65537
  12.  
  13.  
  14. # Modular multiplicative inverse
  15. def mod_inv(a, n):
  16.     t, r = 0, n
  17.     new_t, new_r = 1, a
  18.     while new_r != 0:
  19.         quotient = r // new_r
  20.         t, new_t = new_t, t - quotient * new_t
  21.         r, new_r = new_r, r - quotient * new_r
  22.     if r > 1:
  23.         raise Exception("a is not invertible")
  24.     if t < 0:
  25.         t = t + n
  26.     return t
  27.  
  28.  
  29. # Euler's totient
  30. def calc_phi_n(ps):
  31.     phi = ps[0] - 1
  32.     for i in ps[1:]:
  33.         j = i - 1
  34.         phi *= j
  35.     return phi
  36.  
  37.  
  38. # Carmichael's totient
  39. def calc_lambda_n(ps):
  40.     lcm = ps[0] - 1
  41.     for i in ps[1:]:
  42.         j = i - 1
  43.         lcm = lcm*j//gcd(lcm, j)
  44.     return lcm
  45.  
  46.  
  47. # Test factorization
  48. def test_factor(ps, n):
  49.     q = 1
  50.     for i in ps:
  51.         q *= i
  52.     if q == n:
  53.         b = True
  54.     elif q != n:
  55.         b = False
  56.     return b
  57.  
  58.  
  59. p1 = 8694307819
  60. p2 = 8775407717
  61. p3 = 9026241761
  62. p4 = 9057149533
  63. p5 = 9585778841
  64. p6 = 9733417549
  65. p7 = 9855694997
  66. p8 = 9903110561
  67. p9 = 9960439201
  68. p10 = 10016164247
  69. p11 = 10213913951
  70. p12 = 10375133233
  71. p13 = 10888745003
  72. p14 = 10928267249
  73. p15 = 11093110097
  74. p16 = 11969573459
  75. p17 = 12351917503
  76. p18 = 12471968101
  77. p19 = 13871073973
  78. p20 = 14396917097
  79. p21 = 14539225093
  80. p22 = 14717833373
  81. p23 = 14837579117
  82. p24 = 15209272151
  83. p25 = 15280005421
  84. p26 = 15484737221
  85. p27 = 15554133887
  86. p28 = 16042139507
  87. p29 = 16278648473
  88. p30 = 16335802403
  89. p31 = 16382994611
  90. p32 = 16728205063
  91. p33 = 16809927443
  92. p34 = 16861924201
  93.  
  94. ps = [p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, p31, p32, p33, p34]
  95.  
  96. assert(test_factor(ps, n))   # verify p1*p2*...p34 = n
  97.  
  98. phi_n = calc_phi_n(ps)       # Euler's Totiens of n
  99. lambda_n = (calc_lambda_n(ps)) # Carmichaels's Totient of n
  100.  
  101. d = mod_inv(e, phi_n)        # Calculate d value, trying with both phi_n and lambda_n
  102. #print(d)
  103.  
  104. m2 = pow(c, d, phi_n)        # decrypt cipher text
  105. print(bytes.fromhex(hex(m2)[2:]).decode('utf-8'))
  106.  
  107. #print(binascii.unhexlify(hexadecimals[2:]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement