forked from NehaNagaraja/CMPE-209---Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisor.py
129 lines (112 loc) · 5.96 KB
/
supervisor.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
import socket
import sys
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
from Crypto import Random
from base64 import b64encode, b64decode
import sqlite3, hashlib, os
hash = "SHA-256"
def newkeys(keysize):
random_generator = Random.new().read
key = RSA.generate(keysize, random_generator)
private, public = key, key.publickey()
return public, private
def importKey(externKey):
return RSA.importKey(externKey)
def getpublickey(priv_key):
return priv_key.publickey()
def encrypt(message, pub_key):
#RSA encryption protocol according to PKCS#1 OAEP
cipher = PKCS1_OAEP.new(pub_key)
return cipher.encrypt(message)
def decrypt(ciphertext, priv_key):
#RSA encryption protocol according to PKCS#1 OAEP
cipher = PKCS1_OAEP.new(priv_key)
return cipher.decrypt(ciphertext)
def sign(message, priv_key, hashAlg="SHA-256"):
global hash
hash = hashAlg
signer = PKCS1_v1_5.new(priv_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.sign(digest)
def verify2(message, signature, pub_key):
signer = PKCS1_v1_5.new(pub_key)
if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
digest = SHA256.new()
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.verify(digest, signature)
msg = b'I am the supervisor'
keysize = 1024
(public, private) = newkeys(keysize)
publickeyD='-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHt/J2Z9QCo7jjgxUA4GdDffol\nJExrfgDJHzaKVWKga4fnQl4lh7Rve2dBNkq7QSrjFOjG0J+h1YMpcrFyQGtPmaVb\nUeTepTvUcqqY7M+O+3kkCkxAnf7sM5XrbjFQb7YyFw0hqa8QsNj+UVK2ffKbJ2Pi\nX9x8ubt8JY4DNTa2HQIDAQAB\n-----END PUBLIC KEY-----'
publickeysup='-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHt/J2Z9QCo7jjgxUA4GdDffol\nJExrfgDJHzaKVWKga4fnQl4lh7Rve2dBNkq7QSrjFOjG0J+h1YMpcrFyQGtPmaVb\nUeTepTvUcqqY7M+O+3kkCkxAnf7sM5XrbjFQb7YyFw0hqa8QsNj+UVK2ffKbJ2Pi\nX9x8ubt8JY4DNTa2HQIDAQAB\n-----END PUBLIC KEY-----'
privatekeySup='-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQDHt/J2Z9QCo7jjgxUA4GdDffolJExrfgDJHzaKVWKga4fnQl4l\nh7Rve2dBNkq7QSrjFOjG0J+h1YMpcrFyQGtPmaVbUeTepTvUcqqY7M+O+3kkCkxA\nnf7sM5XrbjFQb7YyFw0hqa8QsNj+UVK2ffKbJ2PiX9x8ubt8JY4DNTa2HQIDAQAB\nAoGACju0oriDNndpG881nkvhPqYP0SNg/wj/xU5iHwhs+0dHOWQ/KrEfX671BXRg\ns+OM7QG/Q6Cg7UvhYphL20zNsrk+lMZj//ZRSrlLIbgMHr515qH5gyanDaUQ9NuH\nOwDZLsUO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6JQOFYY2nFIjhcip\nEIMqKIf/M9ghXeETrftIkYSz5EMbD8/zyQJBAK0ZNFHbxtcAIKIt6jnNwbdnMHmX\n+EADYPMTE3fvsv08L24hunMcegXyuA27IgwsDYrNVeJ47esMlNuqOJ3qACECQGV2\nfy/rIsRdQEQNTmSQ3KW1s8LvcDfNDwsM21zV+hq7/Oe0yQhSCYzHxBm/aZZnROYJ\nhP334KiXbDfLBOtJOzECQFLn4KC/irt7pThqMv+vcUIQjRvs6A/clOJd6Hy/0RX3\npC4FjdftyUKGwAyNzf0njsyNH5kfQz5mizKkBceDqII=\n-----END RSA PRIVATE KEY-----'
publicD = importKey(publickeyD)
publicS = importKey(publickeysup)
privateS= importKey(privatekeySup)
privateSupS= importKey(privateSupSign)
encrypted = b64encode(encrypt(msg, publicD))
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Connect the socket to the port where the server is listening
server_address = ('localhost', 10000)
print (sys.stderr, 'connecting to %s port %s' % server_address)
sock.connect(server_address)
try:
# Send data
sock.send(encrypted)
# Look for the response
amount_received = 0
amount_expected = 1024
#while(amount_received < amount_expected):
data = sock.recv(1024)
print (sys.stderr, 'received "%s"' % data)
dataD = decrypt(b64decode(data), privateS)
print(dataD)
message = ' '
listmsg = []
orderid = raw_input("Which order do you want to approve? ")
sock.send(orderid.encode())
answer= raw_input("Do you want to sign it? ")
if (answer=='yes' or answer=='Yes'):
with sqlite3.connect('database.cmpe_web') as conn:
cur = conn.cursor()
cur.execute("SELECT userId, timestamp FROM orders WHERE orderid = " + str(orderid))
orderData = cur.fetchone()
userId = orderData[0]
timestamp = orderData[1]
message = str(userId)+str(orderid)+str(timestamp)
message = message.encode()
conn.close()
signaturesup = b64encode(sign(message, privateSupS, "SHA-256"))
sock.send(signaturesup)
else:
print("exiting application")
sock.close()
done=raw_input("Are you done?")
sock.send(done.encode())
finally:
if (done=='done' or done=='Done'):
print ('closing socket')
sock.close()