· 9 years ago · Nov 25, 2016, 04:28 PM
1#!/usr/bin/env python
2
3import os
4import subprocess
5import sys
6import time
7from binascii import hexlify, unhexlify
8from pyelliptic import Cipher, ECC
9from pyelliptic import hash as _hash
10import fileinput
11
12## Generating a secret key from the public and private keys using ECDH ##
13def generate_shared_key():
14 print("Generating shared key from ECDH and ECDSA")
15 cmd = "openssl pkeyutl -derive -inkey pc_private_key.pem -peerkey ardi_public_key.pem"
16 p = subprocess.Popen(cmd, stdout = subprocess.PIPE, shell =True)
17 (output, err) = p.communicate()
18 print("Shared key generation - OK!")
19 return output
20
21## main ##
22if __name__ == '__main__':
23
24 secret_key = generate_shared_key()
25 print(hexlify(secret_key))
26
27 print("Data decryption using AES_256_CBC")
28 ciphername = "aes-256-cbc"
29 iv_hex = b"000102030405060708090A0B0C0D0E0F"
30 iv = unhexlify(iv_hex)
31
32
33 while(True):
34 print "Enter Text to Decrypt :\n"
35 for line in fileinput.input():
36 print line
37 print len(line)
38 enc = unhexlify(line[0:len(line)-1])
39 ctx = Cipher(secret_key, iv, 0, ciphername=ciphername)
40 deciphered_txt = ctx.ciphering(enc)
41 print (deciphered_txt)