前言:公司的手机号加密更换了加密方法,这次改成 DES-ECB 加密了

代码操作

# -*- coding:utf-8 -*-

import base64
import json

from Crypto.Cipher import DES


class AesCrypter(object):
    """加密和解密工具类"""
    des_key = b"J2z0Wx7."
    block_size = DES.block_size
    pad_str = ['\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08']

    @classmethod
    def encrypt(cls, reqdata):
        """ 基于DES和base64的加密算法
            @:param reqdata 需要加密的请求数据
        """
        key = cls.des_key
        length = len(reqdata)
        if length < cls.block_size:
            add = cls.block_size - length
        elif length > cls.block_size:
            add = cls.block_size - (length % cls.block_size)
        else:
            add = 8
        reqdata = reqdata + (cls.pad_str[add - 1] * add)
        reqdata1 = bytes(reqdata, encoding="utf8")
        des = DES.new(key, DES.MODE_ECB)
        encrypt_data = des.encrypt(reqdata1)
        return base64.b64encode(encrypt_data)

    @classmethod
    def decrypt(cls, retdata):
        """DES解密
            @:param retdata: lakala reponse retData
        """
        key = cls.des_key
        debase64_data = base64.b64decode(retdata)
        des = DES.new(key, DES.MODE_ECB)
        decrypt_data = des.decrypt(debase64_data)
        return decrypt_data[:11]

if __name__ == '__main__':
    src='39999925802'
    encrypted  =  AesCrypter.encrypt(src)
    print('加密: ', encrypted)
    decrypt = AesCrypter.decrypt(encrypted)
    print('解密: ', decrypt)

运行后的结果