Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get this error everytime i try to login "Error in passwordDecodeInvalid length for a Base-64 char array or string."

Here is my class
C#
public string Encrypt(string password)
       {
           try
           {
               byte[] encPassword = new byte[password.Length];
               encPassword = System.Text.Encoding.UTF8.GetBytes(password);

               string encodedPassword = Convert.ToBase64String(encPassword);
               return encodedPassword;
           }
           catch (Exception ex)
           {
               throw new Exception("Error in PasswordEncode" + ex.Message);

           }

       }
       public string Decrypt(string password)
       {
           try
           {
               System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
               System.Text.Decoder passDecode = encoder.GetDecoder();
               byte[] decodedPassword = Convert.FromBase64String(password);
               int charCount = passDecode.GetCharCount(decodedPassword, 0, decodedPassword.Length);
               char[] decodedChar = new char[charCount];
               passDecode.GetChars(decodedPassword, 0, decodedPassword.Length, decodedChar, 0);
               string result = new string(decodedChar);
               return result;

           }
           catch (Exception ex)
           {
               throw new Exception("Error in passwordDecode" + ex.Message);
           }
       }
Posted
Comments
Maarten Kools 19-Aug-13 6:34am    
Check the parameter you're passing to your Decrypt method, because Decrypt(Encrypt("mypassword")) works perfectly fine. (Possibly you'll want to call Encrypt instead?)

1 solution

During decoding, you could first get the string equivalent of the base64 string, then read the utf8 bytes of the string then decode it and convert it back to string. Something like:

C#
...

using System.Text;

...

public string Decrypt(string password)
{
     try
     {
        string temp = Convert.FromBase64String(password);

        byte[] decryped = Encoding.UTF8.GetBytes(temp) 

        return (string)decrypted;
     }
     catch { ... }
     
     ...
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900