Skip to content

Instantly share code, notes, and snippets.

@intrd
Last active March 3, 2017 05:58
Show Gist options
  • Save intrd/980fa749bb586989f9e0bd97cacf2247 to your computer and use it in GitHub Desktop.
Save intrd/980fa749bb586989f9e0bd97cacf2247 to your computer and use it in GitHub Desktop.
RSA - Recover and use private key generated w/ small prime numbers - crypto200-poor_rsa @ alexctf 2017
#!/usr/bin/python
## RSA - Recover and use private key generated w/ small prime numbers - crypto200-poor_rsa @ alexctf 2017
# @author intrd - http://dann.com.br/ (original script here: http://crypto.stackexchange.com/questions/19444/rsa-given-q-p-and-e)
# @license Creative Commons Attribution-ShareAlike 4.0 International License - http://creativecommons.org/licenses/by-sa/4.0/
from Crypto.PublicKey import RSA
import gmpy, base64
pub = open("key.pub", "r").read()
pub = RSA.importKey(pub)
n = long(pub.n)
print "n"
print n
e = long(pub.e)
print "e"
print e
#w/ n, get p and q from factordb.com
p = 863653476616376575308866344984576466644942572246900013156919
print "p"
print p
q = 965445304326998194798282228842484732438457170595999523426901
print "q"
print q
d = long(gmpy.invert(e,(p-1)*(q-1)))
print "d"
print d
key = RSA.construct((n,e,d))
# message = 'myeggs..'
## to sign
# signature = key.sign(message,'')
# sign = pub.verify(message,signature)
# print "signed?"
# print sign
## to encrypt
# secret = pub.encrypt(message,'')
# print secret
#exit()
## to decrypt
secret = base64.b64decode("Ni45iH4UnXSttNuf0Oy80+G5J7tm8sBJuDNN7qfTIdEKJow4siF2cpSbP/qIWDjSi+w=")
#print secret
print key.decrypt(secret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment