[Python] 纯文本查看 复制代码 import base64
import ast
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
class AesCBC:
key = '2v4T2aTDnXzkCEV7'.encode('utf-8') #16位
iv = b'abcdefghijklmnop' # 16位
# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(self, text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
def encrypt(self, text) -> str:
# 加密函数
mode = AES.MODE_CBC
text = self.add_to_16(text)
cryptos = AES.new(self.key, mode, self.iv)
cipher_text = cryptos.encrypt(text)
# 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
print(cipher_text)
return str(base64.b64encode(b2a_hex(cipher_text)), encoding='utf-8')
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(self, text):
text = base64.b64decode(text)
mode = AES.MODE_CBC
cryptos = AES.new(self.key, mode, self.iv)
plain_text = cryptos.decrypt(a2b_hex(text))
return bytes.decode(plain_text).rstrip('\0')
if __name__ == '__main__':
k = '1667822543031;3033_6282_254;2v4T2aTDnXzkCEV7'
e = AesCBC().encrypt(k) # 加密
d = AesCBC().decrypt(e) # 解密
print("加密:", e)
print("解密:", d)
# print(type(d))
# print(ast.literal_eval(d)['username'])
已知
key = '2v4T2aTDnXzkCEV7'.encode('utf-8') #16位
iv = b'abcdefghijklmnop' # 16位
加密的结果是ODdhMzg1MGE5N2UyNDBlN2FlZjM4MzA3YzhkY2JkZDc0MjExYjZmYjAwZDdmNTM3OGFiNWY4M2IxODcyMmNjYjRhMDQ2OWM3YjgzYzkxZmRiMzczNThkYjdmMGNlYzk0
但是,
我在网页 https://www.qtool.net/aes 加密得到的字符串是 h6OFCpfiQOeu84MHyNy910IRtvsA1/U3irX4OxhyLMubZS/Ofpyb0kP/GhSTRTPP
怎么改我上面的代码,能得到网页的加密效果呢
|