Pooja K Bhatt
Pooja K Bhatt

Reputation: 1027

The input is not a valid Base-64 string as it contains a non-base 64 character, an illegal character among the padding characters

I am trying to convert base64string to readable string but it gives error of "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."

But when I am doing same on online converter, e.g. here's a link, it is working.

string base64String="DQpUZXN0DQoNCkNvbmZpZGVudGlhbGl0eSBOb3RpY2U6IFRoaXMgZS1tYWlsLCBhbmQgYW55IGF0dGFjaG1lbnQgdG8gaXQsIGNvbnRhaW5zIHByaXZpbGVnZWQgYW5kIGNvbmZpZGVudGlhbCBpbmZvcm1hdGlvbiBpbnRlbmRlZCBvbmx5IGZvciB0aGUgdXNlIG9mIHRoZSBpbmRpdmlkdWFsKHMpIG9yIGVudGl0eSBuYW1lZCBvbiB0aGUgZS1tYWlsLiBJZiB0aGUgcmVhZGVyIG9mIHRoaXMgZS1tYWlsIGlzIG5vdCB0aGUgaW50ZW5kZWQgcmVjaXBpZW50LCBvciB0aGUgZW1wbG95ZWUgb3IgYWdlbnQgcmVzcG9uc2libGUgZm9yIGRlbGl2ZXJpbmcgaXQgdG8gdGhlIGludGVuZGVkIHJlY2lwaWVudCwgeW91IGFyZSBoZXJlYnkgbm90aWZpZWQgdGhhdCByZWFkaW5nIHRoaXMgZS1tYWlsIGlzIHN0cmljdGx5IHByb2hpYml0ZWQuIElmIHlvdSBoYXZlIHJlY2VpdmVkIHRoaXMgZS1tYWlsIGluIGVycm9yLCBwbGVhc2UgaW1tZWRpYXRlbHkgcmV0dXJuIGl0IHRvIHRoZSBzZW5kZXIgYW5kIGRlbGV0ZSBpdCBmcm9tIHlvdXIgc3lzdGVtLg0K="; 
var bytes = Convert.FromBase64String(base64String);
var decodededString = Encoding.UTF8.GetString(bytes);
Console.WriteLine(decodededString );

See this enter image description here

Upvotes: 2

Views: 23152

Answers (2)

Pooja K Bhatt
Pooja K Bhatt

Reputation: 1027

I have found answer. Following is the sample code to convert any base64 string to readable utf8 string ::

    static string GetStringFromBase64String(string msg)
    {

       string _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        StringBuilder msgString = new StringBuilder();      
        int len = 0;

        while (len < msg.Length)
        {
            int enc1 = _keyStr.IndexOf(msg[len++]);
            int enc2 = _keyStr.IndexOf(msg[len++]);
            int enc3 = _keyStr.IndexOf(msg[len++]);
            int enc4 = _keyStr.IndexOf(msg[len++]);

            int chr1 = (enc1 << 2) | (enc2 >> 4);
            int chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            int chr3 = ((enc3 & 3) << 6) | enc4;

            msgString.Append(Convert.ToChar(chr1));

            if (enc3 != 64)
            {
                msgString.Append(Convert.ToChar(chr2));
            }
            if (enc4 != 64)
            {
                msgString.Append(Convert.ToChar(chr3));
            }
        }
        string final = GetUtf8String(msgString.ToString());
        return final;
    }

    static string GetUtf8String(string msg)
    {
        string utftext = "";
        msg = msg.Replace("\r", "");
        msg = msg.Replace("\n", "");
        msg = msg.Replace("\r\n", "");
        msg = msg.Replace("\r\r", "");
        msg = msg.Replace("\n\n", "");

        msg = msg.Replace("\\r", "");
        msg = msg.Replace("\\n", "");
        msg = msg.Replace("\\r\\n", "");
        msg = msg.Replace("\\r\\r", "");
        msg = msg.Replace("\\n\\n", "");
        for (var n = 0; n < msg.Length; n++)
        {

            int c = msg[n];

            if (c < 128)
            {
                utftext += Convert.ToChar(c);
            }
            else if ((c > 127) && (c < 2048))
            {
                utftext += Convert.ToChar((c >> 6) | 192);
                utftext += Convert.ToChar((c & 63) | 128);
            }
            else
            {
                utftext += Convert.ToChar((c >> 12) | 224);
                utftext += Convert.ToChar(((c >> 6) & 63) | 128);
                utftext += Convert.ToChar((c & 63) | 128);
            }

        }

        return utftext;
    }

Upvotes: 1

Xueli Chen
Xueli Chen

Reputation: 12685

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.

From the error message , you should check if the length of base64string is a multiple of 4 and the base64string is valid .

A Base64 string will only be composed of characters 'A'..'Z', 'a'..'z', '0'..'9', '+', '/' and it is often padded at the end with up to two '=', to make the length a multiple of 4.

As madreflection commented , your original base64string length is 713, not a multiple of 4, you should delete the last "=" padding character to make the base64string valid .

Upvotes: 5

Related Questions