Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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