Created
March 20, 2016 22:54
-
-
Save Ehnonymoose/86c04c80cb7023475fbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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