-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths2.py
44 lines (34 loc) · 956 Bytes
/
s2.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
# server
# coding:utf-8
import socket
import rsa
# load公钥和密钥
with open('public.pem') as publickfile:
p = publickfile.read()
pubkey = rsa.PublicKey.load_pkcs1(p)
with open('private.pem') as privatefile:
p = privatefile.read()
privkey = rsa.PrivateKey.load_pkcs1(p)
# 用公钥加密,用私钥签名认证
reply = 'hi,client'
abstract = 'server'
crypto = rsa.encrypt(reply, pubkey)
signatures = rsa.sign(abstract, privkey, 'SHA-1')
address = ('127.0.0.1', 13150)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(5)
print 'waiting for connection'
conn, addr = s.accept()
print 'got connected from',addr
conn.send(crypto)
conn.send(signatures)
ra = conn.recv(512)
signaturec = conn.recv(512)
conn.close()
s.close()
# 用私钥解密、用公钥验证签名
message = rsa.decrypt(ra, privkey)
r = rsa.verify('client',signaturec, pubkey)
print r
print 'client:',message