【发布时间】:2012-09-25 06:33:04
【问题描述】:
我必须将密码字段以加密格式存储在 SQL Server 数据库中,并且我必须在用户登录系统时对其进行解密。加密部分工作正常。但是我在解密部分收到错误,即“Base-64 char array 的长度无效”
byte[] todecode_byte = Convert.FromBase64String(encryptpwd);
解密模块。
private string Encryptdata(string password)
{
string encryptpwd = string.Empty;
byte[] encode = new byte[password.Length];
encode = Encoding.UTF8.GetBytes(password);
encryptpwd = Convert.ToBase64String(encode);
return encryptpwd;
}
private string Decryptdata(string encryptpwd)
{
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd); //here I am getting error as "Invalid length for a Base-64 char array"
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;
}
输入数据:prabu
加密数据:cHJhYnU=
【问题讨论】:
-
你根本没有加密...编码只是关于 ANSI/UTF8/... 表示。
-
这段代码运行良好。没有错误。你确定你用 "cHJhYnU=" 参数调用 Decryptdata 方法吗?
-
我建议您查看密码中的one way hashing,让能够解密密码的代码也允许任何攻击者这样做。
-
拜托,拜托,请阅读以下内容:codinghorror.com/blog/2007/09/…
-
标签: c# .net encryption