-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA.py
175 lines (141 loc) · 4.64 KB
/
RSA.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
import math
import sys
class CliArgs:
def __init__(self):
# * Default values
self.P = 1009 # ? This is the first value of the Lock
self.Q = 2741 # ? This is the second value of the Lock
self.E = 1 # ? This is the public key
self.Phi = 1
self.N = 1
self.digest = 1
self.message = 1
self.key = 1
# ! Getting the Lock from the system arguments
def PQ_args(self):
if '-pq' in sys.argv:
pq_index = sys.argv.index('-pq')
pq_index += 1
self.P = int(sys.argv[pq_index])
pq_index += 1
self.Q = int(sys.argv[pq_index])
else:
print('you must provide p and q arguments using the -pq flag...')
sys.exit(1)
# ! Getting the Public Key value from the system arguments
def E_args(self):
if '-e' in sys.argv:
e_index = sys.argv.index('-e')
e_index += 1
self.E = int(sys.argv[e_index])
else:
print('you must provide a public key argument using the -e flag...')
# ! Getting the Digest value from the system arguments
def digest_args(self):
if '-digest' in sys.argv:
digest_index = sys.argv.index('-digest')
digest_index += 1
self.digest = int(sys.argv[digest_index])
else:
print('you must provide a digest argument using the -digest flag...')
def key_args(self):
if '--key' in sys.argv:
key_index = sys.argv.index('--key')
key_index += 1
self.key = int(sys.argv[key_index])
else:
print('you must provide a key argument using the --key flag...')
def msg_args(self):
if '-m' in sys.argv:
msg_index = sys.argv.index('-m')
msg_index += 1
self.msg = int(sys.argv[msg_index])
else:
print('you must provide a message argument using the -m flag...')
# ! Getting the Phi Value
@property
def getPhi(self):
self.Phi = (self.P - 1) * (self.Q - 1) + 1
return self.Phi
# ! Getting the Product of P and Q
@property
def getN(self):
self.N = self.P * self.Q
return self.N
# ! Getters of the values
@property
def getP(self):
return self.P
@property
def getQ(self):
return self.Q
@property
def getE(self):
return self.E
@property
def getDigest(self):
return self.digest
@property
def getKey(self):
return self.key
@property
def getMessage(self):
return self.message
class RSA:
digest = 1
key = 1
message = 1
def __init__(self):
self.cli_args = CliArgs()
def crypt(self):
# ? The Crypt function is for both encryption and decryption purposes
self.cli_args.digest_args()
self.cli_args.key_args()
self.cli_args.msg_args()
self.digest = self.cli_args.getDigest()
self.key = self.cli_args.getKey()
self.message = self.cli_args.getMessage()
exponent = self.message ** self.key
encrypted_message = exponent % self.digest
print('Your de/encrypted message :: ' + str(encrypted_message))
class KeyGen:
# ? This is the private key and public key
private_key = 1
public_key = 1
digest = 1
def __init__(self):
self.cli_args = CliArgs()
def isPrime(cls, num):
# ? Checking if a number is even
if (num != 2 and num % 2 == 0) or num <= 1:
return False
sqrt = int(math.sqrt(num)) + 1
# ? Check for primality
for divisor in range(3, sqrt, 2):
if num % divisor == 0:
return False
# ? Else its a Prime
return True
def generateKeys(self):
# ? First : getting PQ from cli
self.cli_args.PQ_args()
# ? Second : getting Public Key from cli
self.cli_args.E_args()
self.public_key = self.cli_args.getE()
# ? Third : getting the product
self.digest = self.cli_args.getN()
# ? Fourth : getting Phi
Phi = self.cli_args.getPhi()
# ? Fifth : calculating the private key
product = self.public_key * self.private_key
# ? Algorithm
while product != Phi:
self.public_key += 1
self.private_key = Phi / self.public_key
product = self.public_key * self.private_key
def print_in_cli(self):
self.generateKeys()
print("Public Key == " + str(self.public_key))
print("Private Key == " + str(self.private_key))
print('Digest == ' + str(self.digest))