Phú Nguyễn

32 bundles
1 file24 days ago
1

rsa.py

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
import random def prime_check(n, rounds=50): if n == 2: return True if not n & 1: return False def check(a, s, d, n): x = pow(a, d, n) if x == 1: return True for i in range(s - 1): if x == n - 1: return True x = pow(x, 2, n) return x == n - 1 s = 0 d = n - 1 while d % 2 == 0: d >>= 1 s += 1 for i in range(rounds): a = random.randrange(2, n - 1) if not check(a, s, d, n): return False return True def generate_primes(): p = [] for i in range(0, 2): prime = random.randint(2 ** (256 - 1), 2 ** 256) while prime_check(prime) == False: prime = random.randint(2 ** (256 - 1), 2 ** 256) p.append(prime) return p def gcd(a, b): if b == 0: return a return gcd(b, a % b) def egcd(a, b): x0 = 1 x1 = 0 y0 = 0 y1 = 1 while b > 0: r = a % b if r == 0: break q = a // b x = x0 - x1 * q y = y0 - y1 * q a = b b = r x0 = x1 x1 = x y0 = y1 y1 = y return x, y def get_e(phi): e = random.randint(1, phi) while gcd(e, phi) != 1: e = random.randint(1, phi) return e def get_d(e, phi): d = egcd(e, phi)[0] if d < 0: d += phi return d def generate_keys(p, q): n = p * q phi = (p - 1) * (q - 1) e = get_e(phi) d = get_d(e, phi) return [[e, n], [d, n]] def string_to_int(text): x = 0 for i in text: x = 256 * x + ord(i) return x def long_to_string(long): x = "" while long != 0: a = long % 256 x = chr(a) + x long >>= 8 return x def encrypt_rsa(plain, public): plaintext = string_to_int(plain) ciphertext = pow(plaintext, public[0], public[1]) return ciphertext def decrypt_rsa(cipher, private): ciphertext = pow(cipher, private[0], private[1]) return long_to_string(ciphertext) select = input("1 for encryption, 2 for decryption, 3 for generate keys: ") if select == "1": message = input("Input the message: ") auto_generate = input("Auto generate keys? [Y/N]") if auto_generate == "Y" or auto_generate == "y": p, q = generate_primes() pub_key, priv_key = generate_keys(p, q) print("generated public key/ private key: ", pub_key, priv_key) ciphertext = encrypt_rsa(message, pub_key) print("Ciphertext: ", ciphertext) print("Plaintext: ", message) else: e_key = int(input("Input e key: ")) n_key = int(input("Input n key: ")) pub_key = (e_key, n_key) ciphertext = encrypt_rsa(message, pub_key) print("Ciphertext: ", ciphertext) print("Plaintext: ", message) elif select == "2": message = int(input("Input the encrypted message: ")) d_key = int(input("Input d key: ")) n_key = int(input("Input n key: ")) priv_key = (d_key, n_key) print("Encrypted message: ", message) plaintext = decrypt_rsa(message, priv_key) print("Plaintext: ", plaintext) elif select == "3": p = int(input("Input p: ")) q = int(input("Input q: ")) while not prime_check(p) or not prime_check(q): p = int(input("Input p: ")) q = int(input("Input q: ")) pub_key, priv_key = generate_keys(p, q) print("Public key: ", pub_key) print("Private key: ", priv_key)