Skip to content

Instantly share code, notes, and snippets.

@paced
Last active November 14, 2017 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paced/097e457ac4b691e5623e504373f90dde to your computer and use it in GitHub Desktop.
Save paced/097e457ac4b691e5623e504373f90dde to your computer and use it in GitHub Desktop.
Decryption with C, p, q, dp, and dq for RSA's Chinese Remainder Theorem.
"""Decrypt RSA using the Chinese Remainder Theorem."""
C = 95272795986475189505518980251137003509292621140166383887854853863720692420204142448424074834657149326853553097626486371206617513769930277580823116437975487148956107509247564965652417450550680181691869432067892028368985007229633943149091684419834136214793476910417359537696632874045272326665036717324623992885
p = 11387480584909854985125335848240384226653929942757756384489381242206157197986555243995335158328781970310603060671486688856263776452654268043936036556215243
q = 12972222875218086547425818961477257915105515705982283726851833508079600460542479267972050216838604649742870515200462359007315431848784163790312424462439629
dp = 8191957726161111880866028229950166742224147653136894248088678244548815086744810656765529876284622829884409590596114090872889522887052772791407131880103961
dq = 3570695757580148093370242608506191464756425954703930236924583065811730548932270595568088372441809535917032142349986828862994856575730078580414026791444659
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def int2Text(number, size):
text = "".join([chr((number >> j) & 0xff)
for j in reversed(range(0, size << 3, 8))])
return text.lstrip("\x00")
qinv = modinv(q, p)
m1 = pow(C, dp, p) # (C ** dp) % p
m2 = pow(C, dp, p) # (C ** dq) % q
h = ((m1 - m2) * qinv) % p
y = m2 + (q * h)
print int2Text(y, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment