如何解决“base64无效字符”的错误?

时间:2021-07-07 20:14:45

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

当我尝试使用以下代码将value1转换为byte[]时:

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

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

Ι收到一个错误的字符串内容无效的base - 64字符……

What's wrong?

怎么了?

Thanks!

谢谢!

6 个解决方案

#1


6  

From Convert.FromBase64String

从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 '/'.

s由64位数字、空格字符和尾随填充字符组成。从零升序的64位数字是大写字母“A”到“Z”,小写字母“A”到“Z”,数字“0”到“9”,符号“+”和“/”。

% is not allowed in base64 encoding.

在base64编码中不允许%。

Check out The Base64 index table from Wikipedia

从*查看Base64索引表。

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  /

#2


2  

I had the same problem, but none of these answer helping me! so I ask my question here: How to encrypt strings that may have non-base 64 characters and @luke-park solve my problem.(all credits go to him) So long story short is:

我也有同样的问题,但是没有一个答案能帮助我!所以我在这里问我的问题:如何加密可能有非base 64字符和@luke-park的字符串来解决我的问题。(一切归功于他)长话短说:

We have to convert our inputString data into a byte array then convert that to bsae64. Now our data is valid base64 format and can encrypt it:

我们必须将inputString数据转换成字节数组,然后将其转换为bsae64。现在我们的数据是有效的base64格式,可以加密:

byte[] data = Encoding.UTF8.GetBytes(inputString);
string b64 = Convert.ToBase64String(data);

I hope this help others people with the same problem.

我希望这能帮助其他有同样问题的人。

PS: There are Two Encryption Classes in my post, that are great to use them for encrypt/decrypt strings.

PS:我的帖子里有两个加密类,可以用来加密/解密字符串。

#3


1  

I think the problem is with character %. It is not allowed in base64 string

我认为问题在于性格%。在base64字符串中是不允许的。

According to MSDN

根据MSDN

public static byte[] FromBase64String(  string s)

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 "/".

s由base-64位数字、空格字符和尾随填充字符组成。从零升序的base-64位数字是大写字母“A”到“Z”,小写字母“A”到“Z”,数字“0”到“9”,符号“+”和“/”。

#4


1  

The string contains invalid characters. In this case the % sign.

该字符串包含无效字符。在这种情况下,%符号。

If you unescape %2B to "+" and %3D to "=", then the string will be accepted.

如果您将%2B设置为“+”和%3D到“=”,那么该字符串将被接受。

#5


1  

%2B is a + sign URL Encoded %3D is an = sign URL Encoded.

%2B是一个+符号URL编码%3D是一个=符号URL编码。

Your input string is actually.

你的输入字符串实际上是。

4rdHFh+HYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ=

#6


0  

It's because of '%' char in the string. It's not allowed in base64. Where do you take this string from?

因为字符串中有“%”字符。在base64中是不允许的。你把这根绳子从哪里取来?

#1


6  

From Convert.FromBase64String

从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 '/'.

s由64位数字、空格字符和尾随填充字符组成。从零升序的64位数字是大写字母“A”到“Z”,小写字母“A”到“Z”,数字“0”到“9”,符号“+”和“/”。

% is not allowed in base64 encoding.

在base64编码中不允许%。

Check out The Base64 index table from Wikipedia

从*查看Base64索引表。

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  /

#2


2  

I had the same problem, but none of these answer helping me! so I ask my question here: How to encrypt strings that may have non-base 64 characters and @luke-park solve my problem.(all credits go to him) So long story short is:

我也有同样的问题,但是没有一个答案能帮助我!所以我在这里问我的问题:如何加密可能有非base 64字符和@luke-park的字符串来解决我的问题。(一切归功于他)长话短说:

We have to convert our inputString data into a byte array then convert that to bsae64. Now our data is valid base64 format and can encrypt it:

我们必须将inputString数据转换成字节数组,然后将其转换为bsae64。现在我们的数据是有效的base64格式,可以加密:

byte[] data = Encoding.UTF8.GetBytes(inputString);
string b64 = Convert.ToBase64String(data);

I hope this help others people with the same problem.

我希望这能帮助其他有同样问题的人。

PS: There are Two Encryption Classes in my post, that are great to use them for encrypt/decrypt strings.

PS:我的帖子里有两个加密类,可以用来加密/解密字符串。

#3


1  

I think the problem is with character %. It is not allowed in base64 string

我认为问题在于性格%。在base64字符串中是不允许的。

According to MSDN

根据MSDN

public static byte[] FromBase64String(  string s)

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 "/".

s由base-64位数字、空格字符和尾随填充字符组成。从零升序的base-64位数字是大写字母“A”到“Z”,小写字母“A”到“Z”,数字“0”到“9”,符号“+”和“/”。

#4


1  

The string contains invalid characters. In this case the % sign.

该字符串包含无效字符。在这种情况下,%符号。

If you unescape %2B to "+" and %3D to "=", then the string will be accepted.

如果您将%2B设置为“+”和%3D到“=”,那么该字符串将被接受。

#5


1  

%2B is a + sign URL Encoded %3D is an = sign URL Encoded.

%2B是一个+符号URL编码%3D是一个=符号URL编码。

Your input string is actually.

你的输入字符串实际上是。

4rdHFh+HYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ=

#6


0  

It's because of '%' char in the string. It's not allowed in base64. Where do you take this string from?

因为字符串中有“%”字符。在base64中是不允许的。你把这根绳子从哪里取来?