mirror of
https://github.com/daiyanan1992/qinglongtest
synced 2025-12-19 00:04:38 +08:00
更新rsa 支持分段加密
This commit is contained in:
@@ -8,7 +8,7 @@ try:
|
||||
from Crypto.PublicKey.RSA import importKey, construct
|
||||
from Crypto.Cipher import PKCS1_v1_5
|
||||
except:
|
||||
print("检测到还未安装 pycryptdemo 请按照md的方法安装")
|
||||
print("检测到还未安装 pycryptdome 请按照md的方法安装")
|
||||
exit(0)
|
||||
from base64 import b64encode
|
||||
|
||||
@@ -29,8 +29,21 @@ class RSA_Encrypt:
|
||||
return pubkey
|
||||
|
||||
def encrypt(self, data, b64=False):
|
||||
data = data.encode('utf-8')
|
||||
length = len(data)
|
||||
default_length = 117
|
||||
pub_key = importKey(self.key)
|
||||
cipher = PKCS1_v1_5.new(pub_key)
|
||||
rsa_text = cipher.encrypt(data.encode("utf8"))
|
||||
rsa_text = b64encode(rsa_text).decode() if b64 else rsa_text.hex()
|
||||
return rsa_text
|
||||
if length < default_length:
|
||||
rsa_text = cipher.encrypt(data)
|
||||
return b64encode(rsa_text).decode() if b64 else rsa_text.hex()
|
||||
offset = 0
|
||||
res = []
|
||||
while length - offset > 0:
|
||||
if length - offset > default_length:
|
||||
res.append(cipher.encrypt(data[offset:offset + default_length]))
|
||||
else:
|
||||
res.append(cipher.encrypt(data[offset:]))
|
||||
offset += default_length
|
||||
byte_data = b''.join(res)
|
||||
return b64encode(byte_data).decode() if b64 else byte_data.hex()
|
||||
|
||||
Reference in New Issue
Block a user