Python 安装 Crypto

Python 安装 Crypto

今天在用 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:])]
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值