使用 bcrypt 在 Python 中加密密码2025年1月11日 | 阅读时长11分钟 加密至关重要,因为它允许您安全地保护不想让其他人看到的数据。企业用它来保护商业秘密,政府用它来保护机密信息,许多人则用它来保护个人信息以避免身份盗窃。为了安全地将用户凭据保存在数据库中,需要密码加密。任何可以访问公司服务器上用户数据库的人(包括黑客)如果密码未加密,都可能轻易查看任何存储的密码。如果没有密码加密,即使是使用安全密码生成器生成的32位强密码也毫无意义!如果您的密码在服务器上被读取,任何人都可以通过复制粘贴来使用它——无论密码有多长或多难! 在将密码存储到服务器之前,加密会将其打乱。因此,如果有人入侵服务器,他们会发现一串随机的字母和数字,而不是“password123”。 理解密码加密为了成功描述密码加密,我们首先必须理解其术语。一些概念可能比较新,所以这里简要介绍一下密码加密的术语。
当您保存新密码时,哈希函数会生成新密码的哈希版本并将其保存在服务器上。每次您使用密码登录时,哈希函数都会重新创建哈希值以确定它是否与存储的匹配。如果哈希值匹配,算法将通过身份验证并让您登录。 例如
简单的哈希密码,尽管看起来很安全,但并非防黑客。哈希算法为每个密码生成一个不同的哈希值,而不是为每个用户生成。因此,如果多个用户拥有相同的密码,如Pa$$w0rd123,哈希值将是相同的。工程师使用加盐密码来规避这种加密缺陷,确保即使密码相似,每个哈希值也是唯一的。 密码加密隐藏用户密码,使其难以猜测或解码。这是创建安全的用户基础软件的关键一步。无论您是使用Flask还是其他轻量级Python框架,都不能跳过此步骤。这就是bcrypt发挥作用的地方。 bcrypt的工作原理Niels Provos和David Mazières创建了bcrypt密码哈希算法,该算法基于Blowfish密码。bcrypt函数是OpenBSD的默认密码哈希算法。Bcrypt可用于C、C++、C#、Java、JavaScript、PHP、Python等编程语言。Bcrypt是一个多语言哈希库,提供独一无二的密码加密。默认情况下,它在加密字符串时会创建额外的随机字符(盐)以提高密码的安全性。您还可以选择指定要向传入字符串添加的额外字符数量。bcrypt库只读取字节码,而不读取原始字符串。首先,您需要对传入的密码字符串进行编码,然后将其交给bcrypt进行加密。加密与编码不同。它只是确保文本在被加密技术屏蔽之前是机器可读的。 使用bcrypt在Python中加密密码Python使bcrypt密码加密变得简单。我们将重点介绍如何在不使用框架的情况下实现它。但是,如果您了解如何从数据库中保存和读取用户输入,那么在框架中也遵循相同的过程。 代码 因此,在上述代码中,我们创建了两个类,用于指定输入字符串的加密和解密。我们为指定输入字符串的加密和解密创建了不同的函数,这些字符串参数是用户输入的数值。在主函数中,我们创建了这些加密和解密类的对象,并借助相应的类对象调用了这些函数。用户可以通过菜单驱动的方式选择不同的操作,例如加密特定的输入字符串或解密输入字符串,或者选择退出当前代码执行。根据用户提供的选择,将采取进一步的步骤,并且执行将持续进行,直到用户退出代码执行。 成功运行上述代码后,现在让我们看看此代码生成的输出。 输出 Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 1 >Enter the string that you want to convert to the hashed string: iamlearningpythonfromjtp The encrypted text or password is: b'$2b$16$CGuTBdy09v0a/KFwlcQOuOzzwA8XHuzt0X5QggyCKVR8PhzYqWgqG' Do you want to continue or exit the code execution? [y/n] y Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 2 >Enter the string that you want to check against the hashed string: iamlearningpythonfromjtp The entered string has matched successfully with the hashed password/string. Do you want to continue or exit the code execution? [y/n] y Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 1 >Enter the string that you want to convert to the hashed string: iamagainlearningpython The encrypted text or password is: b'$2b$16$NqKG8sBxs.X0AX/fAOEtFOrbnimCQ/ybGkVJQ8hjIRcshhhBgpbE2' Do you want to continue or exit the code execution? [y/n] y Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 2 >Enter the string that you want to check against the hashed string: IamAgainLearningPython The entered string has not matched with the hashed password/string. Do you want to continue or exit the code execution? [y/n] y Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 1 >Enter the string that you want to convert to the hashed string: learning_python_feels_good The encrypted text or password is: b'$2b$16$maGOvCTJkTG1E96sWFAFBeZ6YrhZ0kEYcklazJEzA6KCtOafjp3M2' Do you want to continue or exit the code execution? [y/n] y Please choose one of the appropriate options: 1. To enter a string and print the resultant hashed string using bcrypt. 2. To enter a string and check if it matches the hashed password using bcrypt. 3. To exit from the code execution. 2 >Enter the string that you want to check against the hashed string: learning_python_feels_good The entered string has matched successfully with the hashed password/string. Do you want to continue or exit the code execution? [y/n] n 我们可以看到,成功运行上述代码后,系统会提示用户三个选项:第一个选项是输入字符串并打印其结果哈希字符串;第二个选项是解密输入字符串;第三个选项是用户可以选择退出代码执行的选项。我们为加密和解密函数都提供了不同的输入,并通过打印不同的加密和密字符串验证了这两个函数的结果。最后,用户选择了选项3退出了代码执行。 加密的优点每个人都担心将敏感数据移动到云端,因为许多公司认为云端不如他们自己的数据中心安全。外部人员可以访问云端数据,但客户和竞争对手的数据存储在同一位置。公司需要云端的好处,因为它具有巨大的可负担性和灵活性。此功能包括在市场需求变化时启动或停用服务器的能力。那么,如果服务提供商要求离开怎么办?虚拟化环境可以实现多租户,以及更高的灵活性和成本节约。 如果数据已加密且加密密钥都存在,服务提供商将能够访问它。为了解决这个问题,在云端执行数据加密并将加密密钥保存在用户端是有意义的。无论密钥安全解决方案多么基本,某些公司仍拒绝处理加密密钥。他们对备份、定价和灾难恢复存在担忧。 支付卡用于各种交易,卡及其相关数据必须受到保护。大多数持卡人知道他们的个人信息和数据是安全可靠的。因此,加密是PCI DSS(支付卡行业数据安全标准)最有效的策略之一。 如果发生数据泄露并删除个人信息,必须联系受影响的个人。如果被拦截的数据是安全的且安全密钥未被破解,任何司法管辖区都有带安全港条款的公开通知。因此,在发生泄露时,实施加密和彻底的密钥保护可以节省大量资金。 许多组织现在将其互联网服务作为虚拟办公室提供,这些服务本身并不受保护。机器和存储盗窃是一个非常真实的危险。这些公司许多服务器上都有不安全的机密数据。数据加密可防止数据修改或意外销毁,当今的安全技术已经扩展了可能性。 考虑仅在工作时间向远程数据发送加密密钥,如果断电,则使代码无效。 此外,与文件或文件夹加密不同,全盘加密(FDE)在数据存储在硬盘上时对其进行加密。换句话说,加密过程是自动执行的。因此,加密文件或文件夹要容易得多,但您必须手动选择要加密的文件或目录。 加密的一些缺点
因此,在本文中,我们探讨了如何使用Python提供的bcrypt库作为模块来加密和解密输入字符串。我们还回顾了此功能的各种用例以及bcrypt库的各种优缺点。 |
我们请求您订阅我们的新闻通讯以获取最新更新。