Skip to content

Instantly share code, notes, and snippets.

@GordonOus
Last active August 19, 2021 09:43
Show Gist options
  • Save GordonOus/dd7fadd87cf8374d38afcd935d266a8f to your computer and use it in GitHub Desktop.
Save GordonOus/dd7fadd87cf8374d38afcd935d266a8f to your computer and use it in GitHub Desktop.
Code Gists for Blog Post
from Crypto.PulicKey import RSA
key = open('key.pub').read()
# STEP 1 => get the modulus and exponent
n = key.n
e = key.e
# STEP 2 => Since itis a weak rsa: we could factorise the modulus from using this site: http://factordb.com/
# after factorising we get p and q below
q = 204234381014891586884193035672773438587347585474181580246982884758329525562862413623157552179063729873604871709450624686
05428809604025093949866146482515539
p = 280647078974346688506405094715772940902704965380721096222585441676538885813308485821406669829734814480087920756463422195
60082338772652988896389532152684857
# STEP 3 => Calculate phi using formula (p-1)(q-1)
phi = (q - 1)(p - 1)
#STEP 4 => Having phi and e, we can calculate the private exponent d which is the multiplicative invers of e. we will use the
# inverse function from pycryptodome
from Crypto.Util.number import inverse
d = inverse(e,phi)
#STEP 5 => Open the encrypted file and decrypt using d. First we convert the encrypted data into hex then integer
from base64 import b64decode
from binascii import unhexlify,hexlify
enc_data = b64decode(open('flag.b64').read())
enc_int = int(hexlify(enc_data),16)
message = pow(enc_int,d,n)
#STEP 6 => the message is in long int format: so we need to convert to hex and decode to ascii
message_decoded = unhexlify(hex(message))
print(message_decoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment