转换base64字符串

时间:2016-09-08 14:30:52

标签: c# base64

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

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

  

PGltZyBzcmM9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvYWRtaW1wLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBib3JkZXI9IjAiIGhlaWdodD0iMCIgd2lkdGg9IjAiPjxkaXYgc3R5bGU9IndpZHRoOjYwMHB4OyAgZm9udC1mYW1pbHk6QXJpYWwsIEhlbHZldGljYSwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxOHB4OyBsaW5lLWhlaWdodDoyMnB4OyB0ZXh0LWFsaWduOiBjZW50ZXI7Ij4gIDxmb250IHN0eWxlPSJmb250LXdlaWdodDogYm9sZDsgZm9udC1zaXplOiAyMHB4OyBjb2xvcjogcmVkOyI-IEdST1cgWU9VUiBPV04gSEFJUiBGT1IgSlVTVCAxIFJTLjwvZm9udD4gIDxicj4gIDxicj4gIDxmb250IHN0eWxlPSJjb2xvcjpncmVlbjtmb250LXdlaWdodDogYm9sZDsiPkludGVyZXN0IEZyZWUgRU1JPGJyPjxicj4xIFJzLiBEb3duIFBheW1lbnQ8L2ZvbnQ-ICA8YnI-ICA8YnI-ICA8YSBocmVmPSJodHRwOi8vY2Nwcm9tYWlsLmNvbTo4MTgxL3RyYWNrLmpzcD9pZD1UaUUtbldrcGhESUJuaHNUS2t0NENpclQzTHVvY09ZOVV2czJDb1lFbGo4YjZJWjl0TWp6SzNvRUpERkVHZnBnIiBzdHlsZT0iY29sb3I6IGJsdWU7IGJhY2tncm91bmQtY29sb3I6IHllbGxvdzsiPkdldCBZb3VyIEFwcG9pbnRtZW50IE5vdzwvYT4gIDxicj4gIDxicj4gPC9kaXY-PGJyLz48ZGl2IHN0eWxlPSJmb250LXNpemU6OXB4O3dpZHRoOjYwM HB4O21hcmdpbi1sZWZ0OiAxNTBweDsiPjxhIGhyZWY9Imh0dHA6Ly9jY3Byb21haWwuY29tOjgxODEvdHJhY2suanNwP2lkPVRpRS1uV2twaERJQm5oc1RLa3Q0Q2lyVDNMdW9jT1k5VXZzMkNvWUVsajhiNklaOXRNanpLM29FSkRGRUdmcGcmdT1hIj5DbGljayBIZXJlPC9hPiB0byB1bnN1YnNjcmliZSBmcm9tIHRoaXMgbmV3c2xldHRlci48YnIvPjwvZGl2Pg0K

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

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);

3 个答案:

答案 0 :(得分:3)

怎么样

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

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

答案 1 :(得分:1)

您尝试解码看起来就像是在尝试解码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>
  

网址应用

     

Base64编码在相当冗长的识别时很有用   信息在HTTP环境中使用。例如,一个数据库   Java对象的持久性框架可能使用Base64编码   将相对较大的唯一ID(通常为128位UUID)编码为a   字符串,用作HTTP表单或HTTP GET URL中的HTTP参数。   此外,许多应用程序需要以这样的方式编码二进制数据   方便列入URL,包括隐藏的Web表单字段,   和Base64是一种方便的编码,可以以紧凑的方式呈现它们。

     

在网址中使用标准Base64需要编码&#39; +&#39;,&#39; /&#39;和&#39; =&#39;   将字符转换为特殊的百分比编码的十六进制序列(&#39; +&#39;   成为&#39;%2B&#39;,&#39; /&#39;成为&#39;%2F&#39;和&#39; =&#39;成为&#39;%3D&#39;),这使得   字符串不必要地长了。

     

出于这个原因,存在针对URL变体的修改后的Base64,其中&#39; +&#39;   和&#39; /&#39;标准Base64的字符分别替换为&#39; - &#39;   和&#39; _&#39;,因此不再需要使用URL编码器/解码器   并且对编码值的长度没有影响,留下   完整的相同编码形式,用于关系数据库,网络表格,   和一般的对象标识符。一些变体允许或要求   省略填充&#39; =&#39;避免他们与田地混淆的迹象   分隔符,或要求任何此类填充进行百分比编码。一些   库(如org.bouncycastle.util.encoders.UrlBase64Encoder)将   编码&#39; =&#39;到&#39;。&#39;。

参考Base64 : From Wikipedia

答案 2 :(得分:1)

你在代码中犯了错误: 您再次将raw替换为codebody

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

应该是:

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