我必须以加密格式将密码字段存储在 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=