今天在用 python 写 小程序需要解密时,需要安装 Crypto 使用 AES 对登录的信息进行解密
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Random.OSRNG.winrandom' extension
error: Microsoft Visual C++ 14.0 is required. Get it with "Build Tools for Visual Studio": https://visualstudio.microsoft.com/downloads/
----------------------------------------
ERROR: Command errored out with exit status 1: 'D:\Program Files\python3.8\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\auth\\AppData\\Local\\Temp\\pip-install-lmxibyso\\pycrypto\\setup.py'"'"'; __file__='"'"'C:\\Users\\auth\\AppData\\Local\\Temp\\pip-install-lmxibyso\\pycrypto\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\auth\AppData\Local\Temp\pip-record-ii2jkae9\install-record.txt' --single-version-externally-managed --compile --install-headers 'D:\Program Files\python3.8\Include\pycrypto' Check the logs for full command output.
找了很多方法
我试了更新 pip,试了卸载重装,试了 安装 pycrypto 编译好的 exe 安装包(不知道是不是因为版本不对,最后已失败告终),,,
终于还是发现了一个 可以用的
https://www.cnblogs.com/steinven/p/11216361.html
python 在 Windows下使用AES时要安装的是pycryptodome 模块
pip install pycryptodome
python 在 Linux下使用AES时要安装的是pycrypto模块
pip install pycrypto
终于解开了小程序的加密信息
# 微信官方 提供的 解密 demo
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:])]