How To Encode & Decode Base64 In C# And VB.NET

As we know that Base64 is a group of binary-to-text encoding schemes mainly used for encrypting strings so that they can be protected from direct exposure. In this post , we will see how to encode & decode Base64 in C# & VB.NET

C# Encode String To Base64

Create a new empty project in Visual Studio. Choose C# Windows Form as project. In this project , we will need following components:

  • Two TextBox (mytxt - For accepting value from users , txtoutput - To print the output)
  • One Button (btnEncode - Upon clicking on which , encoding operation occurs)
We have to use System.text namespace

Code To Encode Base64 In C#

using System.Text;

namespace Base64_Encode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncode_Click(object sender, EventArgs e)
        {
            var theBytes = Encoding.UTF8.GetBytes(mytxt.Text);
            var output = Convert.ToBase64String(theBytes);
            txtoutput.Text = (output);
        }
    }
}

Output Of C# Base64 Encoding


C# Decode String To Base64

Create a new empty project in Visual Studio. Choose C# Windows Form as project. In this project , we will need following components:

  • Two TextBox (mytxt - For accepting value from users , txtoutput - To print the output)
  • One Button (btnDecode - Upon clicking on which , decoding operation occurs)
We have to use System.text namespace

Code To Decode Base64 In C#

using System.Text;

namespace Base64_Decode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            var theBytes = System.Convert.FromBase64String(mytxt.Text);
            var output = Encoding.UTF8.GetString(theBytes);
            txtoutput.Text = (output);
        }
    }
}

Output Of C# Base64 Decoding


VB.NET Encode String To Base64

Create a new empty project in Visual Studio. Choose VB Windows Form App as project. In this project , we will need following components:
  • Two TextBox (mytxt - For accepting value from users , txtoutput - To print the output)
  • One Button (btnEncode - Upon clicking on which , encoding operation occurs)
We need to import System.Convert

Code To Encode Base64 In VB.NET

Imports System.Convert

Public Class Form1
    Private Sub btnEncode_Click(sender As Object, e As EventArgs) Handles btnEncode.Click
        Dim theBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(mytxt.Text)
        Dim output = ToBase64String(theBytes)
        txtoutput.Text = output
    End Sub
End Class

Output Of VB.NET Base64 Encoding

VB.NET Decode String To Base64

Create a new empty project in Visual Studio. Choose VB Windows Form App as project. In this project , we will need following components:
  • Two TextBox (mytxt - For accepting value from users , txtoutput - To print the output)
  • One Button (btnDecode - Upon clicking on which , decoding operation occurs)
We need to import System.Convert

Code To Decode Base64 In VB.NET

Imports System.Convert

Public Class Form1
    Private Sub btnDecode_Click(sender As Object, e As EventArgs) Handles btnEncode.Click
        Dim output = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(mytxt.Text))
        txtoutput.Text = output
    End Sub
End Class

Output Of VB.NET Base64 Decoding