-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa alg.py
More file actions
164 lines (128 loc) · 3.72 KB
/
rsa alg.py
File metadata and controls
164 lines (128 loc) · 3.72 KB
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
import math
# Input Prime Numbers
print("PLEASE ENTER THE 'p' AND 'q' VALUES BELOW:")
p = int(input("Enter a prime number for p: "))
q = int(input("Enter a prime number for q: "))
# Check if Input's are Prime
def prime_check(a):
if (a == 2):
return True
elif ((a < 2) or ((a % 2) == 0)):
return False
elif (a > 2):
for i in range(2, a):
if not (a % i):
return false
return True
check_p = prime_check(p)
check_q = prime_check(q)
while (((check_p == False) or (check_q == False))):
p = int(input("Enter a prime number for p: "))
q = int(input("Enter a prime number for q: "))
check_p = prime_check(p)
check_q = prime_check(q)
# RSA Modulus
n = p * q
print("RSA Modulus(n) is:", n)
# Eulers Toitent
r = (p - 1) * (q - 1)
print("Eulers Toitent(r) is:", r)
print("*****************************************************")
# GCD
def egcd(e, r):
while (r != 0):
e, r = r, e % r
return e
# Euclid's Algorithm
def eugcd(e, r):
for i in range(1, r):
while (e != 0):
a, b = r // e, r % e
if (b != 0):
print("%d = %d*(%d) + %d" % (r, a, e, b))
r = e
e = b
# Extended Euclidean Algorithm
def eea(a, b):
if (a % b == 0):
return (b, 0, 1)
else:
gcd, s, t = eea(b, a % b)
s = s - ((a // b) * t)
print("%d = %d*(%d) + (%d)*(%d)" % (gcd, a, t, s, b))
return (gcd, t, s)
# Multiplicative Inverse
def mult_inv(e, r):
gcd, s, _ = eea(e, r)
if (gcd != 1):
return None
else:
if (s < 0):
print("s=%d. Since %d is less than 0, s = s(modr), i.e., s=%d." % (s, s, s % r))
elif (s > 0):
print("s=%d." % (s))
return s % r
# e Value Calculation
for i in range(1, 1000):
if (egcd(i, r) == 1):
e = i
print("The value of e is:", e)
# d, Private and Public Keys
print("EUCLID'S ALGORITHM:")
eugcd(e, r)
print("EUCLID'S EXTENDED ALGORITHM:")
d = mult_inv(e, r)
print("END OF THE STEPS USED TO ACHIEVE THE VALUE OF 'd'.")
print("The value of d is:", d)
public = (e, n)
private = (d, n)
print("Private Key is:", private)
print("Public Key is:", public)
# Encryption
def encrypt(pub_key, n_text):
e, n = pub_key
x = []
m = 0
for i in n_text:
if (i.isupper()):
m = ord(i) - 65
c = (m ** e) % n
x.append(c)
elif (i.islower()):
m = ord(i) - 97
c = (m ** e) % n
x.append(c)
elif (i.isspace()):
spc = 400
x.append(400)
return x
# Decryption
def decrypt(priv_key, c_text):
d, n = priv_key
txt = c_text.split(',')
x = ''
m = 0
for i in txt:
if (i == '400'):
x += ' '
else:
m = (int(i) ** d) % n
m += 65
c = chr(m)
x += c
return x
# Message
message = input("What would you like encrypted or decrypted?(Separate numbers with ',' for decryption):")
print("Your message is:", message)
# Choose Encrypt or Decrypt and Print
choose = input("Type '1' for encryption and '2' for decrytion.")
if (choose == '1'):
enc_msg = encrypt(public, message)
print("Your encrypted message is:", enc_msg)
print("Thank you for using the RSA Encryptor. Goodbye!")
elif (choose == '2'):
print("Your decrypted message is:", decrypt(private, message))
print("Thank you for using the RSA Encryptor. Goodbye!")
else:
print("You entered the wrong option.")
print("Thank you for using the RSA Encryptor. Goodbye!")