Windows 上的 Python 加密错误
Error with Python crypto on Windows
我正在 运行 编写一个用 Python 编写的基本加密程序,虽然它在 OS X 上运行良好,但我无法在 运行 上使用它Windows(当我签入我想要安装的设置 Python 时,在与 VS 2017 一起安装的 3.6/Anaconda 中,以及在独立的 3.4 二进制安装中)。
就像每个导入语句在解释器中单独工作一样,但作为一个整体这个程序不工作
from hashlib import sha256
from pbkdf2_ctypes import *
import hmac
import hashlib
import binascii
from os import urandom
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import getpass
masterpassword = "thisisamasterpassword"
salt = urandom(16)
masterpassword = pbkdf2_hex(masterpassword.encode('utf-8'), salt)
password = masterpassword.decode()
salt = binascii.hexlify(salt)
salt = salt.decode()
print(masterpassword)
结果是:
C:\Users\me\Desktop>py -3.4 masterpassword.py
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pbkdf2_ctypes.py", line 127, in <module>
raise OSError('Library not found')
OSError: Library not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "masterpassword.py", line 3, in <module>
from pbkdf2_ctypes import *
File "C:\Python34\lib\site-packages\pbkdf2_ctypes.py", line 153, in <module>
raise ImportError('Cannot find a compatible cryptographic library '
ImportError: Cannot find a compatible cryptographic library on your system
我还安装了一个 OpenSSL 二进制文件 (https://slproweb.com/products/Win32OpenSSL.html) 并确保它在 Anaconda 下 运行ning。
如果我猜这段代码在 Windows-64 位机器上无法运行。引发的错误来自搜索加密库的逻辑中的pbkdf2_ctypes
;我认为 libeay64.dll 将安装在 64 位系统上而 libeay32.dll 将安装在 32 位系统上是一个偶然的(尽管是明智的)假设:
if system == 'Windows':
if platform.architecture()[0] == '64bit':
libname = ctypes.util.find_library('libeay64') # <--- This does not exist even on 64bit machines ... :)
if not libname:
raise OSError('Library not found')
crypto = ctypes.CDLL(libname)
else:
libname = ctypes.util.find_library('libeay32')
if not libname:
raise OSError('Library libeay32 not found.')
您可以尝试联系 Glisco 的某个人,但我认为他们已经不在了,因为他们的 public 代码库已经用了好几年了。
助你一臂之力
您可以:
运行 ctypes.util.find_library('libeay32')
in python 看看你的图书馆在哪里。然后在同一个文件夹中复制libeay32.dll到libeay64.dll。这应该不会造成任何问题,因为您正在复制一个其他程序都不知道的文件。
删除 from pbkdf2_ctypes import *
,并将这些函数添加到从 pbkdf2_ctypes.
中删除的代码中
导入ctypes
导入 ctypes.util
库名 = ctypes.util.find_library('libeay32')
密码 = ctypes.CDLL(库名)
def _openssl_hashlib_to_crypto_map_get(哈希函数):
hashlib_to_crypto_map = {hashlib.md5: crypto.EVP_md5,
hashlib.sha1: crypto.EVP_sha1,
hashlib.sha256: crypto.EVP_sha256,
hashlib.sha224: crypto.EVP_sha224,
hashlib.sha384: crypto.EVP_sha384,
hashlib.sha512:加密。EVP_sha512}
crypto_hashfunc = hashlib_to_crypto_map.get(哈希函数)
如果 crypto_hashfunc 是 None:
提高 ValueError('Unkwnown digest %s' % hashfunc)
crypto_hashfunc.restype = ctypes.c_void_p
return crypto_hashfunc()
def _openssl_pbkdf2(数据、salt、迭代、摘要、keylen):
"""OpenSSL 兼容包装器
"""
c_hashfunc = ctypes.c_void_p(_openssl_hashlib_to_crypto_map_get(摘要))
c_pass = ctypes.c_char_p(data)
c_passlen = ctypes.c_int(len(data))
c_salt = ctypes.c_char_p(salt)
c_saltlen = ctypes.c_int(len(salt))
c_iter = ctypes.c_int(iterations)
c_keylen = ctypes.c_int(keylen)
c_buff = ctypes.create_string_buffer(keylen)
crypto.PKCS5_PBKDF2_HMAC.argtypes = [ctypes.c_char_p, ctypes.c_int,
ctypes.c_char_p, ctypes.c_int,
ctypes.c_int, ctypes.c_void_p,
ctypes.c_int, ctypes.c_char_p]
crypto.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int
err = crypto.PKCS5_PBKDF2_HMAC(c_pass, c_passlen,
c_salt, c_saltlen,
c_iter,
c_hashfunc,
c_keylen,
c_buff)
return (err, c_buff)
def pkcs5_pbkdf2_hmac(数据、盐、迭代次数=1000、keylen=24、hashfunc=None):
如果 hashfunc 是 None:
hashfunc = hashlib.sha1
错误,c_buff = _openssl_pbkdf2(数据、salt、迭代、hashfunc、keylen)
if err == 0:
raise ValueError('wrong parameters')
return c_buff.raw[:keylen]
def pbkdf2_hex(数据、盐、迭代次数=1000、keylen=24、hashfunc=None):
return binascii.hexlify(pkcs5_pbkdf2_hmac(数据、salt、迭代、keylen、hashfunc))
我正在 运行 编写一个用 Python 编写的基本加密程序,虽然它在 OS X 上运行良好,但我无法在 运行 上使用它Windows(当我签入我想要安装的设置 Python 时,在与 VS 2017 一起安装的 3.6/Anaconda 中,以及在独立的 3.4 二进制安装中)。
就像每个导入语句在解释器中单独工作一样,但作为一个整体这个程序不工作
from hashlib import sha256
from pbkdf2_ctypes import *
import hmac
import hashlib
import binascii
from os import urandom
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import getpass
masterpassword = "thisisamasterpassword"
salt = urandom(16)
masterpassword = pbkdf2_hex(masterpassword.encode('utf-8'), salt)
password = masterpassword.decode()
salt = binascii.hexlify(salt)
salt = salt.decode()
print(masterpassword)
结果是:
C:\Users\me\Desktop>py -3.4 masterpassword.py
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\pbkdf2_ctypes.py", line 127, in <module>
raise OSError('Library not found')
OSError: Library not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "masterpassword.py", line 3, in <module>
from pbkdf2_ctypes import *
File "C:\Python34\lib\site-packages\pbkdf2_ctypes.py", line 153, in <module>
raise ImportError('Cannot find a compatible cryptographic library '
ImportError: Cannot find a compatible cryptographic library on your system
我还安装了一个 OpenSSL 二进制文件 (https://slproweb.com/products/Win32OpenSSL.html) 并确保它在 Anaconda 下 运行ning。
如果我猜这段代码在 Windows-64 位机器上无法运行。引发的错误来自搜索加密库的逻辑中的pbkdf2_ctypes
;我认为 libeay64.dll 将安装在 64 位系统上而 libeay32.dll 将安装在 32 位系统上是一个偶然的(尽管是明智的)假设:
if system == 'Windows':
if platform.architecture()[0] == '64bit':
libname = ctypes.util.find_library('libeay64') # <--- This does not exist even on 64bit machines ... :)
if not libname:
raise OSError('Library not found')
crypto = ctypes.CDLL(libname)
else:
libname = ctypes.util.find_library('libeay32')
if not libname:
raise OSError('Library libeay32 not found.')
您可以尝试联系 Glisco 的某个人,但我认为他们已经不在了,因为他们的 public 代码库已经用了好几年了。
助你一臂之力
您可以:
运行
ctypes.util.find_library('libeay32')
in python 看看你的图书馆在哪里。然后在同一个文件夹中复制libeay32.dll到libeay64.dll。这应该不会造成任何问题,因为您正在复制一个其他程序都不知道的文件。删除
中删除的代码中from pbkdf2_ctypes import *
,并将这些函数添加到从 pbkdf2_ctypes.导入ctypes 导入 ctypes.util
库名 = ctypes.util.find_library('libeay32') 密码 = ctypes.CDLL(库名)
def _openssl_hashlib_to_crypto_map_get(哈希函数): hashlib_to_crypto_map = {hashlib.md5: crypto.EVP_md5, hashlib.sha1: crypto.EVP_sha1, hashlib.sha256: crypto.EVP_sha256, hashlib.sha224: crypto.EVP_sha224, hashlib.sha384: crypto.EVP_sha384, hashlib.sha512:加密。EVP_sha512} crypto_hashfunc = hashlib_to_crypto_map.get(哈希函数) 如果 crypto_hashfunc 是 None: 提高 ValueError('Unkwnown digest %s' % hashfunc) crypto_hashfunc.restype = ctypes.c_void_p return crypto_hashfunc()
def _openssl_pbkdf2(数据、salt、迭代、摘要、keylen): """OpenSSL 兼容包装器 """ c_hashfunc = ctypes.c_void_p(_openssl_hashlib_to_crypto_map_get(摘要))
c_pass = ctypes.c_char_p(data) c_passlen = ctypes.c_int(len(data)) c_salt = ctypes.c_char_p(salt) c_saltlen = ctypes.c_int(len(salt)) c_iter = ctypes.c_int(iterations) c_keylen = ctypes.c_int(keylen) c_buff = ctypes.create_string_buffer(keylen) crypto.PKCS5_PBKDF2_HMAC.argtypes = [ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_char_p] crypto.PKCS5_PBKDF2_HMAC.restype = ctypes.c_int err = crypto.PKCS5_PBKDF2_HMAC(c_pass, c_passlen, c_salt, c_saltlen, c_iter, c_hashfunc, c_keylen, c_buff) return (err, c_buff)
def pkcs5_pbkdf2_hmac(数据、盐、迭代次数=1000、keylen=24、hashfunc=None): 如果 hashfunc 是 None: hashfunc = hashlib.sha1 错误,c_buff = _openssl_pbkdf2(数据、salt、迭代、hashfunc、keylen)
if err == 0: raise ValueError('wrong parameters') return c_buff.raw[:keylen]
def pbkdf2_hex(数据、盐、迭代次数=1000、keylen=24、hashfunc=None): return binascii.hexlify(pkcs5_pbkdf2_hmac(数据、salt、迭代、keylen、hashfunc))