代码之家  ›  专栏  ›  技术社区  ›  Amanda_Panda

Windows上的Python crypto出错

  •  0
  • Amanda_Panda  · 技术社区  · 7 年前

    每个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 )确保它在蟒蛇下运行。

    1 回复  |  直到 7 年前
        1
  •  1
  •   DoDoSmarts    7 年前

    如果我猜到这段代码从未在Windows-64位机器上运行过。引发的错误来自 pbkdf2_ctypes

    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的人,但我认为他们已经不在了,因为他们的公共代码库已经消失了好几年了。

    让你走

    1. ctypes.util.find_library('libeay32') 在python中,查看库的位置。然后复制libeay32。dll到libeay64。dll在同一文件夹中。这不会引起任何问题,因为您正在复制一个其他程序都不知道的文件。

    2. 去除 from pbkdf2_ctypes import *

      导入ctypes。util

      libname=ctypes。util。查找库('libeay32') 加密=ctypes。CDLL(libname)

      def \u openssl\u hashlib\u to\u crypto\u map\u get(hashfunc): hashlib。sha1:加密。执行副总裁sha1, hashlib。sha224:加密。执行副总裁sha224, hashlib。sha384:加密。执行副总裁sha384, crypto\u hashfunc=hashlib\u to\u crypto\u映射。get(hashfunc) 如果crypto_hashfunc为无: crypto_hashfunc。重新类型=ctypes。c\u void\u p 返回crypto_hashfunc()

      “”“OpenSSL兼容包装器 c_hashfunc=ctypes。c\u void\u p(\u openssl\u hashlib\u to\u crypto\u map\u 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)
      

      如果hashfunc为None: hashfunc=hashlib。沙1 err,c\u buff=\u openssl\u pbkdf2(数据,salt,迭代,hashfunc,keylen)

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

      def pbkdf2_hex(数据,salt,迭代次数=1000,keylen=24,hashfunc=无): 返回binascii。hexlify(pkcs5\u pbkdf2\u hmac(数据,salt,迭代,keylen,hashfunc))