-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.py
47 lines (35 loc) · 1.15 KB
/
tools.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
# coding:utf-8
import rsa
def create_genisus_keypair():
# 第一个节点的密钥对
pubkey, privkey = rsa.newkeys(1024)
with open('genisus_public.pem', 'w+') as f:
f.write(pubkey.save_pkcs1().decode())
with open('genisus_private.pem', 'w+') as f:
f.write(privkey.save_pkcs1().decode())
# 导入密钥
with open('genisus_private.pem', 'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
with open('genisus_public.pem', 'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
# 明文
message = 'hello world'
# 公钥加密
crypto = rsa.encrypt(message.encode(), pubkey)
# 私钥解密
message = rsa.decrypt(crypto, privkey).decode()
print(message)
# 明文
message = 'hello world'
# 导入密钥
with open('genisus_private.pem', 'r') as f:
privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
with open('genisus_public.pem', 'r') as f:
pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
# 私钥签名
signature = rsa.sign(message.encode(), privkey, 'SHA-1')
# 公钥验证
try:
rsa.verify(message.encode(), signature, pubkey)
except rsa.pkcs1.VerificationError:
print 'invalid'