Problem with Encryption decryption

Adeel Mirza 121 Reputation points
2022-03-22T10:33:47.587+00:00

I am using this source code to encrypt and decrypt the password

public static string EncodePasswordToBase64(string password) 
{
   try 
   {
      byte[] encData_byte = new byte[password.Length]; 
      encData_byte = System.Text.Encoding.UTF8.GetBytes(password); 
      string encodedData = Convert.ToBase64String(encData_byte); 
      return encodedData; 
   } 
   catch (Exception ex) 
   { 
      throw new Exception("Error in base64Encode" + ex.Message); 
   } 
}
//this function Convert to Decord your Password
public string DecodeFrom64(string encodedData) 
{
   System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); 
   System.Text.Decoder utf8Decode = encoder.GetDecoder();
   byte[] todecode_byte = Convert.FromBase64String(encodedData); 
   int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); 
   char[] decoded_char = new char[charCount]; 
   utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); 
   string result = new String(decoded_char); 
   return result;
}

When I implement it.

string password = DecodeFrom64(txtPassword.Text.Trim());

I am coming across this error.

The input is not a valid Base-64 string as it contains a non-base 64 character

How can I resolve this issue

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-03-23T03:19:03.353+00:00

    Hi @Adeel Mirza ,

    The input is not a valid Base-64 string as it contains a non-base 64 character

    The exact reason you're getting such an error is because it's not a valid Base64 string.
    The Convert.FromBase64String(String) method converts the specified string to an equivalent array of 8-bit unsigned integers that encodes binary data as base-64 numbers.
    FormatException
    The length of string, ignoring white-space characters, is not zero or a multiple of 4.
    The solution is that you need a string length divisible by 4, otherwise you pad it with equal signs.

     if (encodedData.Length % 4 != 0)  
          encodedData += new String('=', 4 - encodedData.Length % 4);  
    

    -or-
    The format of string is invalid. string contains a non-base-64 character, more than two padding characters, or a non-white space-character among the padding characters.

    encodedData = encodedData.Replace('#', '=');  
    

    If there are special characters that need to be replaced, note that there can only be at most two padding characters
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.