Python简单本地加解密

#coding=utf8
from Crypto.Cipher import DES

key = b'testuyun' # 密钥 8位或16位,必须为bytes


def pad(text):
"""
# 加密函数,如果text不是8的倍数【加密文本text必须为8的倍数!】,那就补足为8的倍数
:param text:
:return:
"""
while len(text) % 8 != 0:
text += ' '
return text


des = DES.new(key, DES.MODE_ECB) # 创建一个DES实例
text = '423413'
padded_text = pad(text)


encrypted_text = des.encrypt(padded_text.encode('utf-8')) # 加密
print repr(encrypted_text)


encrypted_text='xe8xf8x82xa9xdax019j4xafxb4xe3x8ax90wxf4'

# rstrip(' ')返回从字符串末尾删除所有字符串的字符串(默认空白字符)的副本
plain_text = des.decrypt(encrypted_text).decode().rstrip(' ') # 解密
print(plain_text)
原文地址:https://www.cnblogs.com/slqt/p/11078386.html