How do I encode and decode a base64 string?

前端 未结 10 2269
我在风中等你
我在风中等你 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:09

    You can use below routine to convert string to base64 format

    public static string ToBase64(string s)
    {
        byte[] buffer = System.Text.Encoding.Unicode.GetBytes(s);
        return System.Convert.ToBase64String(buffer);
    }
    

    Also you can use very good online tool OnlineUtility.in to encode string in base64 format

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

    A slight variation on andrew.fox answer, as the string to decode might not be a correct base64 encoded string:

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

    Based on the answers by Andrew Fox and Cebe, I turned it around and made them string extensions instead of Base64String extensions.

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

    For those that simply want to encode/decode individual base64 digits:

    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));
        else
            return digit == 62 ? digit62 : digit63;
    }
    

    There are various versions of Base64 that disagree about what to use for digits 62 and 63, so DecodeBase64Digit can tolerate several of these.

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

    URL safe Base64 Encoding/Decoding

    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));
        }
    }
    
    0 讨论(0)
提交回复
热议问题