-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (118 loc) · 3.24 KB
/
main.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
import gmpy2 as gmp
from gmpy2 import mpz
from Vig import *
from RSAEnc import *
from RSADec import *
from numNwords import *
import CA
import os, sys
def getCAPublicKey():
with open("ca_public") as fp:
lines = fp.readlines()
line = lines[0]
e,n = line.split()
return mpz(e), mpz(n)
def getUserPublicKey(user):
E, N = getCAPublicKey()
with open("pub_dir") as fp:
lines = fp.readlines()
for line in lines:
u, enc_e, enc_n = line.split()
u = int(u)
enc_e = mpz(enc_e)
enc_n = mpz(enc_n)
if user == u:
e = gmp.powmod(enc_e, E, N)
n = gmp.powmod(enc_n, E, N)
return (e, n)
print(f"user {user} doesn't exist")
return False
def getUserPrivateKey(user):
file_name = str(user) + "_private"
with open(file_name) as file:
data = file.read()
d, n = data.split()
return mpz(d),mpz(n)
def readPlainText():
data = ""
plain = ""
with open("plain", "r") as file:
data = file.read().replace('\n', ' ').lower()
for el in data:
if ord('a') <= ord(el) <= ord('z'):
plain+= el
return plain
def readCipherText():
data = ""
with open("cipher", "r") as file:
data = file.read().replace('\n', ' ')
return data
def getVigKey():
with open("vig_key", "r") as file:
data = file.read().replace('\n', ' ')
return data
def sendMsg(u1, u2):
# print(u1, u2)
print("Reading Plain Text file...")
data = readPlainText()
print("Reading Vignere Key file...")
Vkey = getVigKey()
u1_d, u1_n = getUserPrivateKey(u1)
u2_e, u2_n = getUserPublicKey(u2)
v_data = Vencrypt(data, Vkey)
print("File encrypted with Vignere Cipher")
Vkey_len = len(Vkey)
Vkey_len_word = num2word(Vkey_len)
# message M before sending
M = Vkey_len_word + "{" + Vkey + "{" + v_data
print("Key length, Vignere Key and Vignere Cipher is appended")
DS_M = rsaEncrypt(M, u1_d, u1_n)
print("Digital Signature is generated using sender's private key")
with open("SenderVignere", "w") as file:
file.write(M)
# encrypt using rsa
# print("u2_n", u2_n)
C = rsaEncrypt(DS_M, u2_e, u2_n)
print("Encrypted with reciever's public key")
with open("cipher", "w") as file:
file.write(C)
print("Cipher Text is written to file")
return True
def recieveMsg(u2, u1):
# print(u1, u2)
print("Cipher file is reading")
data = readCipherText()
# Vkey_len_word = num2word(Vkey_len)
u2_d, u2_n = getUserPrivateKey(u2)
u1_e, u1_n = getUserPublicKey(u1)
DM = rsaDecrypt(data, u2_d, u2_n)
print("Decrypted with reciever's private key")
M = rsaDecrypt(DM, u1_e, u1_n)
print("Decrypted with sender's public key")
with open("RecieverVignere", "w") as file:
file.write(M)
Vkey_len_word, Vkey, Vcipher = M.split("{")
print("Vignere Key and Vgnere Cipher extracted")
Vdec = Vdecrypt(Vcipher, Vkey)
print("Vignere Cipher decrypted")
with open("decrypted_vignere_key", "w") as file:
file.write(Vkey)
with open("decrypted_message", "w") as file:
file.write(Vdec)
print("Decrypted text is saved to file")
print("Decrypted key is saved to file")
return True
def main():
init()
def init():
print("Sending message...")
sendMsg(1,2)
print("Sending Completed")
print("_"*30)
print("Recieving message...")
recieveMsg(2,1)
print("Recieving Completed")
print("_"*30)
init()
# print(sendMsg(1,2))
# print(recieveMsg(2,1))