-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubprocess_server_func.py
More file actions
95 lines (73 loc) · 2.86 KB
/
subprocess_server_func.py
File metadata and controls
95 lines (73 loc) · 2.86 KB
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
# -*- coding: utf-8 -*-
# MDA Network Project
# Ring decentralized P2P network for secure instance-messaging
# By Amr Abdelnaser C&E Engineer, Qatar
# Begin in 31/12/2015 04:23 PM
# This file contain the NodeServer - ( Server-side subprocess functions )
from Crypto.PublicKey import RSA
from Crypto.Util import randpool
from Crypto.Cipher import PKCS1_OAEP
import os, sys
import json
import base64
import time
class subcrypto:
def __init__(self):
self.RSAKey = None
def generateRSAkey(self):
#generate the RSA key ( for the AES key )
blah = randpool.RandomPool()
self.RSAKey = RSA.generate(1024, blah.get_bytes)
RSAPubKey = self.RSAKey.publickey()
pup = self.RSAKey.exportKey(format='PEM')
#send the public key over
race1 = base64.b64encode(pup)
go = json.dumps(race1)
#print " sending the public-key", pup
print go
def cryptoserver(self):
from Crypto.Cipher import AES
rcstring = ''
while True:
try:
rcstring = raw_input()
#rcstring += buf
except (EOFError):
break #end of file reached
#print "Encrypted msg :: ", rcstring
sys.stdout.flush()
#encmessage is the cipher text
# getting the AES key to decrypt the msg
# print " Received total message : ", rcstring
secretkey, encmessage, secretsign, race1 = rcstring.split(':', 3)
encmsg = base64.b64decode(encmessage)
seckey = base64.b64decode(secretkey)
signature = base64.b64decode(secretsign)
publickey = base64.b64decode(race1)
pup2 = RSA.importKey(publickey)
self.RSAKey = pup2
rsakey = PKCS1_OAEP.new(self.RSAKey)
# Verifing the msg before decrypt it !
from Crypto.Signature import PKCS1_PSS
from Crypto.Hash import SHA256
h = SHA256.new()
h.update(encmsg)
verifier = PKCS1_PSS.new(self.RSAKey)
if verifier.verify(h, signature):
iv = encmsg[:16]
encmsg = encmsg[16:]
AESkey = rsakey.decrypt(seckey)
cipher = AES.new(AESkey, AES.MODE_CFB, iv)
plaintxt = cipher.decrypt(encmsg)
plaintxt2 = plaintxt.decode("utf8")
print plaintxt2
sys.stdout.flush()
else:
print "The signature is not authentic."
sys.stdout.flush()
if __name__ == '__main__':
sub = subcrypto()
if sys.argv[1] == '1':
sub.generateRSAkey()
elif sys.argv[1] == '2':
sub.cryptoserver()