加密&使用C#解密密码

发布于 2024-10-08 15:30:54 字数 181 浏览 0 评论 0原文

我想创建一个可以使用 C# 加密和解密我的密码的应用程序。

我的想法很简单。我将使用 Substring 从输入的字符串中提取每个字母,然后操作 ASCI 代码并将其转换为另一个字母。我将使用相同的方法来加密它们。

有人解密我生成的密码有多困难?

我正在寻找建议或示例代码。

I want to create an application that can encrypt and decrypt my passwords using C#.

My idea is simple. I will use Substring to extract each letter from the entered string and I will manipulate the ASCI code and convert it to another letter. I will use the same method to encrypt them.

How difficult is it for someone to decrypt my generated password?

I'm looking for either suggestions or example code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

昔日梦未散 2024-10-15 15:30:54

在 .NET 中,有很多安全的加密方法。我认为,与其尝试自己实现解决方案,不如看看 系统.安全.加密

In .NET there are a lot of secure ways for encryption. I think that instead of trying to implement yourself a solution you should better take a look at System.Security.Cryptography.

影子的影子 2024-10-15 15:30:54

凯撒密码是出了名的不安全。认为此方法无果而放弃。对密码保护进行更多研究。

Caesar cyphers are notoriously insecure. Abandon this method as fruitless. Do more research on password protection.

合久必婚 2024-10-15 15:30:54

很少有理由存储密码的加密版本。这会产生安全漏洞。相反,通常最好将密码的单向哈希(例如使用 SHA1)与随机盐相结合来存储。然后,您始终将输入密码的哈希值与数据库中存储的哈希值进行比较,而不是实际比较密码。

这种方法的好处是没有人可以确定用户的密码是什么,即使他或她获得了数据库的访问权限。并且盐使得相同的密码看起来彼此不同。

以下是使用 System.Security.Cryptography 命名空间创建随机盐的示例。

byte[] salt = new byte[10];
RandomNumberGenerator.Create().GetBytes(salt);

您可以将盐与密码组合起来并生成单向哈希,如下所示:

byte[]  passwordBytes = new byte[Encoding.UTF8.GetByteCount(password) + salt.Length];  // Create buffer for password bytes and hash
int passwordLength = Encoding.UTF8.GetBytes(password, 0, password.Length, passwordBytes, 0);
salt.CopyTo(passwordBytes, passwordLength);
byte[] hash = null;
using (SHA512Managed hasher = new SHA512Managed()) {
  hash = hasher.ComputeHash(passwordBytes);
}

同时存储哈希密码和盐。验证用户身份时,请使用与创建存储哈希时使用的盐相同的盐来对用户输入的密码进行哈希处理。将这个新哈希值与数据库中的哈希值进行比较。

There is rarely ever a reason to store an encrypted version of a password. That creates a security vulnerability. Instead, it is usually best to store a one-way hash (such as using SHA1) of the password combined with a random salt. Then you always compare the hash of entered passwords against hashes stored in the database, rather than ever actually comparing passwords.

The benefit of this approach is that no one can determine what a user's password is, even if he or she gains access to the database. And the salt makes identical passwords appear different from one another.

The following is an example of the creation of a random salt using the System.Security.Cryptography namespace.

byte[] salt = new byte[10];
RandomNumberGenerator.Create().GetBytes(salt);

You can combine the salt with the password and generate a one-way hash as follows:

byte[]  passwordBytes = new byte[Encoding.UTF8.GetByteCount(password) + salt.Length];  // Create buffer for password bytes and hash
int passwordLength = Encoding.UTF8.GetBytes(password, 0, password.Length, passwordBytes, 0);
salt.CopyTo(passwordBytes, passwordLength);
byte[] hash = null;
using (SHA512Managed hasher = new SHA512Managed()) {
  hash = hasher.ComputeHash(passwordBytes);
}

Store both the hashed password and the salt. When authenticating a user, use the same salt as that used when creating the stored hash to hash the password entered by the user. Compare this new hash to the one in the database.

一身骄傲 2024-10-15 15:30:54

有些库可以为您加密数据。他们的加密算法比你能想出的任何算法都要好。使用它们。

There are libraries that encrypt data for you. Their encryption algorithms are better than anything you can come up with. Use them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文