Python运行在Windows上有错误的加密问题

2024-09-29 17:22:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行一个用Python编写的基本加密程序,虽然它在OS X上运行得很好,但我无法让它在Windows上运行(无论是在我签入希望安装Python的安装程序时随vs2017一起安装的3.6/Anaconda中,还是在独立的3.4二进制安装中)。在

就像每个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)

结果是:

^{pr2}$

我还安装了OpenSSL二进制文件(https://slproweb.com/products/Win32OpenSSL.html),并确保它在Anaconda下运行。在


Tags: fromimport程序os二进制anacondaurandomhashlib
1条回答
网友
1楼 · 发布于 2024-09-29 17:22:32

如果我猜这个代码从来没有在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.')

我觉得他们已经有好几年没法联系他们了。在

让你继续前进

您可以:

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

  2. 删除from pbkdf2_ctypes import *,并将这些函数添加到从pbkdf2 u ctypes中提取的代码中。在

    导入ctypes 进口ctypes.util在

    libname=ctypes.util.find_库('libeay32') 加密=ctypes.CDLL(libname)

    def\u openssl_hashlib_to_crypto_map_get(hashfunc): 哈希库到加密映射={哈希库.md5: 加密执行副总裁md5, 哈希库.sha1: 密码执行副总裁, hashlib.sha256: 密码, hashlib.sha224: 加密执行副总裁洔sha224, hashlib.sha384: 加密执行副总裁_, hashlib.sha512: 加密执行副总裁_sha512} crypto_hashfunc=hashlib_到\u crypto_地图。获取(hashfunc) 如果crypto_hashfunc为None: raise ValueError('未知摘要%s'%hashfunc) 密码_hashfunc.restype=ctypes.c\u无效 返回crypto_hashfunc()

    def(数据、salt、迭代、摘要、keylen): “”“OpenSSL兼容包装 """ c_hashfunc=ctypes.c_void_p(_openssl_hashlib_to_crypto_map_get(摘要))

    ^{pr2}$

    def pkcs5_pbkdf2_hmac(数据,salt,iterations=1000,keylen=24,hashfunc=None): 如果hashfunc为None: hashfunc=哈希库.sha1 err,c_buff=_openssl_pbkdf2(数据、salt、迭代、hashfunc、keylen)

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

    def pbkdf2_hex(数据,salt,iterations=1000,keylen=24,hashfunc=None): 返回比纳西伊·赫克斯利菲(pkcs5_pbkdf2_hmac(数据、salt、迭代、keylen、hashfunc))

相关问题 更多 >

    热门问题