-
Notifications
You must be signed in to change notification settings - Fork 3
/
RSA.py
39 lines (29 loc) · 839 Bytes
/
RSA.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
import rsa
def read_privkey(privkey_path):
with open(privkey_path, 'rb') as privfile:
p = privfile.read()
privkey = rsa.PrivateKey.load_pkcs1(p)
return privkey
def read_pubkey(privkey_path):
with open(privkey_path, 'rb') as privfile:
p = privfile.read()
privkey = rsa.PublicKey.load_pkcs1(p)
return privkey
# 生成密钥
def creat_key(Len):
pubkey, privkey = rsa.newkeys(Len)
pub = pubkey.save_pkcs1()
with open('data/CApubkey.pem', 'wb+')as f:
f.write(pub)
pri = privkey.save_pkcs1()
with open('data/CAprivkey.pem', 'wb+')as f:
f.write(pri)
return pubkey
# rsa加密
def rsaEncrypt(text, pubkey):
text = rsa.encrypt(text, pubkey)
return text
# rsa解密
def rsaDecrypt(text, privkey):
text = rsa.decrypt(text, privkey)
return text