Python3.6 AES加密依賴包Crypto的安裝,pyinstaller打包exe報錯Cannot load native module ‘Crypto.Cipher._raw_ecb的解決方法
所有方法都來自網際網路,我做了個全套總結
pycrypto,pycrytodome和crypto是一個東西,在很久以前,crypto在python上面的名字是pycrypto它是一個第三方庫,但是已經停止更新三年了,所以不建議安裝這個庫;但有一個延伸版pycryptodome,用法和pycrypto一模一樣的,正常用pip安裝就OK:
pip install pycryptodome安裝完之後,去C:\Python36\Lib\site-packages資料夾(python安裝目錄的第三方庫)下找到crypto資料夾,改成Crypto,‘C’改成大寫,然後用測試程式碼驗證:我用的CFB模式,不限制明文的長度,比較簡單
#AES-dome
# -*- coding: utf-8 -*-
import base64
import os
from Crypto.Cipher import AES
'''
採用AES CFB模式 對稱加密演算法
CFB模式,對待加密原文長度沒有要求
'''
class AES_encrypt_decrypt(object):
def __init__(self, key):
self.key = key.encode('utf-8')
self.mode = AES.MODE_CFB
# 加密函式,採用CFB方式,text明文沒有長度限制
def encrypt(self, text):
text = text.encode('utf-8')
#注意,這裡Python3.6下用AES傳遞key時必須要轉換成2進位制,key前面要加'b'
crypto = AES.new(self.key, self.mode, b'woshigongyao6666')
# 這裡金鑰key 長度必須為16(AES-128),
# 24(AES-192),或者32 (AES-256)Bytes 長度
# 目前AES-128 足夠目前使用
self.ciphertext = crypto.encrypt(text)
# 因為AES加密時候得到的字串不一定是ascii字符集的,輸出到終端或者儲存時候可能存在問題
# 所以這裡統一用base64轉化為字串
return str(base64.encodebytes(self.ciphertext), encoding='utf-8')
# 解密
def decrypt(self, text):
crypto = AES.new(self.key, self.mode, b'woshigongyao6666')
plain_text = crypto.decrypt(base64.decodebytes(text.encode(encoding='utf-8')))
return bytes.decode(plain_text)
if __name__ == '__main__':
# 初始化金鑰
sql = AES_encrypt_decrypt('woshisiyao666888')
text = 'my AES encrypt program!'
e = pc.encrypt(text) # 加密
d = pc.decrypt(e) # 解密
print("原文:"+'\n'+text)
print("加密結果:"+'\n'+ e)
print("解密結果:"+'\n'+d)
執行上面的程式,應該可以跑出來這樣的結果:
原文:
my AES encrypt program!
加密結果:
DyMSIDSVV6zp8r2XtcPEHaWlqcNuEOA=
解密結果:
my AES encrypt program!
好,下面開始用Pyinstaller打包,但無奈報錯,經過網上一頓找,發現原因大致如下,延續開發的人漏了hook,需要手動加一個hook檔案到C:\python36\Lib\site-packages\PyInstaller\hooks,檔名是hook-Crypto.py,內容如下:
#-----------------------------------------------------------------------------
# Copyright (c) 2005-2018, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License with exception
# for distributing bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
"""
Hook for PyCryptodome library: https://pypi.python.org/pypi/pycryptodome
PyCryptodome is an almost drop-in replacement for the now unmaintained
PyCrypto library. The two are mutually exclusive as they live under
the same package ("Crypto").
PyCryptodome distributes dynamic libraries and builds them as if they were
Python C extensions (even though they are not extensions - as they can't be
imported by Python). It might sound a bit weird, but this decision is rooted
in PyPy and its partial and slow support for C extensions. However, this also
invalidates several of the existing methods used by PyInstaller to decide the
right files to pull in.
Even though this hook is meant to help with PyCryptodome only, it will be
triggered also when PyCrypto is installed, so it must be tested with both.
Tested with PyCryptodome 3.5.1, PyCrypto 2.6.1, Python 2.7 & 3.6, Fedora & Windows
"""
import os
import glob
from PyInstaller.compat import EXTENSION_SUFFIXES
from PyInstaller.utils.hooks import get_module_file_attribute
# Include the modules as binaries in a subfolder named like the package.
# Cryptodome's loader expects to find them inside the package directory for
# the main module. We cannot use hiddenimports because that would add the
# modules outside the package.
binaries = []
binary_module_names = [
'Crypto.Math', # First in the list
'Crypto.Cipher',
'Crypto.Util',
'Crypto.Hash',
'Crypto.Protocol',
]
try:
for module_name in binary_module_names:
m_dir = os.path.dirname(get_module_file_attribute(module_name))
for ext in EXTENSION_SUFFIXES:
module_bin = glob.glob(os.path.join(m_dir, '_*%s' % ext))
for f in module_bin:
binaries.append((f, module_name.replace('.', os.sep)))
except ImportError:
# Do nothing for PyCrypto (Crypto.Math does not exist there)
pass
儲存之後,拷貝到那個目錄,重新用Pyinstaller打包,完美生成EXE檔案!
相關文章
- pyinstaller 打包 exe 程式步驟和新增依賴檔案方法
- [PY] PyQT 依賴 pyinstaller 打包QT
- npm 或 yarn安裝依賴報錯 EPERM: operation not permitted, unlink 解決方法NPMYarnMIT
- python3.6 使用pyinstaller 打包web程式的方法PythonWeb
- vue安裝babel依賴報錯VueBabel
- 解決eclipse的maven依賴報錯EclipseMaven
- ubuntu解決軟體安裝依賴錯誤Ubuntu
- solrcloud頁面顯示報錯Cannot load analyzer問題的解決方法SolrCloud
- Electron 如何打包生成exe安裝包,注意是安裝包。
- vscode使用npm安裝依賴報錯VSCodeNPM
- Linux系統yum安裝報錯Cannot find a valid baseurl for repo的解決方法Linux
- 安裝psutil模組報錯的解決方法
- xtrabackup_2.4.9安裝依賴包
- 報錯Intel MKL FATAL ERROR: Cannot load libmkl_core.so.的一種解決方法IntelErrorIBM
- Python PyInstaller 安裝方法 及簡單的編譯exePython編譯
- python 離線依賴包打包&安裝Python
- 依賴衝突時的解決方法
- python pyinstaller打包的exe 反編譯問題記錄 破解加密Python編譯加密
- kaldi的編譯安裝與報錯解決方法編譯
- 四、yum獲取安裝的軟體包及依賴包
- 【python】【安裝包依賴關係】Python
- 關於python安裝dlib報錯“ ImportError: DLL load failed”的解決方法之一PythonImportErrorAI
- Linux下安裝DB2的包依賴LinuxDB2
- 使用yum 安裝依賴關係較多的包
- RoboWare Studio安裝及依賴項解決
- Python: 安裝 sklearn 包出現錯誤的解決方法Python
- Android Studio不能獲取遠端依賴包的解決方法Android
- 解決rpm包依賴關係
- maven解決jar包依賴衝突MavenJAR
- 解決rpm包迴圈依賴
- 解決Maven中90%的依賴(導包)問題Maven
- 使用pyinstaller打包exe檔案教程
- PyInstaller打包Python程式為exePython
- IDEA建立project和多個module(相互依賴打包)IdeaProject
- python離線安裝外部依賴包Python
- 關於React Native報Cannot access ‘serviceOf‘的解決方案React Native
- 安裝pyhs2報錯,裝不上,新增裝sasl依賴也不行
- pyinstaller 生成 exe 的閃退問題解決方案