转换base64字符串时出错

本文关键字:出错 字符串 base64 转换 | 更新日期: 2023-09-27 18:06:09

我正试图将base64字符串转换为HTML代码,我正在通过WinRT中的Gmail Api从我的电子邮件中获得。但问题是,在将base64字符串转换为字节数组时,会抛出FormatException。另外,当我在base64decode网站上解码它时,它完美地解码了base64字符串。

这是我试图解码的base64字符串:

PGltZyBzcmM9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvYWRtaW1wLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBib3JkZXI9IjAiIGhlaWdodD0iMCIgd2lkdGg9IjAiPjxkaXYgc3R5bGU9IndpZHRoOjYwMHB4OyAgZm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxOHB4OyBsaW5lLWhlaWdodDoyMnB4OyB0ZXh0LWFsaWduOiBjZW50ZXI7Ij4gIDxmb250IHN0eWxlPSJmb250LXdlaWdodDogYm9sZDsgZm9udC1zaXplOiAyMHB4OyBjb2xvcjogcmVkOyI-IEdST1cgWU9VUiBPV04gSEFJUiBGT1IgSlVTVCAxIFJTLjwvZm9udD4gIDxicj4gIDxicj4gIDxmb250IHN0eWxlPSJjb2xvcjpncmVlbjtmb250LXdlaWdodDogYm9sZDsiPkludGVyZXN0IEZyZWUgRU1JPGJyPjxicj4xIFJzLiBEb3duIFBheW1lbnQ8L2ZvbnQ-ICA8YnI-ICA8YnI-ICA8YSBocmVmPSJodHRwOi8vY2Nwcm9tYWlsLmNvbTo4MTgxL3RyYWNrLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBzdHlsZT0iY29sb3I6IGJsdWU7IGJhY2tncm91bmQtY29sb3I6IHllbGxvdzsiPkdldCBZb3VyIEFwcG9pbnRtZW50IE5vdzwvYT4gIDxicj4gIDxicj4gPC9kaXY-PGJyLz48ZGl2IHN0eWxlPSJmb250LXNpemU6OXB4O3dpZHRoOjYwMHB4O21hcmdpbi1sZWZ0OiAxNTBweDsiPjxhIGhyZWY9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvdHJhY2suanNwP2lkPVRpRS1uV2twaERJQm5oc1RLa3Q0Q2lyVDNMdW9jT1k5VXZzMkNvWUVsajhiNklaOXRNanpLM29FSkRGRUdmcGcmdT1hIj5DbGljayBIZXJlPC9hPiB0byB1bnN1YnNjcmliZSBmcm9tIHRoaXMgbmV3c2xldHRlci48YnIvPjwvZGl2Pg0K

编辑:这是我用来解码字符串的代码:

string raw = "PGltZyBzcmM9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvYWRtaW1wLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBib3JkZXI9IjAiIGhlaWdodD0iMCIgd2lkdGg9IjAiPjxkaXYgc3R5bGU9IndpZHRoOjYwMHB4OyAgZm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxOHB4OyBsaW5lLWhlaWdodDoyMnB4OyB0ZXh0LWFsaWduOiBjZW50ZXI7Ij4gIDxmb250IHN0eWxlPSJmb250LXdlaWdodDogYm9sZDsgZm9udC1zaXplOiAyMHB4OyBjb2xvcjogcmVkOyI-IEdST1cgWU9VUiBPV04gSEFJUiBGT1IgSlVTVCAxIFJTLjwvZm9udD4gIDxicj4gIDxicj4gIDxmb250IHN0eWxlPSJjb2xvcjpncmVlbjtmb250LXdlaWdodDogYm9sZDsiPkludGVyZXN0IEZyZWUgRU1JPGJyPjxicj4xIFJzLiBEb3duIFBheW1lbnQ8L2ZvbnQ-ICA8YnI-ICA8YnI-ICA8YSBocmVmPSJodHRwOi8vY2Nwcm9tYWlsLmNvbTo4MTgxL3RyYWNrLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBzdHlsZT0iY29sb3I6IGJsdWU7IGJhY2tncm91bmQtY29sb3I6IHllbGxvdzsiPkdldCBZb3VyIEFwcG9pbnRtZW50IE5vdzwvYT4gIDxicj4gIDxicj4gPC9kaXY-PGJyLz48ZGl2IHN0eWxlPSJmb250LXNpemU6OXB4O3dpZHRoOjYwMHB4O21hcmdpbi1sZWZ0OiAxNTBweDsiPjxhIGhyZWY9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvdHJhY2suanNwP2lkPVRpRS1uV2twaERJQm5oc1RLa3Q0Q2lyVDNMdW9jT1k5VXZzMkNvWUVsajhiNklaOXRNanpLM29FSkRGRUdmcGcmdT1hIj5DbGljayBIZXJlPC9hPiB0byB1bnN1YnNjcmliZSBmcm9tIHRoaXMgbmV3c2xldHRlci48YnIvPjwvZGl2Pg0K";
string codedbody = raw.Replace("-", "+");
codedbody = raw.Replace("_", "/");
byte[] data = Convert.FromBase64String(codedbody);
string body = Encoding.UTF8.GetString(data);

转换base64字符串时出错

string input = "PGltZyBzcmM9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvYWRtaW1wLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBib3JkZXI9IjAiIGhlaWdodD0iMCIgd2lkdGg9IjAiPjxkaXYgc3R5bGU9IndpZHRoOjYwMHB4OyAgZm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxOHB4OyBsaW5lLWhlaWdodDoyMnB4OyB0ZXh0LWFsaWduOiBjZW50ZXI7Ij4gIDxmb250IHN0eWxlPSJmb250LXdlaWdodDogYm9sZDsgZm9udC1zaXplOiAyMHB4OyBjb2xvcjogcmVkOyI-IEdST1cgWU9VUiBPV04gSEFJUiBGT1IgSlVTVCAxIFJTLjwvZm9udD4gIDxicj4gIDxicj4gIDxmb250IHN0eWxlPSJjb2xvcjpncmVlbjtmb250LXdlaWdodDogYm9sZDsiPkludGVyZXN0IEZyZWUgRU1JPGJyPjxicj4xIFJzLiBEb3duIFBheW1lbnQ8L2ZvbnQ-ICA8YnI-ICA8YnI-ICA8YSBocmVmPSJodHRwOi8vY2Nwcm9tYWlsLmNvbTo4MTgxL3RyYWNrLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBzdHlsZT0iY29sb3I6IGJsdWU7IGJhY2tncm91bmQtY29sb3I6IHllbGxvdzsiPkdldCBZb3VyIEFwcG9pbnRtZW50IE5vdzwvYT4gIDxicj4gIDxicj4gPC9kaXY-PGJyLz48ZGl2IHN0eWxlPSJmb250LXNpemU6OXB4O3dpZHRoOjYwMHB4O21hcmdpbi1sZWZ0OiAxNTBweDsiPjxhIGhyZWY9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvdHJhY2suanNwP2lkPVRpRS1uV2twaERJQm5oc1RLa3Q0Q2lyVDNMdW9jT1k5VXZzMkNvWUVsajhiNklaOXRNanpLM29FSkRGRUdmcGcmdT1hIj5DbGljayBIZXJlPC9hPiB0byB1bnN1YnNjcmliZSBmcm9tIHRoaXMgbmV3c2xldHRlci48YnIvPjwvZGl2Pg0K";
string result = System.Text.Encoding.Default.GetString(System.Convert.FromBase64String(input.Replace("-", "+").Replace("_", "/")));

我保留了您的替换,因为-_是BASE64的无效字符

您的尝试解码看起来像您正在尝试解码一个base64 URL编码字符串。

public byte[] Decode(string text) {
    var output = text;
    output = output.Replace('-', '+'); // 62nd char of encoding
    output = output.Replace('_', '/'); // 63rd char of encoding
    switch (output.Length % 4) { // Pad with trailing '='s
        case 0: break; // No pad chars in this case
        case 2: output += "=="; break; // Two pad chars
        case 3: output += "="; break;  // One pad char
        default: throw new FormatException("invalid format", text));
    }
    var converted = Convert.FromBase64String(output); // Standard base64 decoder
    return converted;
}
使用上面的代码,您可以执行
string raw = "PGltZyBzcmM9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvYWRtaW1wLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBib3JkZXI9IjAiIGhlaWdodD0iMCIgd2lkdGg9IjAiPjxkaXYgc3R5bGU9IndpZHRoOjYwMHB4OyAgZm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxOHB4OyBsaW5lLWhlaWdodDoyMnB4OyB0ZXh0LWFsaWduOiBjZW50ZXI7Ij4gIDxmb250IHN0eWxlPSJmb250LXdlaWdodDogYm9sZDsgZm9udC1zaXplOiAyMHB4OyBjb2xvcjogcmVkOyI-IEdST1cgWU9VUiBPV04gSEFJUiBGT1IgSlVTVCAxIFJTLjwvZm9udD4gIDxicj4gIDxicj4gIDxmb250IHN0eWxlPSJjb2xvcjpncmVlbjtmb250LXdlaWdodDogYm9sZDsiPkludGVyZXN0IEZyZWUgRU1JPGJyPjxicj4xIFJzLiBEb3duIFBheW1lbnQ8L2ZvbnQ-ICA8YnI-ICA8YnI-ICA8YSBocmVmPSJodHRwOi8vY2Nwcm9tYWlsLmNvbTo4MTgxL3RyYWNrLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBzdHlsZT0iY29sb3I6IGJsdWU7IGJhY2tncm91bmQtY29sb3I6IHllbGxvdzsiPkdldCBZb3VyIEFwcG9pbnRtZW50IE5vdzwvYT4gIDxicj4gIDxicj4gPC9kaXY-PGJyLz48ZGl2IHN0eWxlPSJmb250LXNpemU6OXB4O3dpZHRoOjYwMHB4O21hcmdpbi1sZWZ0OiAxNTBweDsiPjxhIGhyZWY9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvdHJhY2suanNwP2lkPVRpRS1uV2twaERJQm5oc1RLa3Q0Q2lyVDNMdW9jT1k5VXZzMkNvWUVsajhiNklaOXRNanpLM29FSkRGRUdmcGcmdT1hIj5DbGljayBIZXJlPC9hPiB0byB1bnN1YnNjcmliZSBmcm9tIHRoaXMgbmV3c2xldHRlci48YnIvPjwvZGl2Pg0K";
byte[] data = Decode(raw);
string body = Encoding.UTF8.GetString(data);

生成HTML输出

<img src="http://ccpromail.com:8181/admimp.jsp?id=TiE-nWkphDIBnhsTKkt4CirT3LuocOY9Uvs2CoYElj8b6IZ9tMjzK3oEJDFEGfpg" border="0" height="0" width="0"><div style="width:600px;  font-family:Arial, Helvetica, sans-serif; font-size: 18px; line-height:22px; text-align: center;">  <font style="font-weight: bold; font-size: 20px; color: red;"> GROW YOUR OWN HAIR FOR JUST 1 RS.</font>  <br>  <br>  <font style="color:green;font-weight: bold;">Interest Free EMI<br><br>1 Rs. Down Payment</font>  <br>  <br>  <a href="http://ccpromail.com:8181/track.jsp?id=TiE-nWkphDIBnhsTKkt4CirT3LuocOY9Uvs2CoYElj8b6IZ9tMjzK3oEJDFEGfpg" style="color: blue; background-color: yellow;">Get Your Appointment Now</a>  <br>  <br> </div><br/><div style="font-size:9px;width:600px;margin-left: 150px;"><a href="http://ccpromail.com:8181/track.jsp?id=TiE-nWkphDIBnhsTKkt4CirT3LuocOY9Uvs2CoYElj8b6IZ9tMjzK3oEJDFEGfpg&u=a">Click Here</a> to unsubscribe from this newsletter.<br/></div>

URL的应用程序

Base64编码在相当长的标识时很有用信息在HTTP环境中使用。例如,数据库Java对象的持久性框架可以使用Base64编码来实现将较大的惟一id(通常为128位uuid)编码为字符串,用作HTTP表单中的HTTP参数或HTTP GET url。此外,许多应用程序需要以以下方式对二进制数据进行编码方便包含在url中,包括隐藏的web表单字段,Base64是一种方便的编码,可以以紧凑的方式呈现它们。

在URL中使用标准Base64需要对'+','/'和'='进行编码字符转换成特殊百分比编码的十六进制序列('+')变成'%2B', '/'变成'%2F', '='变成'%3D'),这使得字符串不必要地变长。

因此,存在针对URL变体的修改Base64,其中'+'标准Base64的'/'字符分别被'-'替换和'_',这样就不再需要使用URL编码器/解码器了并且对编码值的长度没有影响,留下同样的编码形式,可以完整地用于关系数据库、web表单、一般的对象标识符。有些变体允许或要求省略填充'='符号以避免与field混淆分隔符,或要求任何此类填充都是百分比编码的。一些库(如org.bouncycastle.util.encoders.UrlBase64Encoder)将编码'='到'.'.

Reference Base64: From Wikipedia

你的代码出错了:你再次用raw代替codebody

codedbody = raw.Replace("_", "/");
应:

codedbody = codedbody.Replace("_", "/");