/
main.py
156 lines (106 loc) · 6.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import ElGamal
import RSA
import BlumBlumShub
#!/usr/bin/env python
if __name__ == '__main__':
# Set our game ending flag to False
running = True
while running:
in_algorithm = True
print("\nEncrypter/ Decrypter")
print("================================================")
print("1. RSA Encryption\n2. El-Gamal Encryption\n3. Exit ")
# Pick a number for encryption algorithm
type_encryption = input("\nPlease Enter a number to choose encryption algorithm: ")
if type_encryption == "1" or type_encryption == "2":
# Loop until the user exit from algorithm
while in_algorithm:
print("\n------------------------------------------------------")
print("\n1 - Generate Key Pair\n2 - Encrypt a message\n3 - Decryption a cipher text\n4 - Eavesdrop and decrypt the message\n5 - Choose other encryption algorithm ")
# Get a number for user-requested function from user
function = input("\nPlease enter a number: ")
# Choose function 1 - Generate Key Pair
if function == "1":
# Get the number of bit from user
num_bit = input("Please enter the number of bit for generating keys: ")
print("\n------------------------------------------------------")
# If encryption algorithm is RSA
if type_encryption == "1" :
# Use Blum Blum Shub algorithm to generate random prime numbers
p = BlumBlumShub.generate_prime(num_bit)
q = BlumBlumShub.generate_prime(num_bit)
RSA.generate_keypair(p,q)
# If encryption algorithm is El-Gamal
elif type_encryption == "2" :
# Use Blum Blum Shub algorithm to generate random prime number
p = BlumBlumShub.generate_prime(num_bit) # 127 #999416681 #2860486313 #127
public, private = ElGamal.generate_keypair(p)
# Otherwise, function 2 - Encrypt a message
elif function == "2":
# Get the message from user
message = input("Please enter your message : ")
# Get the public key from user
public = input("Please enter the public key ( (g, p, h) for El-Gamal /(e,n) for RSA ): ")
print("\n------------------------------------------------------")
# If encryption algorithm is RSA
if type_encryption == "1":
print(" public :",public)
result = [int(x.strip()) for x in public.split(',')]
print(" result :", result)
public_key =(result[0]),(result[1])
# Encrypt with RSA Algorithm
RSA.encrypt(public_key, message)
# If encryption algorithm is El-Gamal
elif type_encryption == "2":
result = [int(x.strip()) for x in public.split(',')]
# Encrypt with El-Gamal Algorithm
ElGamal.encrypt((result[0],result[1],result[2]), message)
# Otherwise, function 3 - Decrypt a message
elif function == "3":
# Get the encrypted message from user
encrypted_msg = input("Please enter encrypted message : ")
# Get the private key from user
private = input("Please enter the private key ((g, p, x) for El-Gamal / (d,n) for RSA ): ")
print("\n------------------------------------------------------")
# If encryption algorithm is RSA
if type_encryption == "1":
result = [int(x.strip()) for x in private.split(',')]
private_key = (result[0],result[1])
# Decrypt with RSA Algorithm
RSA.decrypt(private_key, encrypted_msg)
# If encryption algorithm is El-Gamal
elif type_encryption == "2":
result = [int(x.strip()) for x in private.split(',')]
# Decrypt with El-Gamal Algorithm
ElGamal.decrypt((result[0],result[1],result[2]), encrypted_msg)
# Otherwise, function 4 - Eavesdrop and decrypt the message
elif function == "4":
# Get the encrypted message from user
encrypted_msg = input("Please enter encrypted message : ")
# Get the public key from user
public = input("Please enter the public key ((g, p, h) for El-Gamal / (e,n) for RSA ): ")
print("\n------------------------------------------------------")
# If encryption algorithm is RSA
if type_encryption == "1":
result = [int(x.strip()) for x in public.split(',')]
public_key = (result[0],result[1])
# Eavesdrop with factorization and decrypt the message with RSA Algorithm
RSA.factorization(public_key, encrypted_msg)
# If encryption algorithm is El-Gamal
elif type_encryption == "2":
result = [int(x.strip()) for x in public.split(',')]
# Eavesdrop with baby-step giant-step and decrypt the message with El-Gamal Algorithm
ElGamal.eve((result[0],result[1],result[2]), encrypted_msg)
# Otherwise, function 5 - Choose other encryption algorithm
elif function == "5":
break
# Otherwise, nope
else:
print()
print("Oh, that's not proper choose...")
elif type_encryption == "3":
running = False
# Otherwise, nope
else:
print()
print("Oh, that's not proper choose...")