python 加密解密

Python 利用 pyDes对字符串进行解密

import binascii
from pyDes import des, CBC, PAD_PKCS5
 
def des_encrypt(s):
 """
 DES 加密
 :param s: 原始字符串
 :return: 加密后字符串,16进制
 """
 secret_key = '20171117'
 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 = '20171117'
 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
 
str_en = des_encrypt('zx')
print(str_en)
str_de = des_descrypt(str_en)
print(str_de

base64加密解密

import base64
# base64加密
def jiami(str):
    name = str
    name = name.encode('utf-8')
    a = base64.b64encode(name)
    print(a)
    # base64解密
def jiemi(str):
    b =  base64.b64decode(str)
    c = b.decode('utf-8')
    print(c)

str = 'MO2tV6y084G4jjAxODA3MjcxMDI0MTQ='
jiemi(str)

md5加密

# 生成MD5
import hashlib
def genearteMD5(str):
    # 创建md5对象
    hl = hashlib.md5()

    # Tips
    # 此处必须声明encode
    # 否则报错为:hl.update(str)    Unicode-objects must be encoded before hashing
    hl.update(str.encode(encoding='utf-8'))

    print('MD5加密前为 :' + str)
    print('MD5加密后为 :' + hl.hexdigest())

你可能感兴趣的:(python 加密解密)