-
Notifications
You must be signed in to change notification settings - Fork 0
/
reciever.py
118 lines (75 loc) · 2.02 KB
/
reciever.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
import gmpy2 as gmp
from gmpy2 import mpz
from Vig import *
from RSAEnc import *
from RSADec import *
from numNwords import *
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 readCipherText():
global cipher_path
data = ""
with open(cipher_path, "r") as file:
data = file.read().replace('\n', ' ')
return data
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 init():
print("Recieving message...")
recieveMsg(2,1)
print("Recieving Completed")
print("_"*30)
cipher_path = "cipher"
if __name__ == "__main__":
cipher_path = sys.argv[1]
init()
# print(sendMsg(1,2))
# print(recieveMsg(2,1))