-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSA_Encryption.py
181 lines (158 loc) · 4.75 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
#RSA Algorithm
#Done! Step 1: Take two large distinct primes p and q
#Step 2: Multiply p and q and store in n
#Step 3: Find the toitent for n (phi = p-1*q-1)
#Step 4: Find e which is coprime between 1 and n
#Step 5: find d using d*e = 1 mod phi(n)
#note: (e,n) is public key, (d,n) is private key
#Step 6: Encrypt the message and Decrypt the ciphertext
import random
import math
def modularExpo(x,p):
#a==base b == exponent c == mod
binaryNumber = [int(i) for i in list('{0:0b}'.format(p-1))]
binaryNumber.reverse()
powersOfTwo = list()
for i in range(len(binaryNumber)):
if i == 0 or i == 1:
powersOfTwo.append(2**i)
else:
powersOfTwo.append((powersOfTwo[i-1]**2)%p)
x = 1
for i in range(0, len(binaryNumber)):
if binaryNumber[i] == 1:
x = (x* powersOfTwo[i]) % p
return x
def binaryExpo(a,b):
res = 1
while b>0:
if b&1:
res = res*a
a = a*a
b >>=1
return res
def binpow(a,b,m):
a %=m
res = 1
while b>0:
if b&1:
res = res*a%m
a = a*a%m
b >>=1
return res
def isPrime(n):
prime = False
if ((modularExpo(2,n) == 1)):
prime = True
return prime
def generateLargePrime(k):
while True:
n = random.randrange(2**(k-1), (2**k))
if (isPrime(n) == True):
break
return n
def euclid(a,b):
if a == 0:
return b
return euclid(b%a,a)
def euclidExtended(a,b):
if a == 0:
return (b, 0, 1)
else:
gcd, x, y = euclidExtended(b % a, a)
return (gcd, y - (b//a) * x, x)
def encrypt(key, message):
print("In the function")
e, n = key
send = binpow(message, e,n)
print("about the leave the function")
return send
def decrypt(publicKey, privateKey, cipher):
d, n = privateKey
send = binpow(cipher, d, n)
return send
def modInverse(a, m):
m0 = m
y = 0
x = 1
if (m == 1) :
return 0
while (a > 1) :
# q is quotient
q = a // m
t = m
# m is remainder now, process
# same as Euclid's algo
m = a % m
a = t
t = y
# Update x and y
y = x - q * y
x = t
# Make x positive
if (x < 0) :
x = x + m0
return x
def main():
print("Welcome to my RSA program!")
print("Step 1: Generate p and q:")
#p = large prime
print("Generating P...")
p = generateLargePrime(k=1000)
#q = Large prime
print("Generating Q...")
q = generateLargePrime(k=1000)
while abs(p-q) < 10**95:
q = generateLargePrime(k=1000)
print("Step 1 Completed.\n Step 2: Generate N.")
n = p*q
print("Step 2 Completed. \n Step 3:Find the toitent for n (phi = p-1*q-1) ")
phi = (p-1) * (q-1)
print("Step 3 Completed. \n Step 4: Find e which is coprime between 1 and n ")
e = 65537
while euclid(e, phi) != 1:
e = random.randrange(1,phi)
print("Step 4 Completed. \n Step 5: Find d using d*e = 1 mod phi(n) ")
# gcd, x,y = euclidExtended(e, phi)
d = modInverse(e,phi)
print("Step 5 Completed.\n Private and public keys have been generated.")
#private key = d,n // public key = e,n
publicKey = e, n
privateKey = d, n
f = open("message.txt", "r+") #open file containing plaintext
plaintext = int(f.read()) #read the plntext into variable pText
pPrivate = open("private_key.txt", "a+")
pPrivate.write("\nPrivate Key:\n")
pPrivate.write("D: ")
pPrivate.write(str(d))
pPrivate.write("\nN: ")
pPrivate.write(str(n))
pPublic = open("public_key.txt", "a+")
pPublic.write("\nPublic Key:\n")
pPublic.write("E: ")
pPublic.write(str(e))
pPublic.write("\nN: ")
pPublic.write(str(n))
pPrivate.close()
pPublic.close()
print("Private and Public keys saved\n Step 6: Encryption and Decryption ")
print("Encrypting...")
ciphertext = encrypt(publicKey, plaintext)
print("returned from function")
print(ciphertext)
pCipher = open("ciphertext.txt", "a+")
pCipher.write("\n")
pCipher.write(str(ciphertext))
pCipher.close()
print("The message has been converted to ciphertext.")
print("Decrypting...")
decryptedText = decrypt(publicKey, privateKey, ciphertext)
pDecrypt = open("decrypted_message.txt", "w+")
pDecrypt.write("\n")
pDecrypt.write(str(decryptedText))
pDecrypt.close()
print("Your decrypted message is %s" %decryptedText)
print("The original message was %s" %plaintext)
if decryptedText == plaintext:
print("SUCCESS!!")
main();