Skip to content

Commit 16e0b05

Browse files
authored
修复bug
1 parent 609f460 commit 16e0b05

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

callback_json_python3/WXBizJsonMsgCrypt.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,29 @@ def __init__(self,key):
141141
# 设置加解密模式为AES的CBC模式
142142
self.mode = AES.MODE_CBC
143143

144-
145-
def encrypt(self,text,receiveid):
144+
145+
def encrypt(self, text, receiveid):
146146
"""对明文进行加密
147147
@param text: 需要加密的明文
148148
@return: 加密得到的字符串
149-
"""
150-
# 将text和receiveid转换为bytes类型
151-
text = text.encode('utf-8')
152-
receiveid = receiveid.encode('utf-8')
153-
149+
"""
154150
# 16位随机字符串添加到明文开头
155-
random_str = self.get_random_str().encode('utf-8')
156-
text = random_str + struct.pack("I",socket.htonl(len(text))) + text + receiveid
157-
151+
text = text.encode()
152+
text = self.get_random_str() + struct.pack("I", socket.htonl(len(text))) + text + receiveid.encode()
153+
158154
# 使用自定义的填充方式对明文进行补位填充
159155
pkcs7 = PKCS7Encoder()
160156
text = pkcs7.encode(text)
161-
162-
# 加密
163-
cryptor = AES.new(self.key,self.mode,self.key[:16])
157+
# 加密
158+
cryptor = AES.new(self.key, self.mode, self.key[:16])
164159
try:
165160
ciphertext = cryptor.encrypt(text)
166161
# 使用BASE64对加密后的字符串进行编码
167162
return ierror.WXBizMsgCrypt_OK, base64.b64encode(ciphertext)
168163
except Exception as e:
169-
print(e)
170-
return ierror.WXBizMsgCrypt_EncryptAES_Error,None
164+
logger = logging.getLogger()
165+
logger.error(e)
166+
return ierror.WXBizMsgCrypt_EncryptAES_Error, None
171167

172168
def decrypt(self,text,receiveid):
173169
"""对解密后的明文进行补位删除
@@ -202,10 +198,8 @@ def decrypt(self,text,receiveid):
202198
def get_random_str(self):
203199
""" 随机生成16位字符串
204200
@return: 16位字符串
205-
"""
206-
rule = string.ascii_letters + string.digits
207-
str = random.sample(rule, 16)
208-
return "".join(str)
201+
"""
202+
return str(random.randint(1000000000000000, 9999999999999999)).encode()
209203

210204
class WXBizJsonMsgCrypt(object):
211205
#构造函数
@@ -247,6 +241,7 @@ def EncryptMsg(self, sReplyMsg, sNonce, timestamp = None):
247241
#return:成功0,sEncryptMsg,失败返回对应的错误码None
248242
pc = Prpcrypt(self.key)
249243
ret,encrypt = pc.encrypt(sReplyMsg, self.m_sReceiveId)
244+
encrypt = encrypt.decode('utf-8')
250245
if ret != 0:
251246
return ret,None
252247
if timestamp is None:

0 commit comments

Comments
 (0)