加密数据解密算法--python样例中存在一处bug
官方提供的类 WXBizDataCrypt 中的方法decrypt存在一个字节转换的bug [代码]import[代码] [代码]base64[代码][代码]import[代码] [代码]json[代码] [代码]from[代码] [代码]Crypto.Cipher [代码][代码]import[代码] [代码]AES[代码] [代码]class[代码] [代码]WXBizDataCrypt:[代码][代码] [代码][代码]def[代码] [代码]__init__([代码][代码]self[代码][代码], appId, sessionKey):[代码][代码] [代码][代码]self[代码][代码].appId [代码][代码]=[代码] [代码]appId[代码][代码] [代码][代码]self[代码][代码].sessionKey [代码][代码]=[代码] [代码]sessionKey[代码] [代码] [代码][代码]def[代码] [代码]decrypt([代码][代码]self[代码][代码], encryptedData, iv):[代码][代码] [代码][代码]# base64 decode[代码][代码] [代码][代码]sessionKey [代码][代码]=[代码] [代码]base64.b64decode([代码][代码]self[代码][代码].sessionKey)[代码][代码] [代码][代码]encryptedData [代码][代码]=[代码] [代码]base64.b64decode(encryptedData)[代码][代码] [代码][代码]iv [代码][代码]=[代码] [代码]base64.b64decode(iv)[代码] [代码] [代码][代码]cipher [代码][代码]=[代码] [代码]AES.new(sessionKey, AES.MODE_CBC, iv)[代码] [代码] [代码][代码]decrypted [代码][代码]=[代码] [代码]json.loads([代码][代码]self[代码][代码]._unpad(cipher.decrypt(encryptedData))[代码][代码])[代码] [代码] [代码][代码]if[代码] [代码]decrypted[[代码][代码]'watermark'[代码][代码]][[代码][代码]'appid'[代码][代码]] ![代码][代码]=[代码] [代码]self[代码][代码].appId:[代码][代码] [代码][代码]raise[代码] [代码]Exception([代码][代码]'Invalid Buffer'[代码][代码])[代码] [代码] [代码][代码]return[代码] [代码]decrypted[代码] [代码] [代码][代码]def[代码] [代码]_unpad([代码][代码]self[代码][代码], s):[代码][代码] [代码][代码]return[代码] [代码]s[:[代码][代码]-[代码][代码]ord[代码][代码](s[[代码][代码]len[代码][代码](s) [代码][代码]-[代码] [代码]1[代码][代码]:])][代码] 大概19行的位置 [代码]decrypted [代码][代码]=[代码] [代码]json.loads([代码][代码]self[代码][代码]._unpad(cipher.decrypt(encryptedData)))[代码] [代码] [代码] [代码]json.loads()中应该是填写字符串[代码] [代码] [代码] [代码]cipher.decrypt(encryptedData)返回的是字节,需要对字节进行字符串转换[代码] [代码] [代码] [代码]转换后[代码] [代码] [代码] [代码][代码]decrypted [代码][代码]=[代码] [代码]json.loads([代码][代码]self[代码][代码]._unpad(cipher.decrypt(encryptedData)).decode('utf8)[代码][代码])[代码][代码]