Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting response from api in json format which is encoded I have stored json response in a table.I want to decode response and deserilize it. When I am trying to decode the data ,getting error.

Error : The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

Actual Response in json :
data={"Id": "234", "Name": "pinky", "MobileNumber": "", "ClassName": "Class1_Physics",   "DOBTime": "1990-04-11 15:46:38", "Landline": "", "Status": "Unmarried"}


After Encoding :
data%3D%7B%22Id%22%3A%20%22234%22%2C%20%22Name%22%3A%20%22pinky%22%2C%20%22MobileNumber%22%3A%20%22%22%2C%20%22ClassName%22%3A%20%22Class1_Physics%22%2C%20%20%20%22DOBTime%22%3A%20%221990-04-11%2015%3A46%3A38%22%2C%20%22Landline%22%3A%20%22%22%2C%20%22Status%22%3A%20%22Unmarried%22%7D%0A


I have taken help of one of tool for decoding . Code : // Fetching response which was stored in database and assigning to string called 'Name'

string base64Decoded, base64Decoded2;
                base64Decoded = Name.Trim();
            base64Decoded = base64Decoded.Replace('-', '+');
            base64Decoded = base64Decoded.Replace('_', '/');
            base64Decoded = base64Decoded.Replace('=', '/');
            base64Decoded = base64Decoded.Replace(':', '/');
            byte[] data2 = System.Convert.FromBase64String(base64Decoded);
            base64Decoded2 = System.Text.ASCIIEncoding.ASCII.GetString(data2);


Getting error at this line :

byte[] data2 = System.Convert.FromBase64String(base64Decoded);


What I have tried:

string base64Decoded, base64Decoded2;
                base64Decoded = Name.Trim();
            base64Decoded = base64Decoded.Replace('-', '+');
            base64Decoded = base64Decoded.Replace('_', '/');
            base64Decoded = base64Decoded.Replace('=', '/');
            base64Decoded = base64Decoded.Replace(':', '/');
            byte[] data2 = System.Convert.FromBase64String(base64Decoded);
            base64Decoded2 = System.Text.ASCIIEncoding.ASCII.GetString(data2);
Posted
Updated 11-Apr-17 20:49pm
Comments
Peter_in_2780 12-Apr-17 2:49am    
The string you show as "after encoding" is *NOT* base64 encoded. It simply has all non-alphanumerics %-hex encoded, sort of like URLencoded, but not even that.
Member 11589429 12-Apr-17 3:19am    
Yes, I solved it with HttpUtility.UrlDecode(base64Decoded)

1 solution

Quote:
Error while decoding. The input is not a valid base-64 string as it contains a non-base 64 character, more than two padding characters

The message tell you all, you get the error message because your input is not base64 encoded.
Just compare encoded data with decoded, if you look carefully, some parts are encoded and others are not.
data=  {  "  Id"  :     "  234"  ,     "  Name"  :     "  pinky"  ...
data%3D%7B%22Id%22%3A%20%22234%22%2C%20%22Name%22%3A%20%22pinky%22...

you just have to replace the ones by the others.
 
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