Invalid Length for a Base-64 Char Array

Invalid length for a Base-64 char array

The length of a base64 encoded string is always a multiple of 4. If it is not a multiple of 4, then = characters are appended until it is. A query string of the form ?name=value has problems when the value contains = charaters (some of them will be dropped, I don't recall the exact behavior). You may be able to get away with appending the right number of = characters before doing the base64 decode.

Edit 1

You may find that the value of UserNameToVerify has had "+"'s changed to " "'s so you may need to do something like so:

a = a.Replace(" ", "+");

This should get the length right;

int mod4 = a.Length % 4;
if (mod4 > 0 )
{
a += new string('=', 4 - mod4);
}

Of course calling UrlEncode (as in LukeH's answer) should make this all moot.

Invalid length for a Base-64 char array

it's value is: 4+mFeTp3tPF

You are receiving this error because that value, 4+mFeTp3tPF, is in fact not valid Base64.

Is it possible you are simply missing the required padding character, as so 4+mFeTp3tPF=?

c# decode base64 - Invalid length for a Base-64 char array or string

I've made some tests, and according to the comments you should not need the padding, but if you try to encode the decoded string:
{"x5t#S256":"wUYGYYzyu2O33mV65XqAW0bLYLyNSSeJdYFNWJ6zscI","x5t":"tWyf504i37Mvq7KuxErA68U18sY","kid":"SIGNING_KEY","alg":"RS256"}

it gives you:
eyJ4NXQjUzI1NiI6IndVWUdZWXp5dTJPMzNtVjY1WHFBVzBiTFlMeU5TU2VKZFlGTldKNnpzY0kiLCJ4NXQiOiJ0V3lmNTA0aTM3TXZxN0t1eEVyQTY4VTE4c1kiLCJraWQiOiJTSUdOSU5HX0tFWSIsImFsZyI6IlJTMjU2In0=

Notice the padding at the end.

Invalid length for a Base-64 char array or string

If you have the following encrypted Password:

dfghfgdfgd667878nnvghv

It can't be converted to a byte array from Base64, because it's not a valid Base64String. A valid Base64String would be:

dfghfgdfgd667878nnvghv==

System.FormatException: 'Invalid length for a Base-64 char array or string.'

Since your provided string does not completely fit criteria of FromBase64String method accepted values you need to add end symbol to follow the convention. It does not automatically add end symbols to your string.

The valueless character, "=", is used for trailing padding. The end of s can consist of zero, one, or two padding characters.

Source.

To fix issue you are having add "==" to the end of your string.

For example: string middle = "SomeString=="

Invalid length for a Base-64 char array while decryption issue

The query string includes encoded values, e.g. "%2b". That's at least inconvenient.

You could decode those values to end up with the original base64 value that you could then convert back to a byte array. but it would be better to use a web-safe base64 encoding to start with.

Convert.ToBase64String doesn't provide a URL-safe approach, but you can easily just use Replace on the result:

public static string WindowsEncrypted(string text)
{
byte[] plainBinary= Encoding.Unicode.GetBytes(text);
byte[] encrypted = ProtectedData.Protect(plainBinary, null, DataProtectionScope.LocalMachine);
string base64 = Convert.ToBase64String(encrypted);
// Return a url-safe string
return base64.Replace("+", "-").Replace("/", "_").Replace("=", ".");
}

public static string WindowsDecrypted(string text)
{
string base64 = text.Replace("-", "+").Replace("_", "/").Replace(".", "=");
byte[] encrypted = Convert.FromBase64String(base64);
byte[] plainBinary = ProtectedData.Unprotect(encrypted, null, DataProtectionScope.LocalMachine);
return Encoding.Unicode.GetString(plainBinary);
}

What causing this Invalid length for a Base-64 char array

I've seen this error caused by the combination of good sized viewstate and over aggressive content-filtering devices/firewalls (especially when dealing with K-12 Educational institutions).

We worked around it by storing Viewstate in SQL Server. Before going that route, I would recommend trying to limit your use of viewstate by not storing anything large in it and turning it off for all controls which do not need it.

References for storing ViewState in SQL Server:

MSDN - Overview of PageStatePersister

ASP Alliance - Simple method to store viewstate in SQL Server

Code Project - ViewState Provider Model

Invalid length for a Base-64 char array or string (while Decoding)

Just figured out that I was fetching my encrypted values wrongly.
Had a javascript split method which trimmed the == after my encrypted text.


My C# code was working fine. Just a little Googling and reading several posts on Stack helped me.

Here's my Jquery code to which I made minor changes.

        function GetParameterValues(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
/*var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}*/
var urlparam = url[0].substring(2, url[0].length);
return urlparam;
}
}



The commented section was creating problem as it was trimming the =(equal) sign towards the end.

Manuel Zelenka's comment gave me this hint.


I appreciate everyone's comments. And very big thanks to everyone.



Related Topics



Leave a reply



Submit