简体   繁体   English

在C#中使用Salt解密密码

[英]Decryption password using Salt in C#

Currently i am using salt to encrypt the password. 目前,我正在使用Salt加密密码。

public static SaltedHash Create(string password) 
        {
            string salt = _createSalt();
            string hash = _calculateHash(salt, password);
            return new SaltedHash(salt, hash);
        }

private static string _createSalt() 
        {
            byte[] r = _createRandomBytes(SALT_LENGTH);
            return Convert.ToBase64String(r);
        }
private static byte[] _createRandomBytes(int len) 
        {
            byte[] r = new byte[len];
            new RNGCryptoServiceProvider().GetBytes(r);
            return r;
        }
private static string _calculateHash(string salt, string password) 
        {
            byte[] data = _toByteArray(salt + password);
            byte[] hash = _calculateHash(data);
            return Convert.ToBase64String(hash);
        }
private static byte[] _toByteArray(string s) 
        {
            return System.Text.Encoding.UTF8.GetBytes(s);
        }
private static byte[] _calculateHash(byte[] data) 
        {
            return new SHA1CryptoServiceProvider().ComputeHash(data);
        }
/// <summary>
        /// This method verifies a password from a SaltedHash class.
        /// <param name="password">The password to verity</param>
        /// <returns>Boolean</returns>
        /// </summary>
        public bool Verify(string password) 
        {
            string h = _calculateHash(_salt, password);
            return _hash.Equals(h);
        }
/// <summary>
        /// This method creates a SaltedHash object from a salt and hash value. 
        /// <param name="salt">Salt value</param>
        /// <param name="hash">Hash value</param>
        /// <returns>SaltedHash class</returns>
        /// </summary>
        public static SaltedHash Create(string salt, string hash) 
        {
            return new SaltedHash(salt, hash);
        }

Now encryption is fine. 现在加密就可以了。 Now using the same technique i want to decrypt the password. 现在使用相同的技术,我想解密密码。

How to do this ? 这个怎么做 ? Thanks. 谢谢。

You are not encrypting the password, you are hashing it. 您没有加密密码,而是对其进行了哈希处理。

The idea of a hash is that it is a one-way function where it is computationally cheap to create a hash from original text, but computationally expensive to start with a hash and end up with plain text that would create that hash value. 哈希的想法是它是一种单向函数,从原始文本创建哈希在计算上很便宜,但从哈希开始到最终会创建该哈希值的纯文本在计算上却很昂贵。

Although there are various attacks to break SHA1 (your hash algorithm), there is no straightforward approach to "decrypt" the hashed value ("decrypt" in quotes means to find an input value that would correspond to the salted, hashed output value). 尽管有各种攻击可以破坏SHA1(您的哈希算法),但是没有直接的方法来“解密”哈希值(用引号引起的“解密”意味着找到与加盐的哈希输出值相对应的输入值)。

If you really do want to encrypt text, look into algorithms such as AES (also supported by the .NET framework). 如果您确实想加密文本,请查看诸如AES之类的算法(.NET框架也支持该算法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM