python DES加密
作者:
盖码范 | 来源:发表于
2024-04-01 19:17 被阅读0次from Crypto.Cipher import DES
from Crypto.Util.Padding import pad
import base64
def des_encrypt(data_to_encrypt, key_string):
# 将密钥转换为UTF-8编码的字节串,并确保长度为8字节
key = key_string.encode('utf-8')[:8]
# 创建一个新的DES cipher对象,使用ECB模式
cipher = DES.new(key, DES.MODE_ECB)
# 对数据进行UTF-8编码然后进行PKCS7填充
padded_data = pad(data_to_encrypt.encode('utf-8'), DES.block_size)
# 加密数据
encrypted_data = cipher.encrypt(padded_data)
# 将加密后的数据转换为十六进制字符串
encrypted_hex = encrypted_data.hex()
return encrypted_hex
# 使用示例
data = '{"data":"cxqFJ61G_8RC0HlZncQ2vED5j6jc"}'
key = "LheUQWCmdILhe"
encrypted_hex = des_encrypt(data, key)
print(encrypted_hex)
本文标题:python DES加密
本文链接:https://www.haomeiwen.com/subject/trpitjtx.html
网友评论