在Windows中使用crypt模块?

问题描述 投票:0回答:6

在 IDLE 和 Python 版本 3.3.2 中,我尝试像这样调用 python 模块:

hash2 = crypt(word, salt)

我将其导入到程序顶部,如下所示:

from crypt import *

我得到的结果如下:

Traceback (most recent call last):
  File "C:\none\of\your\business\adams.py", line 10, in <module>
    from crypt import *
  File "C:\Python33\lib\crypt.py", line 3, in <module>
    import _crypt
ImportError: No module named '_crypt'

但是,当我在 Ubuntu 中使用 Python 2.7.3 执行相同的文件

adams.py
时,它执行得很好 - 没有错误。

我尝试了以下方法来解决 Windows 和 Python 3.3.2 的问题(尽管我确信操作系统不是问题,Python 版本或我使用的语法才是问题):

  1. Python33
    目录中的目录从
    Lib
    重命名为
    lib
  2. crypt.py
    中的
    lib
    重命名为
    _crypt.py
    。然而,事实证明整个
    crypt.py
    模块也依赖于一个名为
    _crypt.py
    的外部模块。
  3. 浏览互联网远程下载任何类似于
    _crypt.py
  4. 的内容

这不是Python,对吧?是我...(?)我正在使用语法来导入和使用 2.7.3 中可接受的外部模块,但 3.3.2 中不可接受。还是我发现了 3.3.2 中的错误?

python windows python-2.7 python-3.3 crypt
6个回答
10
投票

更好的方法是使用 python passlib 模块,它生成兼容的 linux 密码的 crypt 哈希值(我想这就是你最可能想要的)。我已通过使用 Kickstart 文件将生成的散列密码值注入 rootpw 和用户属性来验证这一点。您需要的功能是:

from passlib.hash import md5_crypt as md5
from passlib.hash import sha256_crypt as sha256
from passlib.hash import sha512_crypt as sha512

md5_passwd = md5.encrypt(passwd, rounds=5000, implicit_rounds=True)
sha256_passwd = sha256.encrypt(passwd, rounds=5000, implicit_rounds=True)
sha512_passwd = sha512.encrypt(passwd, rounds=5000, implicit_rounds=True)

第一个参数是不言自明的。
第二个和第三个参数与规范合规性有关,并且需要生成 Linux 兼容的密码哈希值*** (请参阅:Passlib:SHA256 规范、格式和算法

***注意:使用 SHA512 进行测试,但我认为没有理由它不能与 SHA256 或 MD5 一起使用。


5
投票

我认为这是因为

crypt
是一个 Unix 特定服务

位于文档的顶部,用于

crypt

34.5。 crypt — 检查 Unix 密码的函数

平台:Unix


2
投票

我在这里找到了一个名为 fcrypt 的替代模块:

它很旧,所以不要指望 python3 兼容性。


0
投票

如果您使用的是Windows,您可以轻松使用bcrypt模块 Windows 和 Mac 均支持此功能。但是,如果在我自己的情况下错误仍然存在,请检查代码是否自动为您导入 crypt。


0
投票

我也有同样的问题。我试图在 Windows 10 上使用 crypt。我使用 sha512 解决了这个问题。

from passlib.hash import sha512_crypt as sha512
hash = sha512.hash(passwd, rounds=5000)

-3
投票

您可以在 Windows PC 上使用“bcrypt”来实现此目的,这样做是因为 crypt 只是一个 UNIX 模块,因此不容易在 Windows 中兼容。去bcrypt

import bcrypt
password = b"passit" #passit is the word to encrypt
pass = bcrypt.hashpw(password, bcrypt.gensalt())
print(b)

这将完成你的工作。 如需进一步参考,请访问: http://passlib.readthedocs.io/en/stable/install.html

https://pypi.python.org/pypi/bcrypt/2.0.0

© www.soinside.com 2019 - 2024. All rights reserved.