1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 如何编码和解码base64字符串?

如何编码和解码base64字符串?

时间:2020-06-30 16:49:44

相关推荐

如何编码和解码base64字符串?

本文翻译自:How do I encode and decode a base64 string?

How do I return a base64 encoded string given a string?给定字符串,如何返回以base64编码的字符串?

How do I decode a base64 encoded string into a string?如何将base64编码的字符串解码为字符串?

#1楼

参考:/question/nGvo/如何编码和解码base-字符串

#2楼

Encode编码

public static string Base64Encode(string plainText) {var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);return System.Convert.ToBase64String(plainTextBytes);}

Decode解码

public static string Base64Decode(string base64EncodedData) {var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);}

#3楼

I'm sharing my implementation with some neat features:我将通过一些简洁的功能分享我的实现:

uses Extension Methods for Encoding class.使用扩展方法编码类。Rationale is that someone may need to support different types of encodings (not only UTF8).理由是有人可能需要支持不同类型的编码(不仅限于UTF8)。Another improvement is failing gracefully with null result for null entry - it's very useful in real life scenarios and supports equivalence for X=decode(encode(X)).另一个改进是由于空输入而导致的空结果优雅地失败了-这在现实生活中非常有用,并且支持X = decode(encode(X))的等效项。

Remark: Remember that to use Extension Method youhave to(!) import the namespace withusingkeyword (in this caseusing 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);}}}

#4楼

A slight variation on andrew.fox answer, as the string to decode might not be a correct base64 encoded string: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; }}}}

#5楼

Based on the answers by Andrew Fox and Cebe, I turned it around and made them string extensions instead of Base64String extensions.根据安德鲁·福克斯(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;}}}

#6楼

using System;using System.Text;public static class Base64Conversions{public static string EncodeBase64(this string text, Encoding encoding = null){ if (text == null) return null;encoding = encoding ?? Encoding.UTF8;var bytes = encoding.GetBytes(text);return Convert.ToBase64String(bytes);}public static string DecodeBase64(this string encodedText, Encoding encoding = null){if (encodedText == null) return null;encoding = encoding ?? Encoding.UTF8;var bytes = Convert.FromBase64String(encodedText);return encoding.GetString(bytes);}}

Usage用法

var text = "Sample Text";var base64 = text.EncodeBase64();base64 = text.EncodeBase64(Encoding.UTF8); //or with Encoding

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。