如何在给定字符串的情况下返回base64编码的字符串?
如何将base64编码的字符串解码为字符串?
public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); }
public static string Base64Decode(string base64EncodedData) { var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData); return System.Text.Encoding.UTF8.GetString(base64EncodedBytes); }
我正在分享我的实现与一些简洁的功能:
使用扩展方法编码类 . 理由是有人可能需要支持不同类型的编码(不仅仅是UTF8) .
另一个改进是对于空进入空结果未能正常 - 这是在现实生活中的场景非常有用,并支持等价于X =解码(编码(X)) .
备注:请记住,要使用扩展方法, have to (!)使用 using 关键字(在本例中为 using MyApplication.Helpers.Encoding )导入命名空间 .
using
using MyApplication.Helpers.Encoding
Code:
namespace MyApplication.Helpers.Encoding { public static class EncodingForBase64 { public static string EncodeBase64(this System.Text.Encoding encoding, string text) { if (text == null) { return null; } byte[] textAsBytes = encoding.GetBytes(text); return System.Convert.ToBase64String(textAsBytes); } public static string DecodeBase64(this System.Text.Encoding encoding, string encodedText) { if (encodedText == null) { return null; } byte[] textAsBytes = System.Convert.FromBase64String(encodedText); return encoding.GetString(textAsBytes); } } }
Usage example:
using MyApplication.Helpers.Encoding; // !!! namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Test1(); Test2(); } static void Test1() { string textEncoded = System.Text.Encoding.UTF8.EncodeBase64("test1..."); System.Diagnostics.Debug.Assert(textEncoded == "dGVzdDEuLi4="); string textDecoded = System.Text.Encoding.UTF8.DecodeBase64(textEncoded); System.Diagnostics.Debug.Assert(textDecoded == "test1..."); } static void Test2() { string textEncoded = System.Text.Encoding.UTF8.EncodeBase64(null); System.Diagnostics.Debug.Assert(textEncoded == null); string textDecoded = System.Text.Encoding.UTF8.DecodeBase64(textEncoded); System.Diagnostics.Debug.Assert(textDecoded == null); } } }
对andrew.fox答案略有不同,因为要解码的字符串可能不是正确的base64编码字符串:
using System; namespace Service.Support { public static class Base64 { public static string ToBase64(this System.Text.Encoding encoding, string text) { if (text == null) { return null; } byte[] textAsBytes = encoding.GetBytes(text); return Convert.ToBase64String(textAsBytes); } public static bool TryParseBase64(this System.Text.Encoding encoding, string encodedText, out string decodedText) { if (encodedText == null) { decodedText = null; return false; } try { byte[] textAsBytes = Convert.FromBase64String(encodedText); decodedText = encoding.GetString(textAsBytes); return true; } catch (Exception) { decodedText = null; return false; } } } }
基于Andrew Fox和Cebe的答案,我转过身来,使它们成为字符串扩展而不是Base64String扩展 .
public static class StringExtensions { public static string ToBase64(this string text) { return ToBase64(text, Encoding.UTF8); } public static string ToBase64(this string text, Encoding encoding) { if (string.IsNullOrEmpty(text)) { return text; } byte[] textAsBytes = encoding.GetBytes(text); return Convert.ToBase64String(textAsBytes); } public static bool TryParseBase64(this string text, out string decodedText) { return TryParseBase64(text, Encoding.UTF8, out decodedText); } public static bool TryParseBase64(this string text, Encoding encoding, out string decodedText) { if (string.IsNullOrEmpty(text)) { decodedText = text; return false; } try { byte[] textAsBytes = Convert.FromBase64String(text); decodedText = encoding.GetString(textAsBytes); return true; } catch (Exception) { decodedText = null; return false; } } }
4 回答
编码
解码
我正在分享我的实现与一些简洁的功能:
使用扩展方法编码类 . 理由是有人可能需要支持不同类型的编码(不仅仅是UTF8) .
另一个改进是对于空进入空结果未能正常 - 这是在现实生活中的场景非常有用,并支持等价于X =解码(编码(X)) .
备注:请记住,要使用扩展方法, have to (!)使用
using
关键字(在本例中为using MyApplication.Helpers.Encoding
)导入命名空间 .Code:
Usage example:
对andrew.fox答案略有不同,因为要解码的字符串可能不是正确的base64编码字符串:
基于Andrew Fox和Cebe的答案,我转过身来,使它们成为字符串扩展而不是Base64String扩展 .