如何在给定字符串的情况下返回Base64编码的字符串?
我如何解码一个bas64编码的字符串成一个字符串?
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);}
我正在分享我的实现与一些整洁的功能:
注意:请记住,要使用扩展方法,您必须(!)使用using关键字导入命名空间(在本例中为using MyApplication.Helpers.Encoding)。
using
using MyApplication.Helpers.Encoding
代码:
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);}}}
使用示例:
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答案略有变化,因为要解码的字符串可能不是正确的bas64编码字符串:
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;}}}
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);}}
用法
var text = "Sample Text";var base64 = text.EncodeBase64();base64 = text.EncodeBase64(Encoding.UTF8); //or with Encoding
对于那些只想编码/解码单个Base64数字的人:
public static int DecodeBase64Digit(char digit, string digit62 = "+-.~", string digit63 = "/_,"){if (digit >= 'A' && digit <= 'Z') return digit - 'A';if (digit >= 'a' && digit <= 'z') return digit + (26 - 'a');if (digit >= '0' && digit <= '9') return digit + (52 - '0');if (digit62.IndexOf(digit) > -1) return 62;if (digit63.IndexOf(digit) > -1) return 63;return -1;} public static char EncodeBase64Digit(int digit, char digit62 = '+', char digit63 = '/'){digit &= 63;if (digit < 52)return (char)(digit < 26 ? digit + 'A' : digit + ('a' - 26));else if (digit < 62)return (char)(digit + ('0' - 52));elsereturn digit == 62 ? digit62 : digit63;}
有各种版本的Base64不同意使用什么数字62和63,所以DecodeBase64Digit可以容忍其中的几个。
DecodeBase64Digit
您可以使用下面的例程将字符串转换为Base64格式
public static string ToBase64(string s){byte[] buffer = System.Text.Encoding.Unicode.GetBytes(s);return System.Convert.ToBase64String(buffer);}
您也可以使用非常好的在线工具OnlineUtility.in将字符串编码为Base64格式
你可以像这样显示它:
var strOriginal = richTextBox1.Text; byte[] byt = System.Text.Encoding.ASCII.GetBytes(strOriginal); // convert the byte array to a Base64 stringstring strModified = Convert.ToBase64String(byt); richTextBox1.Text = "" + strModified;
现在,把它转换回来。
var base64EncodedBytes = System.Convert.FromBase64String(richTextBox1.Text); richTextBox1.Text = "" + System.Text.Encoding.ASCII.GetString(base64EncodedBytes);MessageBox.Show("Done Converting! (ASCII from base64)");
我希望这有帮助!
URL安全Base64编码/解码
public static class Base64Url{public static string Encode(string text){return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)).TrimEnd('=').Replace('+', '-').Replace('/', '_');} public static string Decode(string text){text = text.Replace('_', '/').Replace('-', '+');switch (text.Length % 4){case 2:text += "==";break;case 3:text += "=";break;}return Encoding.UTF8.GetString(Convert.FromBase64String(text));}}
一个班轮代码:
注意:使用System和System.Text指令。
System
System.Text
编码:
string encodedStr = Convert.ToBase64String(Encoding.UTF8.GetBytes("inputStr"));
解码:
string inputStr = Encoding.UTF8.GetString(Convert.FromBase64String(encodedStr));
// Encodingstring passw = "tes123";var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(passw);string pass = System.Convert.ToBase64String(plainTextBytes); // Normalvar encodedTextBytes = Convert.FromBase64String(pass);string plainText = Encoding.UTF8.GetString(encodedTextBytes);