How to encode or decode a base64 string

This is an absolutely trivial programming task, but one that annoyed me enough to have to write some code to do…

I use Filezilla for FTP uploads/downloads at work, but never remember my passwords for FTP sites. Nor do I store them anywhere. What I’ve always done is, if I need to give someone a FTP password, just use Filezilla’s export functionality to export the list of sites. I did that for my colleague the other day, and to my surprise saw that the current version of the application writes every password in the exported file as a base64 encoded string. Why? Other than to be annoying, I can not imagine a reason.

So I wrote a stupid little application to do this:

image

It uses a couple of simple extension methods:

using System;
using System.Text;

namespace Base64StringHandler
{
    public static class StringExtensions
    {
        public static string Base64Encode(this string input)
        {
            byte[] data = Encoding.UTF8.GetBytes(input);
            return Convert.ToBase64String(data);
        }

        public static string Base64Decode(this string input)
        {
            byte[] data = Convert.FromBase64String(input);
            return Encoding.UTF8.GetString(data);
        }
    }
}

The code for the form is this:

using System;
using System.Windows.Forms;

namespace Base64StringHandler
{
    public partial class Base64StringHandlerForm : Form
    {
        public Base64StringHandlerForm()
        {
            InitializeComponent();
        }

        private void btnEncode_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbPlaintext.Text))
                tbEncoded.Text = tbPlaintext.Text.Base64Encode();
            else
                MessageBox.Show("Input a string to encode.", "No input", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void btnDecode_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbEncoded.Text))
                tbPlaintext.Text = tbEncoded.Text.Base64Decode();
            else
                MessageBox.Show("Input a string to decode.", "No input", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

If you dislike extension methods and think a Windows Forms application is overkill for this (It is!) then just use this console application instead. (Code to encode commented out. I just run it from Visual Studio anyway. If I cared to write something to be a reusable executable, I would’ve coded for expected command line arguments or something.)

using System;
using System.Text;

namespace DecodeBase64String
{
    class Program
    {
        static void Main(string[] args)
        {

            //Console.WriteLine("Enter string to base64-enocde:");
            //string enoceded = EncodeBase64(Console.ReadLine());
            //Console.WriteLine(enoceded);

            Console.WriteLine("Enter base64-encoded string to decode:");
            string decodedString = DecodeBase64(Console.ReadLine());
            Console.WriteLine(decodedString);

            Console.ReadKey();
        }

        private static string DecodeBase64(string base64EncodedValue)
        {
            byte[] data = Convert.FromBase64String(base64EncodedValue);
            return Encoding.UTF8.GetString(data);
        }

        private static string EncodeBase64(string plainText)
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(data);
        }
    }
}

Note that none of this code contains any error handling at all. That’s because its purpose is only to illustrate the simple concept of how to encode or decode a base64 string. Error handling is left as an exercise to anyone who uses this code…

Advertisements

About Jerome

I am a senior C# developer in Johannesburg, South Africa. I am also a recovering addict, who spent nearly eight years using methamphetamine. I write on my recovery blog about my lessons learned and sometimes give advice to others who have made similar mistakes, often from my viewpoint as an atheist, and I also write some C# programming articles on my programming blog.
This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s