pycryptodome des加解密学习记录

安装pycrytodome

pip install pycrytodome

cryto 要C++环境,不想装vs studio就放弃了,装pycrytodome代替

DES ECB pkcs7加密举例

DES不安全不建议用
import binascii
import json
from Crypto.Cipher import DES
from Crypto.Util.Padding import pad

jsonfile = {
  "app": "com.knowyou.perception",
  "code": 200,
  "duration": "161ms",
  "headers": {
    "Server": "nginx/1.9.8",
    "Date": "Mon, 25 Jul 2022 04:04:05 GMT",
    "Content-Type": "application/json",
    "Transfer-Encoding": "chunked",
    "Connection": "keep-alive",
    "Vary": "Access-Control-Request-Headers"
  },
  "message": "",
  "protocol": "HTTP/1.1",
  "remoteIp": "36.112.158.205",
  "remotePort": 80,
  "sessionId": "c61e8820-a1b7-4ddb-bc1a-a942c2eed3e3",
  "time": "2022-07-25 12:04:37"
}
def encryptJson(key,jsondata):
    #加密json
    jsondata = json.dumps(jsondata,separators=(',', ':'),ensure_ascii=False)
    #字典->json字符串  不要把中文变成unicode编码,去掉美化空格
    #ensure_ascii=False
    # print(jsondata)
    # print(type(jsondata))
    key_encrypt = bytes(key[0:8], encoding = "utf8")
    jsondata=bytes('{}'.format(jsondata),'utf-8')
    
    cipher = DES.new(key_encrypt, DES.MODE_ECB)
    jsondata = pad(jsondata,DES.block_size,style='pkcs7')
    result = cipher.encrypt(jsondata)
    #加密json文件
    return binascii.b2a_hex(result)
    #字符转16进制

print(encryptJson('abcdefgh',jsonfile))
#key 长度为8位
===================================================
##结果输出
b'816e246e5ce8c9fa3d5bf5feeac5dde828841b1e30f3fb944293df7e513cdffe5a22db5beaaf59be3f9902e20bfa1300d86e2c1ce2cefaf10d9e4c005467e79c7632f233da870d392237d4721a1db03c17b3f3f18065f7da73ed7c54426961ce124872da22b730e47bf0ae72599ccfbd357b6ac254485f83f5252edeffffd8d0934b25d3c9183422b9f2f641be83c3d5aafa16d1b46ab2e60df3a6591ff80a2804b076a85da055062f206c495cb23b12a41355b10fcd1905413c44f7aa0b3b0781ed5be70edae9a25670d50bf2c7ce728f18c038f3a281cc688385df45c4c997528296940db7539ede940ae24eb9bb810238cc4573b43afb7c739d26ffa5d2a1949173cb4f6e4dcb669f1e8c0465791e164083d40cafeb0a2f7d85c7adccc49c1f5341b41eafa0c5df22491b7c336c65085627f2cbad7b51761fb21815e23f710e30f29c9f91ac951233562087a086f2747161a8f298709b6c199ebadd47cec16abb9c55f5e5b9b6d4d263aff752f861c332776275d5be30b76e5e56ec27f8e2e8f6cc21bf23555619104751376bef09899fce88a671adc9b14a405da422e86f2aa43c32d5b43cfdcc2ce11cc9797990'

DES ECB pkcs7解密举例

import binascii
from Crypto.Cipher import DES

def stripPad(data):
    #去除填充数据
    data = data.strip(b'\x01')
    data = data.strip(b'\x02')
    data = data.strip(b'\x03')
    data = data.strip(b'\x04')
    data = data.strip(b'\x05')
    data = data.strip(b'\x06')
    data = data.strip(b'\x07')
    data = data.strip(b'\x08')
    return data
    
def decrypt(key,encryptdata):
    # encryptdata = base64.b64decode(encryptdata.encode())
    encryptdata=bytes('{}'.format(encryptdata),'utf-8')
    key_encrypt = bytes(key[0:8], encoding = "utf-8")
    #把encryptdata和key_encrypt改成byte格式
    cipher = DES.new(key_encrypt, DES.MODE_ECB)
    encryptdata = binascii.a2b_hex(encryptdata)
    #将byte数据转为16进制数据
    result = cipher.decrypt(encryptdata)
    result = str(result,'utf-8')
    #byte转换成字符串,16进制会自动转换中文,去掉填充的pad
    #result=bytes.decode(base64.b64encode(result))
    print(result)

encryptdata = r'42419f2121b2a54aad154e6a4b5237c94ee667dfe873aaf4304819c8ce3fb50ea086050948510f0663ec462e67c843846ac74144bbfaa79cc7e72d98c362b58e0a66cbaeb30f64f13435a3b8c7859034960132546936f8e7d881d61e0baa6f89e9f32bc1943cd18b5d0352da6c2cd6daa9fe59b1edca0e4d280478c64b857cc7c2216a89495f344096db573ce0b630a3b78ed47101f441468502f759dcf8186feebe420b12a214d31efa9fe6e215830b159bc93bd3fe453f5f2dae618b1498aa30838a3e82ee7fc5'
key = '12345678'

decrypt(key,encryptdata)

===================================================
##结果输出
PS C:\python学习> & C:/Users/a2602/AppData/Local/Programs/Python/Python310/python.exe c:/python学习/test.py
说明:DES数据块长度为64位,所以IV长度需要为8个字符(ECB模式不用IV),密钥长度也为8个字符,IV与密钥超过长度则截取,不足则在末尾填充'\0'补足

你可能感兴趣的:(python,DES,pycryptodome)