python实现DES加密,解密

import base64
import binascii
from pyDes import des, ECB, PAD_PKCS5

def des_encrypt():
    '''
     DES 加密
    :DES_SECRET_KEY: 秘钥
    :content: 原始字符串
    :return: secret_content加密后字符串,base64
    '''

    DES_SECRET_KEY = 'MoveUnio'
    content = 'move_13761100490'

    des_obj = des(DES_SECRET_KEY, ECB, padmode=PAD_PKCS5)
    secret_content = str(base64.b64encode(des_obj.encrypt(content)), encoding='utf-8')
    secret_content = str(base64.b64encode(secret_content.encode('utf-8')), "utf-8")

    secret_content = secret_content.replace('+', '-', -1).replace('/', '_', -1).replace('=', '', -1)
    print(secret_content)

def des_descrypt():
    """
     DES 解密
    :DES_SECRET_KEY: 秘钥
    :secret_content: 加密后字符串,base64
    :return: content原始字符串
    """

    DES_SECRET_KEY = 'MoveUnio'
    secret_content = 'cnU2U3pNWXVzY095K3JlSDNDVGc3UHhJQlJVSTU1U3o'

    secret_content = secret_content.replace('-', '+', -1).replace('_', '/', -1)
    if len(secret_content) % 3 == 1:
        secret_content += "=="
    elif len(secret_content) % 3 == 2:
        secret_content += "="

    des_obj = des(DES_SECRET_KEY, ECB, padmode=PAD_PKCS5)
    content = bytes.decode(des_obj.decrypt(binascii.a2b_base64(base64.b64decode(str.encode(secret_content))), padmode=PAD_PKCS5))
    print(content)

if __name__== '__main__':
    des_encrypt()
    # des_descrypt()
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值