-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSA_Algo.py
145 lines (99 loc) · 3.26 KB
/
RSA_Algo.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
import random
import sys
import math
from Gen_prime import *
from Crypto.Util import number
#Calculate GCD
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
#Calculate Multiplicative inverse
def multiplicative_inverse(e, phi):
d = 0
x1 = 0
x2 = 1
y1 = 1
temp_phi = phi
while e > 0:
temp1 = temp_phi//e
temp2 = temp_phi - temp1 * e
temp_phi = e
e = temp2
x = x2 - temp1 * x1
y = d - temp1 * y1
x2 = x1
x1 = x
d = y1
y1 = y
if temp_phi == 1:
return d + phi
#Generate Public and Private keys
def generate_key_pair(p, q):
n = p*q
phi = (p-1) * (q-1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = multiplicative_inverse(e, phi)
#Public key is (e, n) and private key is (d, n)
return ((e, n), (d, n))
#Generating ciphertext (Encryption)
def encrypt(pk, plaintext):
key, n = pk
cipher = pow(plaintext,key,n)
return cipher
#Generating plaintext (Decryption)
def decrypt(pk, ciphertext):
key, n = pk
plain = pow(ciphertext,key,n)
return plain
def main():
print("====== RSA Encryptor / Decrypter ======")
print(" ")
while True:
try:
p = generateLargePrime(10)
q = generateLargePrime(10)
except TypeError:
continue
else:
break
while(p==q):
q = generateLargePrime(10)
n = p*q
public, private = generate_key_pair(p, q)
print(" - P is: ",p)
print(" - Q is: ",q)
print(" - N is: ",n)
print(" - Public key is: ", public)
print(" - Private key is ", private)
message1 = int(input(" - Enter a message 1 to encrypt with your public key: "))
message2 = int(input(" - Enter a message 2 to encrypt with your public key: "))
actual_product = message1*message2
encrypted_msg1 = encrypt(public, message1)
encrypted_msg2 = encrypt(public, message2)
encrypted_msg3 = encrypt(public, actual_product) #RHS
product = encrypted_msg1*encrypted_msg2 #LHS
decrypted_msg1 = decrypt(private, encrypted_msg1)
decrypted_msg2 = decrypt(private, encrypted_msg2)
decrypted_msg3 = decrypt(private, product)
print("="*50)
print(" - Your encrypted message 1 is: ", encrypted_msg1)
print(" - Decrypting message with private key ", private, " . . .")
print(" - Your message is: ", decrypted_msg1)
print("="*50)
print(" - Your encrypted message 2 is: ", encrypted_msg2)
print(" - Decrypting message with private key ", private, " . . .")
print(" - Your message is: ", decrypted_msg2)
print("="*50)
print(" - Your message is (decrypted from product of ciphertext): ", decrypted_msg3)
print(" - YOur message product is: ",message1*message2)
if(decrypted_msg3 == message1*message2):
print(" - Homomorphic Multiplication Works!")
else:
print(" - Homomorphic Multiplication Doesnt Work.")
print("====================== END =======================")
main()