python-3.x - Python3 PKCS7编码器
问题描述
我尝试使用 PKCS7Encoder 解密和加密 pycryptodome
我的密码器.py
# -*- coding: utf-8 -*-
from Crypto.Cipher import AES
import base64
import json
from pkcs7 import PKCS7Encoder
class Crypter(object):
def __init__(self):
self.encoder=PKCS7Encoder()
def decrypt(self,input,key):
return self.encoder.decode(AES.new(base64.b64decode(key), AES.MODE_CBC, '\x00'*16).decrypt(base64.b64decode(input)))
def encrypt(self,input,key):
padded_data=self.encoder.encode(json.dumps(input))
return base64.b64encode(AES.new(base64.b64decode(key), AES.MODE_CBC, '\x00'*16).encrypt(padded_data))
我的 pkcs7.py
import binascii
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
class PKCS7Encoder(object):
def __init__(self, k=16):
self.k = k
def decode(self, text):
'''
Remove the PKCS#7 padding from a text string
'''
nl = len(text)
val = int(binascii.hexlify(text[-1]), 16)
if val > self.k:
raise ValueError('Input is not padded or padding is corrupt')
l = nl - val
return text[:l]
def encode(self, text):
'''
Pad an input string according to PKCS#7
'''
l = len(text)
output = StringIO()
val = self.k - (l % self.k)
for _ in range(val):
output.write('%02x' % val)
return text + binascii.unhexlify(output.getvalue())
它总是像这样返回错误
return text + binascii.unhexlify(output.getvalue()) TypeError: can only concatenate str (not "bytes") to str
我尝试将字节更改为 str 不起作用,有什么解决方案吗?
解决方案
推荐阅读
- java - 假装客户端异常重试
- c# - 如何将 T-SQL 转换为 Linq C#
- vb.net - 以编程方式更新 DataSet 结构
- ruby-on-rails - 如何将属性添加到 ransackable_attributes(:admin)
- reactjs - ReactJS:在useEffect中获取时超出最大更新深度错误
- reactjs - 我已经渲染了列表获取 API,但是如何将列表数据索引传递给子组件?
- kubernetes - 使用公共 IP 启动 Kubernetes 集群
- python - Selenium 不执行点击操作
- c# - 在 ClickOnce 安装期间包括第三方安装
- c++ - 如何从 QtObjects(lineedit,combobox) 获取?