Convert.FromBase64String() 抛出 "invalid Base-64 string" 错误
Convert.FromBase64String() throws "invalid Base-64 string" error
我有一个 Base64 编码的密钥。
我在尝试解码时收到以下错误。 byte[] todecode_byte = Convert.FromBase64String(data);
抛出错误
Error in base64DecodeThe input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
我正在使用以下方法对此进行解码:
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);
}
}
您的 base64 字符串无效。它包含不允许的 -
。
static void Main()
{
string tmp = "eL78WIArGQ7bC44Ozr0yvUBkz9oc5YlsENYJilInSP==";
byte[] tmp2 = Convert.FromBase64String(tmp);
}
-> 删除减号
-> 添加了两个填充字符“=
”
所以有两个问题:
- 您的字符串长度不是 4 的倍数。需要使用“=”字符将其填充为 4 的倍数。
- 看起来是the format of base 64 used for URLs and suchlike, "modified Base64 for URL"。这使用
-
而不是 +
和 _
而不是 /
。
所以要解决这个问题,您需要将 -
交换为 +
并将 _
交换为 /
并填充它,如下所示:
public static byte[] DecodeUrlBase64(string s)
{
s = s.Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/4), '=');
return Convert.FromBase64String(s);
}
我在使用 .Net 5 Identity Framework 时在 ASP.Net MVC 应用程序中发送密码重置令牌时遇到了同样的问题。 URL 中的重置密码令牌是一个有效的 URL 编码 Base64 字符串,但在绑定查询参数时 .Net Framework 将 + 号转换为空格会产生问题。所以在用 + 号替换空格后它完美地工作了。
我已经根据@Mathew Watson 接受的答案更新了 DecodeUrlBase64 方法来处理空格。
public static byte[] DecodeUrlBase64(string s)
{
s = s.Replace(' ', '+').Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/4),'=');
return Convert.FromBase64String(s);
}
我有一个 Base64 编码的密钥。
我在尝试解码时收到以下错误。 byte[] todecode_byte = Convert.FromBase64String(data);
Error in base64DecodeThe input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
我正在使用以下方法对此进行解码:
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);
}
}
您的 base64 字符串无效。它包含不允许的 -
。
static void Main()
{
string tmp = "eL78WIArGQ7bC44Ozr0yvUBkz9oc5YlsENYJilInSP==";
byte[] tmp2 = Convert.FromBase64String(tmp);
}
-> 删除减号
-> 添加了两个填充字符“=
”
所以有两个问题:
- 您的字符串长度不是 4 的倍数。需要使用“=”字符将其填充为 4 的倍数。
- 看起来是the format of base 64 used for URLs and suchlike, "modified Base64 for URL"。这使用
-
而不是+
和_
而不是/
。
所以要解决这个问题,您需要将 -
交换为 +
并将 _
交换为 /
并填充它,如下所示:
public static byte[] DecodeUrlBase64(string s)
{
s = s.Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/4), '=');
return Convert.FromBase64String(s);
}
我在使用 .Net 5 Identity Framework 时在 ASP.Net MVC 应用程序中发送密码重置令牌时遇到了同样的问题。 URL 中的重置密码令牌是一个有效的 URL 编码 Base64 字符串,但在绑定查询参数时 .Net Framework 将 + 号转换为空格会产生问题。所以在用 + 号替换空格后它完美地工作了。
我已经根据@Mathew Watson 接受的答案更新了 DecodeUrlBase64 方法来处理空格。
public static byte[] DecodeUrlBase64(string s)
{
s = s.Replace(' ', '+').Replace('-', '+').Replace('_', '/').PadRight(4*((s.Length+3)/4),'=');
return Convert.FromBase64String(s);
}