我想在数据库中保存加密值,同时从数据库中获取值,我想解密并在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 char数组的长度无效。我正在使用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;
}
答案 0 :(得分:3)
如果您要将密码存储在数据库中,除非您有充分的理由需要能够获取纯文本,您应该对密码进行哈希处理,而不是加密或以纯文本格式存储。
加密和散列之间的区别在于加密可以从您无法进行散列的位置检索纯文本。当您存储密码时,您应该使用用户提供的密码并对其进行哈希处理(理想情况下使用盐),然后当用户尝试使用密码下次登录时,再次对其进行哈希处理,然后将存储的哈希值与一个你刚刚生成的,如果匹配则它们是相同的。
我在这里(Password storage, how to do it right)写了更详细的解释。
我的网站上有一些散列函数可以使用代码(在VB.NET中,但很容易转移到C#),最好的方法是使用SHA512(Calculate the SHA512 hash of string or file)。
如果您仍然不确定哈希等,请随意说出您不理解的内容,我会尽力帮助:)