[Asm] 纯文本查看 复制代码 import base64
from Crypto.Cipher import AES
import binascii
class PrpCrypt(object):
def __init__(self,password):
self.password = add_to_16(password)
def add_to_16(self,text):
while len(text) % 16 != 0:
text += '\0'
return text
def encrypt(self,data):
password = self.password
if isinstance(password, str):
password = password.encode('utf8')
bs = AES.block_size
pad = lambda s: s + (bs - len(s.encode('utf-8')) % bs) * chr(bs - len(s.encode('utf-8')) % bs)
cipher = AES.new(password, AES.MODE_ECB)
data = cipher.encrypt(pad(data).encode('utf8'))
# encrypt_data = binascii.b2a_hex(data) # 输出hex
encrypt_data = base64.b64encode(data) # 取消注释,输出Base64格式
return encrypt_data.decode('utf8')
def decrypt(self,decrData): # 解密
password = self.password
if isinstance(password, str):
password = password.encode('utf8')
cipher = AES.new(password, AES.MODE_ECB)
# plain_text = cipher.decrypt(binascii.a2b_hex(decrData)) # 解密hex
plain_text = cipher.decrypt(base64.decodebytes(decrData.encode("utf8"))) # 解密base64格式
return plain_text.decode('utf8').rstrip('\0')
def add_to_16(text):
while len(text) % 16 != 0:
text += '\0'
return text
def encrypt(data, password):
if isinstance(password, str):
password = password.encode('utf8')
bs = AES.block_size
pad = lambda s: s + (bs - len(s.encode('utf-8')) % bs) * chr(bs - len(s.encode('utf-8')) % bs)
cipher = AES.new(password, AES.MODE_ECB)
data = cipher.encrypt(pad(data).encode('utf8'))
# encrypt_data = binascii.b2a_hex(data) # 输出hex
encrypt_data = base64.b64encode(data) # 取消注释,输出Base64格式
return encrypt_data.decode('utf8')
def decrypt(decrData, password):
if isinstance(password, str):
password = password.encode('utf8')
cipher = AES.new(password, AES.MODE_ECB)
# plain_text = cipher.decrypt(binascii.a2b_hex(decrData)) # 解密hex
plain_text = cipher.decrypt(base64.decodebytes(decrData.encode("utf8"))) # 解密base64格式
return plain_text.decode('utf8').rstrip('\0')
if __name__ == '__main__':
data = '123456asd' # 待加密数据
# data = '中文测试!' # 待加密数据
password = '8NONwyJtHesysWpM' # 16,24,32位长的密码(**)
# password = 'iiiioooojjjjpppp' # 16,24,32位长的密码(**)
# password = add_to_16(password)
# encrypt_data = encrypt(data, password)
# print('加密前数据:{}\n======================='.format(data))
# print('加密后的数据:', encrypt_data) |