-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (45 loc) · 1.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
import libnum
from Crypto.Random import get_random_bytes
from Crypto.Util.number import getPrime, bytes_to_long, long_to_bytes
# Default key length for primes
key_size = 2048
# Message to be encrypted and decrypted
message = "Hello"
# Override with command-line arguments if needed
if len(sys.argv) > 1:
message = str(sys.argv[1])
if len(sys.argv) > 2:
key_size = int(sys.argv[2])
# Generate prime numbers p and q
prime_p = getPrime(key_size, randfunc=get_random_bytes)
prime_q = getPrime(key_size, randfunc=get_random_bytes)
# Compute modulus n and Euler's totient (PHI)
modulus_n = prime_p * prime_q
totient_phi = (prime_p - 1) * (prime_q - 1)
# Public exponent (standard RSA value)
public_exponent_e = 65537
# Private exponent d (modular inverse of e modulo PHI)
private_exponent_d = libnum.invmod(public_exponent_e, totient_phi)
# Convert the message to a number (plaintext)
plaintext_m = bytes_to_long(message.encode('utf-8'))
# Check if the message is too large for encryption (should be smaller than n)
if plaintext_m >= modulus_n:
print(f"Error: Message is too long to encrypt with the given key size of {key_size} bits.")
sys.exit(1)
# Encrypt the plaintext to produce the ciphertext
ciphertext_c = pow(plaintext_m, public_exponent_e, modulus_n)
# Decrypt the ciphertext to recover the plaintext
decrypted_m = pow(ciphertext_c, private_exponent_d, modulus_n)
# Print results
print(f"Original message: {message}")
print(f"Prime p: {prime_p}")
print(f"Prime q: {prime_q}")
print(f"Modulus n (p * q): {modulus_n}")
print(f"Euler's Totient (phi): {totient_phi}")
print(f"Public exponent (e): {public_exponent_e}")
print(f"Private exponent (d): {private_exponent_d}")
print(f"\nPrivate key: (d={private_exponent_d}, n={modulus_n})")
print(f"Public key: (e={public_exponent_e}, n={modulus_n})")
print(f"\nCiphertext: {ciphertext_c}")
print(f"Decrypted message: {long_to_bytes(decrypted_m).decode('utf-8')}")