我必须以加密格式将密码字段存储在SQL Server数据库中,并且我必须在用户登录系统时解密它。加密部分工作正常。但我解密部分的错误为“无效长度为Base-64字符数组”在行密码解密中的错误
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方法吗? –
我建议你看一下密码的[单向哈希](http://en.wikipedia.org/wiki/Cryptographic_hash_function#Password_verification),让代码能够解密密码,这样攻击者也可以这么做。 –