Windows上的Python加密错误
我正在运行一个用Python编写的基本加密程序,虽然它在OS X上运行得很好,但我无法让它在Windows上运行(无论是在3.6/Anaconda中,当我签入希望安装Python的安装程序时,它与VS 2017一起安装,还是在独立的3.4二进制安装中) 每个import语句都在解释器中工作,但作为一个整体,这个程序不工作Windows上的Python加密错误,python,python-3.x,pyopenssl,Python,Python 3.x,Pyopenssl,我正在运行一个用Python编写的基本加密程序,虽然它在OS X上运行得很好,但我无法让它在Windows上运行(无论是在3.6/Anaconda中,当我签入希望安装Python的安装程序时,它与VS 2017一起安装,还是在独立的3.4二进制安装中) 每个import语句都在解释器中工作,但作为一个整体,这个程序不工作 from hashlib import sha256 from pbkdf2_ctypes import * import hmac import hashlib impor
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
C:\Users\me\Desktop>py-3.4 masterpassword.py
回溯(最近一次呼叫最后一次):
文件“C:\Python34\lib\site packages\pbkdf2_ctypes.py”,第127行,在
引发操作错误('未找到库')
OSError:找不到库
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“masterpassword.py”,第3行,在
从pbkdf2_ctypes导入*
文件“C:\Python34\lib\site packages\pbkdf2_ctypes.py”,第153行,在
raise ImportError('找不到兼容的加密库'
ImportError:在系统上找不到兼容的加密库
我还安装了一个OpenSSL二进制文件(),并确保它在Anaconda下运行。如果我猜这段代码从未在Windows-64位机器上运行过的话。引发的错误来自搜索加密库的逻辑中的
pbkdf2\u 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.')
def pkcs5_pbkdf2_hmac(数据,salt,迭代次数=1000,keylen=24,hashfunc=None):
如果hashfunc为无:
hashfunc=hashlib.sha1
err,c_buff=_openssl_pbkdf2(数据、salt、迭代、hashfunc、keylen)
def pbkdf2_hex(数据,salt,迭代次数=1000,keylen=24,hashfunc=None):
返回binascii.hexlify(pkcs5_pbkdf2_hmac(数据、salt、迭代、keylen、hashfunc))
谢谢!我遵循了您的第一个(我想是更简单的)解决方案,将libeay32复制到一个新的libeay64,它恰好位于C:\ProgramFiles\Anaconda\Library\bin中(运行VS2017附带的3.6)。我也注意到之前的测试失败了,但我对深入研究该代码不太自信:(
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)
if err == 0:
raise ValueError('wrong parameters')
return c_buff.raw[:keylen]