DES加解密(python3实现)
安装工具包:pip install pyDes
加解密实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #!/usr/bin/env python3 # -*-coding: utf-8 -*- """ @author: kyle @time: 2019/7/9 17:15 """ import binascii from pyDes import des, PAD_PKCS5, ECB
def des_en(msg): """ DES 加密,分组方式 ECB,填充方式 PAD_PKCS5 :param msg: msg待加密字符串 :return: 返回加密后字符串,16进制 """ secret_key = 'IuFWKUut' iv =secret_key key = des(secret_key, ECB, iv, pad=None, padmode=PAD_PKCS5) entrymsg = key.encrypt(msg, padmode=PAD_PKCS5) return binascii.b2a_hex(entrymsg)
def des_des(msg): """ DES 解密,分组方式 ECB,填充方式 PAD_PKCS5 :param msg: 16进制加密码 :return: 解密字符串 """ secret_key = 'IuFWKUut' iv = secret_key key = des(secret_key, ECB, iv, pad=None, padmode=PAD_PKCS5) desmsg = key.decrypt(binascii.a2b_hex(msg), padmode=PAD_PKCS5) return desmsg
str_en = des_en('DESTest') print("DES加密DESTest结果为: " + str(str_en))
str_des = des_des(str_en) print("DES解密结果为: " + str(str_des))
|
结果输出:
1 2
| DES加密DESTest结果为: b'9811175955dda6f9' DES解密结果为: b'DESTest'
|