如何用pyinstaller构建使用pycrypdome的可执行文件? [英] How to build executable with pyinstaller that uses pycryptodome?

查看:19
本文介绍了如何用pyinstaller构建使用pycrypdome的可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建以下使用pycryptodome的脚本:

# based on this example http://www.codekoala.com/posts/aes-encryption-python-using-pycrypto/#comment-25921785
from Crypto.Cipher import AES
from Crypto import Random
import base64

plain_text = 'Secret data'
block_size = 16
key_size = 32
mode = AES.MODE_CBC

key_bytes = Random.get_random_bytes(key_size)
pad = block_size - len(plain_text) % block_size
data = plain_text + pad * chr(pad)
iv_bytes = Random.get_random_bytes(block_size)
encrypted_bytes = iv_bytes + AES.new(key_bytes, mode, iv_bytes).encrypt(bytes(data, encoding='utf-8'))

encrypted_string = base64.urlsafe_b64encode(encrypted_bytes)
key_string = base64.urlsafe_b64encode(key_bytes)

key_bytes2 = base64.urlsafe_b64decode(key_string)
assert key_bytes == key_bytes2

encrypted_bytes2 = base64.urlsafe_b64decode(encrypted_string)
assert encrypted_bytes == encrypted_bytes2

iv_bytes2 = encrypted_bytes2[:block_size]
assert iv_bytes == iv_bytes2

encrypted_bytes2 = encrypted_bytes2[block_size:]
plain_text2 = AES.new(key_bytes2, mode, iv_bytes2).decrypt(encrypted_bytes2)
print(plain_text2)
pad = int(plain_text2[-1])
print(pad)
plain_text2 = plain_text2[:-pad].decode('utf-8')
print(plain_text2)
assert plain_text == plain_text2

这是我在运行pyinstaller时获得的输出:

C:Users	estDocumentsMiniCryptoCodecminicryptocodec>pyinstaller crypto.py
46 INFO: PyInstaller: 3.3
46 INFO: Python: 3.6.2
46 INFO: Platform: Windows-7-6.1.7601-SP1
46 INFO: wrote C:Users	estDocumentsMiniCryptoCodecminicryptocodeccrypto.spec
46 INFO: UPX is not available.
62 INFO: Extending PYTHONPATH with paths
['C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec',
 'C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec']
62 INFO: checking Analysis
62 INFO: Building Analysis because out00-Analysis.toc is non existent
62 INFO: Initializing module dependency graph...
62 INFO: Initializing module graph hooks...
62 INFO: Analyzing base_library.zip ...
2718 INFO: running Analysis out00-Analysis.toc
2718 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:users	estappdatalocalprogramspythonpython36python.exe
3281 INFO: Caching module hooks...
3281 INFO: Analyzing C:Users	estDocumentsMiniCryptoCodecminicryptocodeccrypto.py
3625 INFO: Loading module hooks...
3625 INFO: Loading module hook "hook-encodings.py"...
3703 INFO: Loading module hook "hook-pydoc.py"...
3703 INFO: Loading module hook "hook-xml.py"...
3921 INFO: Looking for ctypes DLLs
3921 INFO: Analyzing run-time hooks ...
3921 INFO: Looking for dynamic libraries
4000 INFO: Looking for eggs
4000 INFO: Using Python library c:users	estappdatalocalprogramspythonpython36python36.dll
4000 INFO: Found binding redirects:
[]
4015 INFO: Warnings written to C:Users	estDocumentsMiniCryptoCodecminicryptocodecuildcryptowarncrypto.txt
4062 INFO: Graph cross-reference written to C:Users	estDocumentsMiniCryptoCodecminicryptocodecuildcryptoxref-crypto.html
4062 INFO: checking PYZ
4062 INFO: Building PYZ because out00-PYZ.toc is non existent
4062 INFO: Building PYZ (ZlibArchive) C:Users	estDocumentsMiniCryptoCodecminicryptocodecuildcryptoout00-PYZ.pyz
4515 INFO: Building PYZ (ZlibArchive) C:Users	estDocumentsMiniCryptoCodecminicryptocodecuildcryptoout00-PYZ.pyz completed successfully.
4515 INFO: checking PKG
4515 INFO: Building PKG because out00-PKG.toc is non existent
4515 INFO: Building PKG (CArchive) out00-PKG.pkg
4531 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
4531 INFO: Bootloader c:users	estappdatalocalprogramspythonpython36libsite-packagesPyInstallerootloaderWindows-64bit
un.exe
4531 INFO: checking EXE
4531 INFO: Building EXE because out00-EXE.toc is non existent
4531 INFO: Building EXE from out00-EXE.toc
4531 INFO: Appending archive to EXE C:Users	estDocumentsMiniCryptoCodecminicryptocodecuildcryptocrypto.exe
4546 INFO: Building EXE from out00-EXE.toc completed successfully.
4546 INFO: checking COLLECT
4546 INFO: Building COLLECT because out00-COLLECT.toc is non existent
4546 INFO: Building COLLECT out00-COLLECT.toc
4875 INFO: Building COLLECT out00-COLLECT.toc completed successfully.

一切似乎都很好。但是,当我运行构建可执行文件时,我得到以下信息:

C:Users	estDocumentsMiniCryptoCodecminicryptocodec>distcryptocrypto.exe
Traceback (most recent call last):
  File "crypto.py", line 1, in <module>
    from Crypto.Cipher import AES
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "c:users	estappdatalocalprogramspythonpython36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagesCryptoCipher\__init__.py", line 3, in <module>
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "c:users	estappdatalocalprogramspythonpython36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagesCryptoCipher\_mode_ecb.py", line 46, in <module>
  File "site-packagesCryptoUtil\_raw_api.py", line 189, in load_pycryptodome_raw_lib
OSError: Cannot load native module 'Crypto.Cipher._raw_ecb'
[2824] Failed to execute script crypto

我甚至将'pycryptodome'添加到规范文件的hiddenimports列表中,但它仍然不起作用。

要生成使用pycrypdome的正常工作的可执行文件,我需要做些什么?

推荐答案

如果要解决问题,请使用pycryptodomex

pip uninstall -y pycryptodome
pip install pycryptodomex

然后搜索并导入Crypto,并将其替换为Cryptodome

这篇关于如何用pyinstaller构建使用pycrypdome的可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆