Skip to content

Instantly share code, notes, and snippets.

@eenblam
Created September 7, 2016 19:51
Show Gist options
  • Save eenblam/298521aab139a88041a68e069832430b to your computer and use it in GitHub Desktop.
Save eenblam/298521aab139a88041a68e069832430b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import math
from Crypto.Util.number import *
import Crypto.PublicKey.RSA as RSA
with open('key1','r') as f1, open('key2', 'r') as f2:
# Short n
n1 = long(f1.readline().strip())
# Long n
n2 = long(f2.readline().strip())
#e = long(f2.readline().strip())
e = long(65537)
rhs = ((n2 - n1 - 4) / 2) + 1
f1 = n1 - rhs
f2 = n1 + rhs
d1 = inverse(e, f1)
d2 = inverse(e, f2)
rsa1 = RSA.construct((n1, e, d1))
rsa2 = RSA.construct((n2, e, d2))
with open('encrypted', 'r') as f, open('out', 'w') as out:
encrypted = long(f.read().strip())
msg = rsa1.decrypt(rsa2.decrypt(encrypted))
out.write(long_to_bytes(msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment