Python 在 Windows 中使用 crypt 模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19877867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:52:08  来源:igfitidea点击:

Using the crypt module in Windows?

pythonwindowspython-2.7python-3.3crypt

提问by adampski

In IDLE and Python version 3.3.2, I try and call the python module like so:

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

hash2 = crypt(word, salt)

I import it at the top of my program like so:

我在程序的顶部导入它,如下所示:

from crypt import *

The result I get is the following:

我得到的结果如下:

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'

However, when I execute the same file adams.pyin Ubuntu, with Python 2.7.3, it executes perfectly - no errors.

但是,当我adams.py在 Ubuntu 中使用 Python 2.7.3执行相同的文件时,它执行得很好 - 没有错误。

I tried the following to resolve the issue for my Windows & Python 3.3.2 (though I'm sure the OS isn't the issue, the Python version or my use of syntax is the issue):

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

  1. Rename the directory in the Python33directory from Libto lib
  2. Rename the crypt.pyin libto _crypt.py. However, it turns out the entire crypt.pymodule depends on an external module called _crypt.pytoo.
  3. Browsed internet to download anything remotely appropriate to resemble _crypt.py
  1. 将目录中的Python33目录重命名Liblib
  2. crypt.pyin重命名lib_crypt.py. 然而,事实证明整个crypt.py模块也依赖于一个被调用的外部模块_crypt.py
  3. 浏览互联网以下载任何适合远程类似的东西 _crypt.py

It's not Python, right? It's me...(?) I'm using syntaxes to import and use external modules that are acceptable in 2.7.3, but not in 3.3.2. Or have I found a bug in 3.3.2?

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

采纳答案by roippi

I assume that is because cryptis a Unix Specific Service.

我认为这是因为cryptUnix 特定服务

Right at the top of the docsfor crypt:

就在顶部文档crypt

34.5. crypt — Function to check Unix passwords

Platforms: Unix

34.5. crypt - 检查 Unix 密码的函数

平台:Unix

回答by Gringo Suave

I found an alternative module called fcrypt available here:

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

It's old, so don't expect python3 compatibility.

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

回答by Kounavi

A better approach would be to use the python passlib module which generates compatible crypt hashes of linux passwords (I assume that's what you most probably want). I've verified this by using Kickstart files by injecting the generated hashed password value in rootpw and user attributes. The functions you need are:

更好的方法是使用 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)

The first parameter is self-explanatory.
The second & third parameter have to do with specification compliance and are required to generate linux compatible password hashes***(see: Passlib: SHA256 spec, format & algorithm)

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

***NOTE: Tested with SHA512 but I see no reason why it shouldn't work with SHA256 or MD5.

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

回答by Akash Kumar

You can use instead 'bcrypt' for this purpose on a Windows PC, this is done because crypt is a UNIX module only so wont be compatible in windows easily. Go for bcrypt

为此,您可以在 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)

This will get your work done. For further reference, visit: http://passlib.readthedocs.io/en/stable/install.html

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

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

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