How to build executable with pyinstaller that uses pycryptodome?
Asked Answered
K

4

2

I'm trying to build the following script that uses 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

This is the output I get while running pyinstaller:

C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec>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\test\Documents\MiniCryptoCodec\minicryptocodec\crypto.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\test\appdata\local\programs\python\python36\python.exe
3281 INFO: Caching module hooks...
3281 INFO: Analyzing C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\crypto.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\test\appdata\local\programs\python\python36\python36.dll
4000 INFO: Found binding redirects:
[]
4015 INFO: Warnings written to C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\warncrypto.txt
4062 INFO: Graph cross-reference written to C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\xref-crypto.html
4062 INFO: checking PYZ
4062 INFO: Building PYZ because out00-PYZ.toc is non existent
4062 INFO: Building PYZ (ZlibArchive) C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\out00-PYZ.pyz
4515 INFO: Building PYZ (ZlibArchive) C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\out00-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\test\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.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\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\crypto.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.

Everything seems ok. But when I run the build executable I get this:

C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec>dist\crypto\crypto.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\test\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\Crypto\Cipher\__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\test\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 631, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\Crypto\Cipher\_mode_ecb.py", line 46, in <module>
  File "site-packages\Crypto\Util\_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

I even added 'pycryptodome' to the hiddenimports list in the spec file but it still does not work.

What do I need to do to build a working executable that uses pycryptodome?

Kiyokokiyoshi answered 5/10, 2017 at 11:7 Comment(0)
T
2

You may need a more recent version of PyInstaller - there was a 'hook' added for pycryptodome three months ago https://github.com/pyinstaller/pyinstaller/tree/develop/PyInstaller/hooks

After adding the hook I had additional issues and was able to get it working by installing cffi package https://pypi.python.org/pypi/cffi

pip install cffi
Tombouctou answered 17/10, 2017 at 21:1 Comment(2)
Unfortunately it did not work, i installed version 1.11.2 and when I run the built executable I get: Error loading Python DLL 'C:\Users\test\Documents\MiniCryptoCodec\minicryptocodec\build\crypto\python36.dll'. LoadLibrary: The specified module could not be found.Kiyokokiyoshi
I've installed PyInstaller: 3.4.dev0+133d18156 and I get the same error.Kiyokokiyoshi
N
2

If you want to solve your problems use pycryptodomex

pip uninstall -y pycryptodome
pip install pycryptodomex

Then search and all the imports of Crypto and replace them with Cryptodome.

Nidanidaros answered 23/1, 2018 at 18:59 Comment(1)
Thanks a million, mate! It's SO much better to have disambiguation now: from Cryptodome.PublicKey import RSA. Ran pyinstaller with this and it worked without needing any hiddenimports etc. In the dist, there's now a clear separate folder of Cryptodome.Cupronickel
K
0

It works using pyinstaller 3.4 as installed by pip on conda 4.5.12:

>conda create -n pyinstaller python=3.7
>activate pyinstaller
>pip install pyinstaller
>pip install pycryptodome
>conda list 
# packages in environment at C:\Users\pablo\AppData\Local\Continuum\anaconda3\envs\pyinstaller:
#
# Name                    Version                   Build  Channel
altgraph                  0.16.1                    <pip>
ca-certificates           2018.03.07                    0
certifi                   2018.11.29               py37_0
future                    0.17.1                    <pip>
macholib                  1.11                      <pip>
openssl                   1.1.1a               he774522_0
pefile                    2018.8.8                  <pip>
pip                       18.1                     py37_0
pycryptodome              3.7.2                     <pip>
PyInstaller               3.4                       <pip>
python                    3.7.2                h8c8aaf0_0
pywin32-ctypes            0.2.0                     <pip>
setuptools                40.6.3                   py37_0
sqlite                    3.26.0               he774522_0
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.15.26706          h3a45250_0
wheel                     0.32.3                   py37_0
wincertstore              0.2                      py37_0
>
>pyinstaller main.py
62 INFO: PyInstaller: 3.4
62 INFO: Python: 3.7.2
62 INFO: Platform: Windows-10-10.0.15063-SP0
62 INFO: wrote C:\Users\pablo\Dev\untitled\main.spec
78 INFO: UPX is not available.
78 INFO: Extending PYTHONPATH with paths
['C:\\Users\\pablo\\Dev\\untitled', 'C:\\Users\\pablo\\Dev\\untitled']
78 INFO: checking Analysis
78 INFO: Building Analysis because Analysis-00.toc is non existent
78 INFO: Initializing module dependency graph...
78 INFO: Initializing module graph hooks...
78 INFO: Analyzing base_library.zip ...
2734 INFO: running Analysis Analysis-00.toc
2734 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\pablo\appdata\local\continuum\anaconda3\envs\pyinstaller\python.exe
3390 INFO: Caching module hooks...
3406 INFO: Analyzing C:\Users\pablo\Dev\untitled\main.py
3671 INFO: Loading module hooks...
3671 INFO: Loading module hook "hook-Crypto.py"...
3687 INFO: Loading module hook "hook-encodings.py"...
3765 INFO: Loading module hook "hook-pydoc.py"...
3765 INFO: Loading module hook "hook-xml.py"...
3953 INFO: Looking for ctypes DLLs
3953 INFO: Analyzing run-time hooks ...
3968 INFO: Looking for dynamic libraries
4859 INFO: Looking for eggs
4859 INFO: Using Python library c:\users\pablo\appdata\local\continuum\anaconda3\envs\pyinstaller\python37.dll
4859 INFO: Found binding redirects:
[]
4875 INFO: Warnings written to C:\Users\pablo\Dev\untitled\build\main\warn-main.txt
4906 INFO: Graph cross-reference written to C:\Users\pablo\Dev\untitled\build\main\xref-main.html
4937 INFO: checking PYZ
4937 INFO: Building PYZ because PYZ-00.toc is non existent
4937 INFO: Building PYZ (ZlibArchive) C:\Users\pablo\Dev\untitled\build\main\PYZ-00.pyz
5484 INFO: Building PYZ (ZlibArchive) C:\Users\pablo\Dev\untitled\build\main\PYZ-00.pyz completed successfully.
5499 INFO: checking PKG
5499 INFO: Building PKG because PKG-00.toc is non existent
5499 INFO: Building PKG (CArchive) PKG-00.pkg
5515 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
5515 INFO: Bootloader c:\users\pablo\appdata\local\continuum\anaconda3\envs\pyinstaller\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
5515 INFO: checking EXE
5515 INFO: Building EXE because EXE-00.toc is non existent
5515 INFO: Building EXE from EXE-00.toc
5515 INFO: Appending archive to EXE C:\Users\pablo\Dev\untitled\build\main\main.exe
5515 INFO: Building EXE from EXE-00.toc completed successfully.
5531 INFO: checking COLLECT
5531 INFO: Building COLLECT because COLLECT-00.toc is non existent
5531 INFO: Building COLLECT COLLECT-00.toc
6062 INFO: Building COLLECT COLLECT-00.toc completed successfully.
Kiyokokiyoshi answered 16/1, 2019 at 12:35 Comment(0)
A
0

my environment:

python 3.6
pyinstaller 4.10
pycryptodomex 3.15.0

and then copy the hook file(hook-Cryptodome.py) into venv/Lib/site-packages/PyInstaller. then using the pyinstaller to excute following bash:

pyinstaller ../../demo.py -F -p ../venv/Lib/site-packages

ps. the path maybe you should adjust in your own PC.

Anacreontic answered 15/7, 2022 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.