How do I encode and decode a base64 string?

前端 未结 10 2263
我在风中等你
我在风中等你 2020-11-22 03:41
  1. How do I return a base64 encoded string given a string?

  2. How do I decode a base64 encoded string into a string?

相关标签:
10条回答
  • 2020-11-22 04:26

    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).
    • 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)).

    Remark: Remember that to use Extension Method you have to (!) import the namespace with using keyword (in this case 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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:28

    You can display it like this:

    var strOriginal = richTextBox1.Text;
    
    byte[] byt = System.Text.Encoding.ASCII.GetBytes(strOriginal);
    
    // convert the byte array to a Base64 string
    string strModified = Convert.ToBase64String(byt);
    
    richTextBox1.Text = "" + strModified;
    

    Now, converting it back.

    var base64EncodedBytes = System.Convert.FromBase64String(richTextBox1.Text);
    
    richTextBox1.Text = "" + System.Text.Encoding.ASCII.GetString(base64EncodedBytes);
    MessageBox.Show("Done Converting! (ASCII from base64)");
    

    I hope this helps!

    0 讨论(0)
  • 2020-11-22 04:33

    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);
    }
    
    0 讨论(0)
  • 2020-11-22 04:36

    One liner code:

    Note: Use System and System.Text directives.

    Encode:

    string encodedStr = Convert.ToBase64String(Encoding.UTF8.GetBytes("inputStr"));
    

    Decode:

    string inputStr = Encoding.UTF8.GetString(Convert.FromBase64String(encodedStr));
    
    0 讨论(0)
提交回复
热议问题