Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I solve a "base64 invalid characters" error?

When I m trying to convert the value1 to byte[] using the following code:

string value1 = "4rdHFh%2BHYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ%3D";
byte[] value2 = Convert.FromBase64String(value1);

Ι received an error that the string contents invalid base-64 characters...

What's wrong?

Thanks!

like image 943
voimak Avatar asked May 27 '13 13:05

voimak


People also ask

What characters are invalid in Base64?

The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'. % is not allowed in base64 encoding. Save this answer.

What is Base64 error?

This error happens when the string that you are trying to transform contains a character not recognized by the basic Base 64 Alphabet (in this case it was an underscore character). Below you can see which characters are accepted. ietf.org - RFC 4648.

What are Base64 characters?

BASE64 characters are 6 bits in length. They are formed by taking a block of three octets to form a 24-bit string, which is converted into four BASE64 characters.

What does invalid length for a base 64 char array or string mean?

Base64 encoding uses some characters which must be encoded if they're part of a querystring (namely + and / , and maybe = too). If the string isn't correctly encoded then you won't be able to decode it successfully at the other end, hence the errors.


1 Answers

From Convert.FromBase64String

s is composed of base 64 digits, white space characters, and trailing padding characters. The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'.

% is not allowed in base64 encoding.

Check out The Base64 index table from Wikipedia

Value   Char
0   A   16  Q   32  g   48  w
1   B   17  R   33  h   49  x
2   C   18  S   34  i   50  y
3   D   19  T   35  j   51  z
4   E   20  U   36  k   52  0
5   F   21  V   37  l   53  1
6   G   22  W   38  m   54  2
7   H   23  X   39  n   55  3
8   I   24  Y   40  o   56  4
9   J   25  Z   41  p   57  5
10  K   26  a   42  q   58  6
11  L   27  b   43  r   59  7
12  M   28  c   44  s   60  8
13  N   29  d   45  t   61  9
14  O   30  e   46  u   62  +
15  P   31  f   47  v   63  /
like image 154
Soner Gönül Avatar answered Oct 15 '22 21:10

Soner Gönül