ASE加密
一、安装
官网下载: https://www.dlitz.net/software/pycrypto/
如果是linux系统,PyCrypto的安装非常简单,解压直接安装即可:
python setup.py build
python setup.py install
如果是windows系统,只要安装 pycryptodome 模块即可
pip3 install pycryptodome
二、在python中的使用
- 在对字符进行加密时,密码和明文长度必须为16,24,或32。
- 因此要对密码和明文进行预处理,确保密码长度为16,24或32,明文长度为16,24或32的整数倍
################################## 加密 ##################################
from Crypto.Cipher import AES
def encrypt(message):
key = b'dfdsdfsasdfdsdfs'
cipher = AES.new(key, AES.MODE_CBC, key)
ba_data = bytearray(message,encoding='utf-8')
v1 = len(ba_data)
v2 = v1 % 16
if v2 == 0:
v3 = 16
else:
v3 = 16 - v2
for i in range(v3):
ba_data.append(v3)
final_data = ba_data
msg = cipher.encrypt(final_data) # 要加密的字符串,必须是16个字节或16个字节的倍数
return msg
# ############################## 解密 #####################################
from Crypto.Cipher import AES
def decrypt(msg):
key = b'dfdsdfsasdfdsdfs'
cipher = AES.new(key, AES.MODE_CBC, key)
result = cipher.decrypt(msg) # result = b'\xe8\xa6\x81\xe5\x8a\xa0\xe5\xaf\x86\xe5\x8a\xa0\xe5\xaf\x86\xe5\x8a\xa0sdfsd\t\t\t\t\t\t\t\t\t'
data = result[0:-result[-1]]
return str(data,encoding='utf-8')
msg = encrypt('你好好爱好爱好sss')
res = decrypt(msg)
print(res)
博客内容仅供参考,部分参考他人优秀博文,仅供学习使用