0

我想在数据库中保存一个加密的值,在从数据库中获取值的同时,我想解密并在 UI 中显示该值。这是我使用的代码

private string Decryptdata(string encryptpwd)
{
    string decryptpwd = string.Empty;
    UTF8Encoding encodepwd = new UTF8Encoding();
    Decoder Decode = encodepwd.GetDecoder();
    encryptpwd = encryptpwd.Replace(" ", "+");
    byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
    int charCount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
    char[] decoded_char = new char[charCount];
    Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
    decryptpwd = new String(decoded_char);
    return decryptpwd;
}

但是我收到错误,因为Base-64 字符数组的长度无效。我正在使用 c#.net

加密功能:

Encryptdata(string password) { 
       string strmsg = string.Empty; 
       byte[] encode = new byte[password.Length]; 
       encode = Encoding.UTF8.GetBytes(password); 
       strmsg = Convert.ToBase64String(encode); 
       return strmsg; 
}
4

1 回答 1

3

如果您将密码存储在数据库中,除非您有充分的理由需要能够获取纯文本,否则您应该对密码进行散列处理,而不是将它们加密或以纯文本形式存储。

加密和散列之间的区别在于,加密可以检索纯文本,而散列则不能。当您存储密码时,您应该获取用户提供的密码并对其进行哈希处理(最好使用盐),然后当用户下次尝试使用密码登录时,您再次对其进行哈希处理,然后将存储的哈希值与您刚刚生成的一个,如果它们匹配,那么它们是相同的。

我已经在这里写了这个(密码存储,如何正确操作)并给出了更全面的解释。

我的网站上有一些带有代码的散列函数(在 VB.NET 中,但它们很容易移至 C#),可能最好的方法是使用 SHA512(计算字符串或文件的 SHA512 散列)。

如果您仍然不确定哈希等,请随时说出您不明白的内容,我会尽力提供帮助:)

于 2013-04-17T10:48:16.133 回答