Skip to content

Instantly share code, notes, and snippets.

@Ehnonymoose
Created March 20, 2016 22:54
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 Ehnonymoose/86c04c80cb7023475fbc to your computer and use it in GitHub Desktop.
Save Ehnonymoose/86c04c80cb7023475fbc to your computer and use it in GitHub Desktop.
from Crypto.PublicKey import RSA
with open('public_key.pem', 'rb') as f:
modulus = RSA.importKey(f.read()).n
with open('message.enc', 'rb') as f:
message = int.from_bytes(f.read(), byteorder='big')
def cube_root(n):
guess, previous = n, 0
while abs(guess - previous) > 1:
previous = guess
guess = guess - (guess // 3) + (n // (3 * pow(guess, 2)))
while pow(guess, 3) > n:
guess -= 1
return guess
def to_bytes(n):
return n.to_bytes((n.bit_length() // 8) + 1, byteorder='big')
def try_multiple(k):
message_guess = cube_root(message + k * modulus)
message_bytes = to_bytes(message_guess)
if max(message_bytes) < 128:
print(message_bytes)
return True
else:
return False
k = 0
while not try_multiple(k):
k += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment