|
| 1 | +from random import randint, randrange |
| 2 | +import base64 |
| 3 | + |
| 4 | +def is_prime(n, k=10): |
| 5 | + def check(a, s, d, n): |
| 6 | + x = pow(a, d, n) |
| 7 | + if x == 1 or x == n - 1: |
| 8 | + return True |
| 9 | + for _ in range(s - 1): |
| 10 | + x = pow(x, 2, n) |
| 11 | + if x == n - 1: |
| 12 | + return True |
| 13 | + return False |
| 14 | + |
| 15 | + if n < 2: |
| 16 | + return False |
| 17 | + if n in (2, 3): |
| 18 | + return True |
| 19 | + if n % 2 == 0: |
| 20 | + return False |
| 21 | + |
| 22 | + s = 0 |
| 23 | + d = n - 1 |
| 24 | + while d % 2 == 0: |
| 25 | + d >>= 1 |
| 26 | + s += 1 |
| 27 | + |
| 28 | + for _ in range(k): |
| 29 | + a = randrange(2, n - 1) |
| 30 | + if not check(a, s, d, n): |
| 31 | + return False |
| 32 | + return True |
| 33 | + |
| 34 | +def keygen(): |
| 35 | + key_p = randint(2**1023, 2**1024) |
| 36 | + while not is_prime(key_p): |
| 37 | + key_p = randint(2**1023, 2**1024) |
| 38 | + |
| 39 | + key_q = randint(2**1023, 2**1024) |
| 40 | + while not is_prime(key_q): |
| 41 | + key_q = randint(2**1023, 2**1024) |
| 42 | + |
| 43 | + key_n = key_p * key_q |
| 44 | + phi_n = (key_p - 1) * (key_q - 1) |
| 45 | + |
| 46 | + key_e = randint(2, phi_n - 1) |
| 47 | + while not is_prime(key_e): |
| 48 | + key_e = randint(2, phi_n - 1) |
| 49 | + |
| 50 | + old_r, r = key_e, phi_n |
| 51 | + old_s, s = 1, 0 |
| 52 | + |
| 53 | + while r != 0: |
| 54 | + quotient = old_r // r |
| 55 | + old_r, r = r, old_r - quotient * r |
| 56 | + old_s, s = s, old_s - quotient * s |
| 57 | + |
| 58 | + key_d = old_s % phi_n |
| 59 | + |
| 60 | + return key_n, key_e, key_d |
| 61 | + |
| 62 | +def encrypt(msg: str, key_n: int, key_e: int) -> str: |
| 63 | + msg_bytes = msg.encode('utf-8') |
| 64 | + msg_int = int.from_bytes(msg_bytes, 'big') |
| 65 | + if msg_int >= key_n: |
| 66 | + raise ValueError("Message too large for the key size.") |
| 67 | + cipher_int = pow(msg_int, key_e, key_n) |
| 68 | + cipher_bytes = cipher_int.to_bytes((cipher_int.bit_length() + 7) // 8, 'big') |
| 69 | + return base64.b64encode(cipher_bytes).decode('utf-8') |
| 70 | + |
| 71 | +def decrypt(cipher_b64: str, key_n: int, key_d: int) -> str: |
| 72 | + cipher_bytes = base64.b64decode(cipher_b64.encode('utf-8')) |
| 73 | + cipher_int = int.from_bytes(cipher_bytes, 'big') |
| 74 | + msg_int = pow(cipher_int, key_d, key_n) |
| 75 | + msg_bytes = msg_int.to_bytes((msg_int.bit_length() + 7) // 8, 'big') |
| 76 | + return msg_bytes.decode('utf-8') |
| 77 | + |
| 78 | +def main(): |
| 79 | + print("Generating RSA keypair...") |
| 80 | + key_n, key_e, key_d = keygen() |
| 81 | + print("Key generation successful!") |
| 82 | + |
| 83 | + print(f"Key D: {key_d}") |
| 84 | + print(f"Key N: {key_n}") |
| 85 | + print(f"Key E: {key_e}") |
| 86 | + |
| 87 | + message = "Whatup?" |
| 88 | + print(f"Original Message: {message}") |
| 89 | + |
| 90 | + cipher_b64 = encrypt(message, key_n, key_e) |
| 91 | + print(f"Encrypted (base64): {cipher_b64}") |
| 92 | + |
| 93 | + plaintext = decrypt(cipher_b64, key_n, key_d) |
| 94 | + print(f"Decrypted Message: {plaintext}") |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments