C# 如何解决“base64 无效字符”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16774024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I solver an "base64 invalid characters" error?
提问by voimak
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!
谢谢!
回答by Haris Hasan
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”,以及符号“+”和“/” ”。
回答by Andriy Kizym
It's because of '%' char in the string. It's not allowed in base64. Where do you take this string from?
这是因为字符串中有 '%' 字符。它在 base64 中是不允许的。你从哪里得到这个字符串?
回答by Soner G?nül
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 base64encoding.
%不允许在base64编码中。
Check out The Base64 index tablefrom Wikipedia
检查出的Base64编码索引表从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 /
回答by John Willemse
The string contains invalid characters. In this case the %sign.
该字符串包含无效字符。在这种情况下的%标志。
If you unescape %2Bto "+" and %3Dto "=", then the string will be accepted.
如果您转义%2B为“+”和%3D“=”,则该字符串将被接受。
回答by Eoin Campbell
%2B is a + sign URL Encoded %3D is an = sign URL Encoded.
%2B 是 + 号 URL 编码 %3D 是 = 号 URL 编码。
Your input string is actually.
您的输入字符串实际上是。
4rdHFh+HYoS8oLdVvbUzEVqB8Lvm7kSPnuwF0AAABYQ=
回答by MRK
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 charactersand @luke-parksolve my problem.(all credits go to him) So long story short is:
我遇到了同样的问题,但这些答案都没有帮助我!所以我在这里问我的问题: How to encrypt strings that may have non-base 64 charactersand @ luke-park解决我的问题。(所有功劳归他所有)长话短说是:
We have to convert our inputStringdata into a byte array then convert that to bsae64. Now our data is valid base64format 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:我的帖子中有两个加密类,非常适合将它们用于加密/解密字符串。

