0%

python rsa

生成RSA公钥、私钥

1
2
3
key = RSA.generate(2048)
private_key = key.exportKey().decode()
public_key = key.publickey().exportKey().decode()

签名与验签

1
2
3
4
5
6
7
8
9
# 签名
h = SHA.new(message)
signer = Signature.new(RSA.importKey(private_key))
sign = base64.b64encode(signer.sign(h)).decode()
# 验签
sign = base64.b64decode(sign)
h = SHA.new(message)
verifier = Signature.new(RSA.importKey(public_key))
verify = verifier.verify(h, sign)

加密与解密

1
2
3
4
5
6
7
8
# 加密
cipher = Cipher.new(RSA.importKey(public_key))
ciphertext = base64.b64encode(cipher.encrypt(message)).decode()
# 解密
ciphertext = base64.b64decode(ciphertext)
sentinel = Random.new().read(SHA.digest_size)
cipher = Cipher.new(RSA.importKey(private_key))
message = cipher.decrypt(ciphertext, sentinel).decode()

完整代码如下

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
# encoding: utf-8
import base64

from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5 as Cipher
from Crypto.Hash import SHA
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5 as Signature


class RSAUtils(object):

@classmethod
def generate_keys(cls):
"""
:return: private_key and public_key
:rtype: (str, str)
"""
key = RSA.generate(2048)
private_key = key.exportKey().decode()
public_key = key.publickey().exportKey().decode()
return private_key, public_key

@classmethod
def sign(cls, message, private_key):
"""
:type message: str | bytes
:type private_key: str | bytes
:rtype: str
"""
if isinstance(message, str):
message = message.encode()
h = SHA.new(message)
signer = Signature.new(RSA.importKey(private_key))
return base64.b64encode(signer.sign(h)).decode()

@classmethod
def verify(cls, message, sign, public_key):
"""
:type message: str | bytes
:type sign: str | bytes
:type public_key: str | bytes
:rtype: bool
"""
if isinstance(message, str):
message = message.encode()
sign = base64.b64decode(sign)
h = SHA.new(message)
verifier = Signature.new(RSA.importKey(public_key))
return verifier.verify(h, sign)

@classmethod
def encrypt(cls, message, public_key):
"""
:type message: str | bytes
:type public_key: str | bytes
:rtype: str
"""
if isinstance(message, str):
message = message.encode()
cipher = Cipher.new(RSA.importKey(public_key))
return base64.b64encode(cipher.encrypt(message)).decode()

@classmethod
def decrypt(cls, ciphertext, private_key):
"""
:type ciphertext: str | bytes
:type private_key: str | bytes
:rtype: str
"""
ciphertext = base64.b64decode(ciphertext)
sentinel = Random.new().read(SHA.digest_size)
cipher = Cipher.new(RSA.importKey(private_key))
return cipher.decrypt(ciphertext, sentinel).decode()