-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreceive.py
More file actions
51 lines (42 loc) · 1.46 KB
/
Copy pathreceive.py
File metadata and controls
51 lines (42 loc) · 1.46 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
from Crypto.Signature import PKCS1_PSS
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto.Cipher import PKCS1_OAEP
import binascii
import os
import sys
args = sys.argv
if len(args) != 4:
print("Usage: receive.py <symkey> <msg_crypt> <msg_sig> ")
exit(0)
symkey_crypt,msg_crypt,msg_sig = args[1],args[2],args[3]
def verify_sig(message,signature,key):
key = RSA.importKey(key)
h = SHA256.new(message)
verifier = PKCS1_PSS.new(key)
try:
verifier.verify(h, signature)
print( "The signature is authentic.")
except (ValueError, TypeError):
print( "The signature is not authentic.")
def RSAdecrypt(ciphertext,privatekey_der):
key = RSA.importKey(open(privatekey_der,'rb').read())
dsize = SHA256.digest_size
sentinel = Random.new().read(15+dsize) # Let's assume that average data length is 15
cipher = PKCS1_OAEP.new(key)
message = cipher.decrypt(ciphertext)
return message
def AES_decrypt(message, key):
iv = message[:16]
ctr = Counter.new(128, initial_value=int(binascii.hexlify(iv), 16))
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
return aes.decrypt(message[16:])
ciphertext = open(symkey_crypt,"rb").read()
k = RSAdecrypt(ciphertext,'rec_priv.der')
message = open(msg_crypt,"rb").read()
ct = AES_decrypt(message,k)
key = open('send_pub.der','rb').read()
verify_sig(ct,msg_sig,key)