question

AdeelMirza-7596 avatar image
0 Votes"
AdeelMirza-7596 asked LanHuang-MSFT answered

Problem with Encryption decryption

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


dotnet-aspnet-webforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @AdeelMirza-7596,

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.