pyt*_*bie 5 python encryption crypt module python-3.x
我在查找 python 模块时,发现了一个叫做“crypt”的东西。我不明白。我试过阅读这个,这是什么“盐”的东西,这个 crypt 模块的用途是什么,有没有某种方法可以将“crypt”应用于这段 python 代码?:
import crypt
max_attempts = 3
attempt = 0
try:
while attempt < max_attempts:
uname = input('Username: ')
password = input('pass: ')
if uname == 'admin' and password == 'Khs9':
print('Welcome Admin')
break
else:
attempt += 1
if attempt == max_attempts:
raise RuntimeError("\nYou've reached the maximum number of attempts allowed.")
else:
print('Wrong credentials.\n Try again or press <ctrl+c> to exit.\n')
continue
except KeyboardInterrupt:
print('Terminated by the user.\nGood-bye.')
except RuntimeError as e:
print("Goodbye")
Run Code Online (Sandbox Code Playgroud)
现在我已经看到你的代码,我知道密码是“Khs9”,我可以登录到你的盒子。
您可以私下运行以下命令。
>>> crypt.crypt('Khs9', 'aa')
'aa0GPiClW35DQ
Run Code Online (Sandbox Code Playgroud)
现在您可以这样更新代码:
import crypt
max_attempts = 3
attempt = 0
stored_pw_hash = 'aa0GPiClW35DQ'
try:
while attempt < max_attempts:
uname = input('Username: ')
entered_pw_hash = crypt.crypt(input('pass: '), stored_pw_hash)
if uname == 'admin' and entered_pw_hash == stored_pw_hash:
print('Welcome Admin')
break
else:
attempt += 1
if attempt == max_attempts:
raise RuntimeError("\nYou've reached the maximum number of attempts allowed.")
else:
print('Wrong credentials.\n Try again or press <ctrl+c> to exit.\n')
continue
except KeyboardInterrupt:
print('Terminated by the user.\nGood-bye.')
except RuntimeError as e:
print("Goodbye")
Run Code Online (Sandbox Code Playgroud)
现在,如果您的代码被泄露,他们将无法立即访问。您应该有足够的时间意识到自己被黑客入侵,然后更改密码。
这是背景信息...
crypt.crypt(password) 将返回密码的哈希值。您存储哈希值而不是明文密码。这样,您就不会因为没有密码而将密码丢失给黑客。丢失哈希值并不是一个大问题,因为它不能保证访问(如果您遵循最佳实践,其中包括使用盐)。
下次有人提供密码时,您会计算它的哈希值,将其与您之前存储的哈希值进行比较,如果它们匹配,您就知道他们给了您正确的密码。
为什么需要使用盐?因为有人花了很长时间来生成一个包含常用密码和哈希值的表。完成后,可以通过快速检查来破解哈希值。通过使用盐,您可以确保应用不同的查找表,该查找表可能不可用,并且普通黑客没有时间生成它。
crypt.crypt() 需要两个字符来用作盐。您可以向其传递两个字符的字符串或使用该函数之前的输出。(crypt.crypt() 返回一个字符串,前两个字符是盐,其余是哈希值)
我查看了https://docs.python.org/3.4/library/crypt.html来回答这个问题。
| 归档时间: |
|
| 查看次数: |
14620 次 |
| 最近记录: |