C# Base64 字符串抛出无效字符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/710853/
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
Base64 String throwing invalid character error
提问by Brandon
I keep getting a Base64 invalid character error even though I shouldn't.
即使我不应该,我也不断收到 Base64 无效字符错误。
The program takes an XML file and exports it to a document. If the user wants, it will compress the file as well. The compression works fine and returns a Base64 String which is encoded into UTF-8 and written to a file.
该程序获取一个 XML 文件并将其导出为文档。如果用户需要,它也会压缩文件。压缩工作正常并返回一个 Base64 字符串,该字符串被编码为 UTF-8 并写入文件。
When its time to reload the document into the program I have to check whether its compressed or not, the code is simply:
当需要将文档重新加载到程序中时,我必须检查它是否被压缩,代码很简单:
byte[] gzBuffer = System.Convert.FromBase64String(text);
return "1F-8B-08" == BitConverter.ToString(new List<Byte>(gzBuffer).GetRange(4, 3).ToArray());
It checks the beginning of the string to see if it has GZips code in it.
它检查字符串的开头以查看其中是否包含 GZips 代码。
Now the thing is, all my tests work. I take a string, compress it, decompress it, and compare it to the original. The problem is when I get the string returned from an ADO Recordset. The string is exactly what was written to the file (with the addition of a "\0" at the end, but I don't think that even does anything, even trimmed off it still throws). I even copy and pasted the entire string into a test method and compress/decompress that. Works fine.
现在的问题是,我所有的测试都有效。我取一个字符串,对其进行压缩、解压缩,然后将其与原始字符串进行比较。问题是当我得到从 ADO 记录集返回的字符串时。该字符串正是写入文件的内容(最后添加了一个“\ 0”,但我认为它不会做任何事情,即使修剪掉它仍然会抛出)。我什至将整个字符串复制并粘贴到测试方法中,然后对其进行压缩/解压缩。工作正常。
The tests will pass but the code will fail using the exact same string? The only difference is instead of just declaring a regular string and passing it in I'm getting one returned from a recordset.
测试将通过但代码将使用完全相同的字符串失败?唯一的区别是不是仅仅声明一个常规字符串并将其传递给我,而是从记录集中返回一个。
Any ideas on what am I doing wrong?
关于我做错了什么的任何想法?
采纳答案by Daniel LeCheminant
You say
你说
The string is exactly what was written to the file (with the addition of a "\0" at the end, but I don't think that even does anything).
该字符串正是写入文件的内容(在末尾添加了一个“\0”,但我认为这甚至没有任何作用)。
In fact, it does do something (it causes your code to throw a FormatException
:"Invalid character in a Base-64 string") because the Convert.FromBase64String
does not consider "\0" to be a valid Base64 character.
事实上,它确实做了一些事情(它会导致你的代码抛出一个FormatException
:“Base-64 字符串中的无效字符”),因为Convert.FromBase64String
它不认为“\0”是一个有效的 Base64 字符。
byte[] data1 = Convert.FromBase64String("AAAAstring stringToDecrypt = HttpContext.Current.Request.QueryString.ToString()
"); // Throws exception
byte[] data2 = Convert.FromBase64String("AAAA"); // Works
Solution: Get rid of the zero termination.(Maybe call .Trim("\0")
)
解决方案:去掉零终止。(也许打电话.Trim("\0")
)
Notes:
注意事项:
The MSDN docs for Convert.FromBase64String
say it will throw a FormatException
when
在MSDN文档的Convert.FromBase64String
说,它会抛出一个FormatException
时
The length of s, ignoring white space characters, is not zero or a multiple of 4.
-or-
The format of s is invalid. s contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
忽略空白字符的 s 的长度不是零或 4 的倍数。
-或者-
s 的格式无效。s 包含非 base 64 字符、两个以上的填充字符或填充字符中的非空白字符。
and that
然后
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 '/'.
从零开始按升序排列的 64 位基数是大写字符 'A' 到 'Z'、小写字符 'a' 到 'z'、数字 '0' 到 '9',以及符号 '+' 和 '/' .
回答by StaxMan
Whether null char is allowed or not really depends on base64 codec in question. Given vagueness of Base64 standard (there is no authoritative exact specification), many implementations would just ignore it as white space. And then others can flag it as a problem. And buggiest ones wouldn't notice and would happily try decoding it... :-/
是否允许空字符实际上取决于所讨论的 base64 编解码器。鉴于 Base64 标准的模糊性(没有权威的确切规范),许多实现只会将其作为空白忽略。然后其他人可以将其标记为问题。并且最有缺陷的人不会注意到并且会很乐意尝试解码它...... :-/
But it sounds c# implementation does not like it (which is one valid approach) so if removing it helps, that should be done.
但听起来 c# 实现不喜欢它(这是一种有效的方法)所以如果删除它有帮助,那就应该这样做。
One minor additional comment: UTF-8 is not a requirement, ISO-8859-x aka Latin-x, and 7-bit Ascii would work as well. This because Base64 was specifically designed to only use 7-bit subset which works with all 7-bit ascii compatible encodings.
一个小的附加评论:UTF-8 不是必需的,ISO-8859-x aka Latin-x 和 7 位 Ascii 也可以工作。这是因为 Base64 专门设计为仅使用 7 位子集,该子集适用于所有 7 位 ascii 兼容编码。
回答by abatishchev
If removing \0 from the end of string is impossible, you can add your own character for each string you encode, and remove it on decode.
如果从字符串末尾删除 \0 是不可能的,您可以为您编码的每个字符串添加自己的字符,并在解码时将其删除。
回答by SteveCav
One gotcha to do with converting Base64 from a string is that some conversion functions use the preceding "data:image/jpg;base64," and others only accept the actual data.
从字符串转换 Base64 的一个问题是,某些转换函数使用前面的“data:image/jpg;base64”,而其他转换函数只接受实际数据。
回答by Uday
//change to string stringToDecrypt = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString.ToString())
//改为字符串 stringToDecrypt = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString.ToString())