from Crypto.Cipher import DES
from Crypto.Util.Padding import pad, unpad
import json
# 创建 DES 加密对象
key = b'123456' # 8字节的密钥,注意必须为字节类型
cipher = DES.new(key, DES.MODE_ECB)
# 加密
def encrypt_data(data):
plaintext = json.dumps(data).encode('utf-8')
padded_plaintext = pad(plaintext, DES.block_size)
ciphertext = cipher.encrypt(padded_plaintext)
return ciphertext
# 解密
def decrypt_data(ciphertext):
padded_plaintext = cipher.decrypt(ciphertext)
plaintext = unpad(padded_plaintext, DES.block_size)
data = json.loads(plaintext.decode('utf-8'))
return data
# 测试
account_list = {
'Togin_name': 'account',
'password': 'pwd',
'ume': 'n_time'
}
encrypted_data = encrypt_data(account_list)
decrypted_data = decrypt_data(encrypted_data)
print("原始数据:", account_list)
print("加密后数据:", encrypted_data)
print("解密后数据:", decrypted_data)
运行结果: