Created
April 3, 2019 00:28
-
-
Save proelbtn/00516be8bf390ad3c0390e4da03a823c 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
import gmpy2 | |
def get_prime(random_state, b, n=25): | |
p = gmpy2.mpz_urandomb(random_state, b) | |
while not gmpy2.is_prime(p, n): | |
p = gmpy2.mpz_urandomb(random_state, b) | |
return p | |
def get_rsa_params(random_state, bit_count, e=65537, n=25): | |
p = get_prime(state, bit_count // 2) | |
q = get_prime(state, bit_count // 2) | |
n = p * q | |
l = gmpy2.lcm(p - 1, q - 1) | |
d = gmpy2.invert(e, l) | |
return (n, e, d, p, q) | |
if __name__ == '__main__': | |
state = gmpy2.random_state(42) | |
n, e, d, p, q = get_rsa_params(state, 2048) | |
msg = input("Please input message: ") | |
m = int.from_bytes(msg.encode(), 'little') | |
c = gmpy2.powmod(m, e, n) | |
md = gmpy2.powmod(c, d, n) | |
print(m) | |
print(md) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment