mirror of
https://github.com/daiyanan1992/qinglongtest
synced 2025-12-20 00:35:13 +08:00
更新rsa 支持分段加密
This commit is contained in:
@@ -12,7 +12,7 @@ pycryptdemo限制 同一个aes加密对象不能即加密又解密 所以当加
|
|||||||
try:
|
try:
|
||||||
from Crypto.Cipher import AES
|
from Crypto.Cipher import AES
|
||||||
except:
|
except:
|
||||||
print("检测到还未安装 pycryptdemo 请按照md的方法安装")
|
print("检测到还未安装 pycryptdome 请按照md的方法安装")
|
||||||
exit(0)
|
exit(0)
|
||||||
from binascii import b2a_hex, a2b_hex
|
from binascii import b2a_hex, a2b_hex
|
||||||
from base64 import b64encode, b64decode
|
from base64 import b64encode, b64decode
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pycryptdemo限制 同一个aes加密对象不能即加密又解密 所以当加
|
|||||||
try:
|
try:
|
||||||
from Crypto.Cipher import AES, DES, DES3
|
from Crypto.Cipher import AES, DES, DES3
|
||||||
except:
|
except:
|
||||||
print("检测到还未安装 pycryptdemo 请按照md的方法安装")
|
print("检测到还未安装 pycryptdome 请按照md的方法安装")
|
||||||
exit(0)
|
exit(0)
|
||||||
from binascii import b2a_hex, a2b_hex
|
from binascii import b2a_hex, a2b_hex
|
||||||
from base64 import b64encode, b64decode
|
from base64 import b64encode, b64decode
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ try:
|
|||||||
from Crypto.PublicKey.RSA import importKey, construct
|
from Crypto.PublicKey.RSA import importKey, construct
|
||||||
from Crypto.Cipher import PKCS1_v1_5
|
from Crypto.Cipher import PKCS1_v1_5
|
||||||
except:
|
except:
|
||||||
print("检测到还未安装 pycryptdemo 请按照md的方法安装")
|
print("检测到还未安装 pycryptdome 请按照md的方法安装")
|
||||||
exit(0)
|
exit(0)
|
||||||
from base64 import b64encode
|
from base64 import b64encode
|
||||||
|
|
||||||
@@ -29,8 +29,21 @@ class RSA_Encrypt:
|
|||||||
return pubkey
|
return pubkey
|
||||||
|
|
||||||
def encrypt(self, data, b64=False):
|
def encrypt(self, data, b64=False):
|
||||||
|
data = data.encode('utf-8')
|
||||||
|
length = len(data)
|
||||||
|
default_length = 117
|
||||||
pub_key = importKey(self.key)
|
pub_key = importKey(self.key)
|
||||||
cipher = PKCS1_v1_5.new(pub_key)
|
cipher = PKCS1_v1_5.new(pub_key)
|
||||||
rsa_text = cipher.encrypt(data.encode("utf8"))
|
if length < default_length:
|
||||||
rsa_text = b64encode(rsa_text).decode() if b64 else rsa_text.hex()
|
rsa_text = cipher.encrypt(data)
|
||||||
return rsa_text
|
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