def encrypt(self, text, appid):
"""???????
@param text: ???????
@return: ????????
"""
# 16?????????????
pack_str = struct.pack(b"I", socket.htonl(len(text)))
text = smart_bytes(self.get_random_str()) + pack_str + smart_bytes(text) + smart_bytes(appid)
# ???????????????????
pkcs7 = PKCS7Encoder()
text = pkcs7.encode(text)
# ??
cryptor = AES.new(self.key, self.mode, self.key[:16])
try:
ciphertext = cryptor.encrypt(text)
# ??BASE64????????????
return WXBizMsgCrypt_OK, base64.b64encode(ciphertext)
except Exception:
return WXBizMsgCrypt_EncryptAES_Error, None
评论列表
文章目录