【问题标题】:Why are the encrypted passwords not the same?为什么加密后的密码不一样?
【发布时间】:2023-01-21 15:33:29
【问题描述】:

2 个变量“x”和“l”的输出应该是相同的,因为它们使用完全相同的加盐和加密过程。

我想要一些关于为什么它不一样的信息,以及我如何为一个简单的登录算法修复它。

代码:

import os
import hashlib
import mysql.connector
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64


def encrypt_password(password, salt=None):
    # Create a password hash
    if not salt:
        salt = os.urandom(64)
        #print("salt", salt)
        #print("1", salt)
        print("...")
        token = base64.b64encode(salt).decode('utf-8')
        #print("2", token)

        #print("3", salt_again)
        #print(salt == salt_again)

    else: pass #token = salt
    password = password.encode()
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256,
        iterations=100000,
        length=32,
        salt=salt,
        backend=default_backend()
    )
    key = base64.urlsafe_b64encode(kdf.derive(password))
    # Encrypt the password
    f = Fernet(key)
    encrypted_password = f.encrypt(password)
    return encrypted_password, salt

x, salt2 = encrypt_password("Hello")
#print(x)
print(salt2)
l, salt1 = encrypt_password("Hello", salt2)
#print(l)
print(salt1)
print(salt1 == salt2)

我已经检查了 20 次盐,我几乎可以肯定它们在任何过程中都不会发生变化。我能想到的是,对于过程中使用的任何不一致的库,我都不知道。

【问题讨论】:

  • 我在下面修复了答案,它现在应该可以工作了!
  • 您的新答案毫无意义,它会破坏整个程序而不是首先加密密码。

标签: python cryptography base64


【解决方案1】:

else 声明后有 pass。删除它应该可以解决问题:

import os
import hashlib
import mysql.connector
from cryptography.fernet import Fernet
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64


def encrypt_password(password, salt=None):
    # Create a password hash
    if not salt:
        salt = os.urandom(64)
        #print("salt", salt)
        #print("1", salt)
        print("...")
        token = base64.b64encode(salt).decode('utf-8')
        #print("2", token)

        #print("3", salt_again)
        #print(salt == salt_again)

    else: #token = salt
        password = password.encode()
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256,
            iterations=100000,
            length=32,
            salt=salt,
            backend=default_backend()
        )
        key = base64.urlsafe_b64encode(kdf.derive(password))
        # Encrypt the password
        f = Fernet(key)
        encrypted_password = f.encrypt(password)
    return encrypted_password, salt

x, salt2 = encrypt_password("Hello")
#print(x)
print(salt2)
l, salt1 = encrypt_password("Hello", salt2)
#print(l)
print(salt1)
print(salt1 == salt2)

如果不进行此更改,如果将 salt 变量传递给 encrypt_password,函数将不会返回任何内容。

【讨论】:

  • 不幸的是,这不会改变任何东西。
  • 我得到 IndentationError
  • @Pingu 在哪条线上?
  • @Pingu 删除整个 else 语句,它的作用与删除“pass”相同,但它没有解决任何问题。 pass 语句不执行任何操作,它只是跳过随机生成盐但继续正常运行的部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多