在Windows上的Python加密错误

在Windows上的Python加密错误

问题描述:

我正在运行一个用Python编写的基本加密程序,虽然它在OS X上运行良好,但我无法让它在Windows上运行(在安装有VS 2017的3.6/Anaconda中当我检查我想安装Python的设置时,以及在独立3.4二进制安装中)。在Windows上的Python加密错误

就像每个单独的import语句工作在解释,但作为一个整体,该程序不能正常工作

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下运行。

如果我猜这个代码从来没有在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与某人联系,但我认为他们已经不在了,因为他们的公开代码基础已经相当一段时间了。

,让你去

您可以:在Python

  1. 运行ctypes.util.find_library('libeay32'),看看你的库。然后将libeay32.dll复制到同一文件夹中的libeay64.dll。这不应该导致任何问题,因为你正在复制一个没有其他程序知道的文件。

  2. 删除from pbkdf2_ctypes import *,并将这些函数添加到您从pbkdf2_ctypes中剥离的代码中。

    进口ctypes的 进口ctypes.util

    LIBNAME = ctypes.util.find_library( 'libeay32') 加密= ctypes.CDLL(LIBNAME)

    DEF _openssl_hashlib_to_crypto_map_get(hashfunc): 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:crypto.EVP_sha512} crypto_hashfunc = hashlib_to_crypto_map.get(hashfunc) 如果crypto_hashfunc是无: 提高ValueError异常( 'Unkwnown消化%s' 的%hashfunc) crypto_hashfunc.restype = ctypes.c_void_p 返回crypto_hashfunc()

    DEF _openssl_pbkdf2(数据,盐,迭代,消化,KEYLEN): “” 为 “OpenSSL compatibile包装器 ”“” 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 =无): 如果hashfunc是无: hashfunc = hashlib.sha1 ERR,c_buff = _openssl_pbkdf2(数据,盐,迭代hashfunc,KEYLEN)

    if err == 0: 
        raise ValueError('wrong parameters') 
    return c_buff.raw[:keylen] 
    

    DEF pbkdf2_hex(数据,盐,迭代= 1000,KEYLEN = 24,hashfunc =无): 返回binascii.hexlify(pkcs5_pbkdf2_hmac(数据,盐,迭代,keylen,hashfunc))

+0

谢谢!我遵循你的第一个(更简单,我猜)解决方案,将libeay32复制到一个新的libeay64,它恰好位于C:\ ProgramFiles \ Anaconda \ Library \ bin中(运行VS 2017附带的3.6)。我也注意到,测试在哪里它失败了,但我不觉得如此自信挖入该代码:( –