【发布时间】:2018-05-25 08:21:22
【问题描述】:
我有一个 Base64 编码的密钥。
在尝试解码时,我收到以下错误。错误是byte[] todecode_byte = Convert.FromBase64String(data);抛出的
base64Decode 中的错误输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符、两个以上的填充字符或填充字符中的非法字符。
我正在使用下面的方法来解码:
public string base64Decode(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data); // this line throws the exception
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
【问题讨论】:
-
请张贴您要解码的字符串(如果不是太大)。
-
抛出什么异常,从哪里抛出?
-
如果它很大,请使用 PasteBin。
-
该错误似乎表明您的输入错误。您检查过输入的字符串是否正确吗?
-
-不是有效字符。你的字符串是从哪里来的?您可能需要将-字符更改为+或/。此外,它的长度必须是 4 个字符的倍数。除非我数错,否则您的字符串有 43 个字符。