-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA_Encryption.py
181 lines (126 loc) · 3.22 KB
/
RSA_Encryption.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
176
177
178
179
180
181
"""
# Encrypts strings to int values
# using RSA encryption
#
#
#
"""
def encrypt(M, PU): # M list PU List
C = []
for i in M:
a = (i**PU[0])%PU[1]
C.append(a)
return C
def decrypt(C, PR):
M = []
for i in C:
a = (i**PR[0])%PR[1]
M.append(a)
return M
def keygen(p , q):
n = p*q
phi_n = (p-1)*(q-1)
e = 2
E = [] #need to find 5 e
for i in range(5):
while gcd(phi_n, e) != 1: # this will search for an e that is prime
e+=1
E.append(e) #store the prime e
e+=1
#Find all d Values
D = []
for i in range(5):
D.append(euc(phi_n,E[i]))
#return E D n and phi n
keys = (E,D, n, phi_n)
return keys
#end
def euc(a, b):
#phi_n = a, e = b
dd, dv = a, b
r = [a, b, 0]
x = [1, 0, 0]
y = [0, 1, 0]
i = 2
q = dd/dv #divisor/ dividend
r[i] = a - q*b
while(r[i] != 0):
q = dd/dv #divisor/ dividend
r[i] = r[i-2]-r[i-1] * q
y[i] = y[i-2]-y[i-1] * q
x[i] = x[i-2]-x[i-1] * q
r[i-2] = r[i-1]
r[i-1] = r[i]
y[i-2] = y[i-1]
y[i-1] = y[i]
x[i-2] = x[i-1]
x[i-1] = x[i]
dd, dv = dv, r[i]
return ((y[i-2])%a)
def gcd(a, b):
while(b != 0):
rem = a%b
a = b
b = rem
return a
def to_int(M):
alphaL = 'abcdefghijklmnopqrstuvwxyz'
alphaU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numPlus ='0123456789 .,:?'
rsaBlock = alphaL +alphaU +numPlus # rsa list
newM = []
for i in M:
if i in rsaBlock:
newM.append(rsaBlock.find(i))
m = [] #array grouped into pairs
i = 0
while i < len(newM):
tmp = newM[i]*100 + newM[i+1]
m.append(tmp)
i+=2
return m
def to_char(M):
alphaL = 'abcdefghijklmnopqrstuvwxyz'
alphaU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
numPlus ='0123456789 .,:?'
rsaBlock = alphaL +alphaU +numPlus
newM = []
i = 0
while i < len(M):
tmp = M[i]/100
newM.append(tmp)
newM.append(M[i]-tmp*100)
i+=1
pt =''
for i in newM:
pt+=rsaBlock[i]
return pt
def main():
p = 73
q = 151
plaintext = "How are you?"
M = to_int(plaintext)
keys = keygen(p,q)
E = keys[0]
D = keys[1]
n = keys[2]
phi_n = keys[3]
print "RSA Key info:"
print "Original plain text: %s" % plaintext
print "n: %d, Phi(n): %d" %(n, phi_n)
print "List of e's: ", E
print "List of d's", D
print "\n"
for i in range(5):
PU = [E[i], n]
PR = [D[i], n]
cipher = encrypt(M ,PU)
pt = decrypt(cipher, PR)
print "Public key: ", PU
print "Cipher text :" ,cipher
print "Private key: ", PR
print "Plain text :" ,pt
print "\n"
dec = to_char(M)
print "Decrypted cipher text: %s" % dec
main()