I am making a Registration and a login Form with encoded password saved to database, I can encode the password using encodin.utf8.
Problem is I register with some username and password and the data get saved to database with encoded password, but when I login with the same data it shows me this below error.
Invalid length for a Base-64 char array or string.
Can anyone give me the described solution for this. Below I am attaching my encoded and decoded methods :
Encoded :
- private string encryption(string clearText)
- {
- string encryptkey = "123";
- byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
- using (Aes encrypt = Aes.Create())
- {
- Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(encryptkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encrypt.Key = rdb.GetBytes(32);
- encrypt.IV = rdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cst = new CryptoStream(ms, encrypt.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cst.Write(clearBytes, 0, clearBytes.Length);
- cst.Close();
- }
- clearText = Convert.ToBase64String(ms.ToArray());
- }
- }
- return clearText;
Decoded :
- private string decryp(string cipherText)
- {
- cipherText = cipherText.Replace(" ", "+");
- string decryptkey = "123";
- byte[] cipherBytes = Convert.FromBase64String(cipherText.Replace(" ", "+"));
- using (Aes encrypt = Aes.Create())
- {
- Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(decryptkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encrypt.Key = rdb.GetBytes(32);
- encrypt.IV = rdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cst = new CryptoStream(ms, encrypt.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cst.Write(cipherBytes, 0, cipherBytes.Length);
- cst.Close();
- }
- cipherText = Encoding.Unicode.GetString(ms.ToArray());
- }
- }
- return cipherText;
Any help will be appreciated,thank you !
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

ketan borsdiyaPosted Sep 13, 2016, 12:32 AM
Tapan PatelPosted Sep 12, 2016, 2:43 PM
Nilesh JadavPosted Sep 12, 2016, 11:37 AM
Invalid length for a Base-64 char array or string.
Vishal JadavPosted Sep 12, 2016, 10:18 AM
ketan borsdiyaPosted Sep 12, 2016, 9:17 AM