pyDES实现DES加解密
程序员文章站
2024-03-14 14:21:40
...
from pyDes import des, CBC, PAD_PKCS5
import binascii
# 秘钥
KEY='mHAxsLYz'
def des_encrypt(s):
"""
DES 加密
:param s: 原始字符串
:return: 加密后字符串,16进制
"""
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
en = k.encrypt(s, padmode=PAD_PKCS5)
return binascii.b2a_hex(en)
def des_descrypt(s):
"""
DES 解密
:param s: 加密后的字符串,16进制
:return: 解密后的字符串
"""
secret_key = KEY
iv = secret_key
k = des(secret_key, CBC, iv, pad=None, padmode=PAD_PKCS5)
de = k.decrypt(binascii.a2b_hex(s), padmode=PAD_PKCS5)
return de
s='123456789987654321'
enc=des_encrypt(s)
print(enc)
des=des_descrypt(enc)
print(des)
上一篇: SM4加密解密