欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python实现DES加密,解密

程序员文章站 2024-03-14 13:59:34
...
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()