Minifycode 2022-05-02 Viewed 1K times C#

In this article, we will learn How To Base64 Encoding and Decoding in C#.

Each Base64 digit represents exactly 6-bits of data that means 4 6-bit Base64 digits can represent 3 bytes, Base64 is a group of similar binary-to-text encoding schemes representing binary data in an ASCII string format by translating it into a radix-64 
representation.

 
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are 
designed to deal with textual data. This is to ensure that the data remain intact without modification during transport. Base64 is commonly used in 
a number of applications, including email via MIME and storing complex data in XML.

C# help to support for Base64 Encoding and Decoding capabilities in System.Convert utility class, which consists of static methods for obtaining 
instances of encoders (Base64.Encoder) and decoders (Base64.Decoder) for the Base64 encoding scheme.

using System;
 
public class MinifyCode
{
    public static string ToBase64Encode(string arg)
    {
        if (String.IsNullOrEmpty(arg)) {
            return arg;
        }
 
        byte[] textBytes = Text.Encoding.UTF8.GetBytes(arg);
        return Convert.ToBase64String(textBytes);
    }
 
    public static string ToBase64Decode(string base64EncodedText)
    {
        if (String.IsNullOrEmpty(base64EncodedText)) {
            return base64EncodedText;
        }
 
        byte[] base64EncodedBytes = Convert.FromBase64String(base64EncodedText);
        return Text.Encoding.UTF8.GetString(base64EncodedBytes);
    }

 
 

public static void Main()
    {
        string str = "Hello World...."; 
        string encodedText = ToBase64Encode(str);
        Console.WriteLine("Base64 Encoded String: " + encodedText); 
        string decodedText = ToBase64Decode(encodedText);
        Console.WriteLine("Base64 Decoded String: " + decodedText);
    }
}

 

Encode

using System;
using System.Text;

namespace encode_and_decode_base64_string
{
    class Program
    {
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
        static void Main(string[] args)
        {
            string original = "This is a string";
            Console.WriteLine("Original String = " + original);
            string base64 = Base64Encode(original);
            Console.WriteLine("Encoded String = "+base64);
        }
    }
}

 

Decode

using System;
using System.Text;

namespace encode_and_decode_base64_string
{
    class Program
    {
        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
        static void Main(string[] args)
        {
            string base64 = "VGhpcyBpcyBhIHN0cmluhtftrsreDF==";
            Console.WriteLine("Encoded String = "+base64);
            string original = Base64Decode(base64);
            Console.WriteLine("Decoded String = "+original);
        }
    }
}


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);
}


Other way..

   

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


Encode:

string encodedString = Convert.ToBase64String(Encoding.UTF8.GetBytes("inputString"));

Decode:

string inputString = Encoding.UTF8.GetString(Convert.FromBase64String(encodedString));

How do I encode and decode a base64 string in c#?
C# is a programming language developed by Microsoft that runs on the .NET Framework. C# is used to develop web, desktop, mobile, games and much more application. C# is a object-oriented programming language developed by Microsoft within its .NET Framework. Led by Anders Hejlsberg, your basic C# programming and will also take you through various advanced concepts related to C# programming language. C# such as control statements, objects and classes, inheritance, constructor, destructor, this, static, sealed, polymorphism, abstraction, abstract class, interface, File IO, Collections, namespace, encapsulation, properties, indexer, arrays, strings, regex, exception handling, multithreading etc. For example... using System; namespace MinifyCode { class Program { static void Main(string[] args) { Console.WriteLine("Hello Minify Code"); } } } Output: Hello Minify Code In this article you will learn, what is server side controls. We will discuss each of these objects in due time. In this tutorial we will explore the Server object, the Request object, and the Response object. Session Application Cache Request Response Server User Trace Server Object The Server object in Asp.NET is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides numerous properties and methods to perform many type of jobs. Methods and Properties of the Server object The methods and properties of the HttpServerUtility class are exposed through the intrinsic Server object provided by ASP.NET. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using System.Data.Entity; namespace minifycode { public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // Code that runs on application startup RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); // Initialize the product database. Database.SetInitializer(new ProductDatabaseInitializer()); // Create custom role and user. RoleActions roleActions = new RoleActions(); roleActions.AddUserAndRole(); // Add Routes. RegisterCustomRoutes(RouteTable.Routes); } void RegisterCustomRoutes(RouteCollection routes) { routes.MapPageRoute( "ProductsCategoryRoute", "Category/{categoryName}", "~/ProductList.aspx" ); routes.MapPageRoute( "ProductNameRoute", "Product/{productName}", "~/ProductDetails.aspx" ); } } }