perf: 优化超长文本rsa加密解密 (#312)
This commit is contained in:
parent
c4c4934932
commit
a788d8f3b8
@ -62,18 +62,6 @@ def get_key_pair_by_sql():
|
|||||||
return system_setting.meta
|
return system_setting.meta
|
||||||
|
|
||||||
|
|
||||||
# def get_key_pair():
|
|
||||||
# if not os.path.exists("/opt/maxkb/conf/receiver.pem"):
|
|
||||||
# kv = generate()
|
|
||||||
# private_file_out = open("/opt/maxkb/conf/private.pem", "wb")
|
|
||||||
# private_file_out.write(kv.get('value'))
|
|
||||||
# private_file_out.close()
|
|
||||||
# receiver_file_out = open("/opt/maxkb/conf/receiver.pem", "wb")
|
|
||||||
# receiver_file_out.write(kv.get('key'))
|
|
||||||
# receiver_file_out.close()
|
|
||||||
# return {'key': open("/opt/maxkb/conf/receiver.pem").read(), 'value': open("/opt/maxkb/conf/private.pem").read()}
|
|
||||||
|
|
||||||
|
|
||||||
def encrypt(msg, public_key: str | None = None):
|
def encrypt(msg, public_key: str | None = None):
|
||||||
"""
|
"""
|
||||||
加密
|
加密
|
||||||
@ -111,28 +99,27 @@ def rsa_long_encrypt(message, public_key: str | None = None, length=200):
|
|||||||
:param length: 1024bit的证书用100, 2048bit的证书用 200
|
:param length: 1024bit的证书用100, 2048bit的证书用 200
|
||||||
:return: 加密后的数据
|
:return: 加密后的数据
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# 读取公钥
|
# 读取公钥
|
||||||
if public_key is None:
|
if public_key is None:
|
||||||
public_key = get_key_pair().get('key')
|
public_key = get_key_pair().get('key')
|
||||||
cipher = PKCS1_cipher.new(RSA.importKey(extern_key=public_key,
|
cipher = PKCS1_cipher.new(RSA.importKey(extern_key=public_key,
|
||||||
passphrase=secret_code))
|
passphrase=secret_code))
|
||||||
# 处理:Plaintext is too long. 分段加密
|
# 处理:Plaintext is too long. 分段加密
|
||||||
if len(message) <= length:
|
if len(message) <= length:
|
||||||
# 对编码的数据进行加密,并通过base64进行编码
|
# 对编码的数据进行加密,并通过base64进行编码
|
||||||
result = base64.b64encode(cipher.encrypt(message.encode('utf-8')))
|
result = base64.b64encode(cipher.encrypt(message.encode('utf-8')))
|
||||||
else:
|
else:
|
||||||
rsa_text = []
|
rsa_text = []
|
||||||
# 对编码后的数据进行切片,原因:加密长度不能过长
|
# 对编码后的数据进行切片,原因:加密长度不能过长
|
||||||
for i in range(0, len(message), length):
|
for i in range(0, len(message), length):
|
||||||
cont = message[i:i + length]
|
cont = message[i:i + length]
|
||||||
# 对切片后的数据进行加密,并新增到text后面
|
# 对切片后的数据进行加密,并新增到text后面
|
||||||
rsa_text.append(cipher.encrypt(cont.encode('utf-8')))
|
rsa_text.append(cipher.encrypt(cont.encode('utf-8')))
|
||||||
# 加密完进行拼接
|
# 加密完进行拼接
|
||||||
cipher_text = b''.join(rsa_text)
|
cipher_text = b''.join(rsa_text)
|
||||||
# base64进行编码
|
# base64进行编码
|
||||||
result = base64.b64encode(cipher_text)
|
result = base64.b64encode(cipher_text)
|
||||||
return result.decode()
|
return result.decode()
|
||||||
|
|
||||||
|
|
||||||
def rsa_long_decrypt(message, pri_key: str | None = None, length=256):
|
def rsa_long_decrypt(message, pri_key: str | None = None, length=256):
|
||||||
@ -143,12 +130,11 @@ def rsa_long_decrypt(message, pri_key: str | None = None, length=256):
|
|||||||
:param length : 1024bit的证书用128,2048bit证书用256位
|
:param length : 1024bit的证书用128,2048bit证书用256位
|
||||||
:return: 解密后的数据
|
:return: 解密后的数据
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if pri_key is None:
|
if pri_key is None:
|
||||||
pri_key = get_key_pair().get('value')
|
pri_key = get_key_pair().get('value')
|
||||||
cipher = PKCS1_cipher.new(RSA.importKey(pri_key, passphrase=secret_code))
|
cipher = PKCS1_cipher.new(RSA.importKey(pri_key, passphrase=secret_code))
|
||||||
base64_de = base64.b64decode(message)
|
base64_de = base64.b64decode(message)
|
||||||
res = []
|
res = []
|
||||||
for i in range(0, len(base64_de), length):
|
for i in range(0, len(base64_de), length):
|
||||||
res.append(cipher.decrypt(base64_de[i:i + length], 0))
|
res.append(cipher.decrypt(base64_de[i:i + length], 0))
|
||||||
return b"".join(res).decode()
|
return b"".join(res).decode()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user