Base64 encoding and decoding in .NET

What do you do when you get an email asking you a question, but your reply with the answer bounces?
I'm not sure what the best answer is, but I figure if I put the answer up here it might help someone else with the same question, even if the original asker doesn't find it.
The email I got was about my online base64 encoder and decoder:


I ask you some help


I have big problem of base64 decode

please help me.


please base64 decode source in be given me

Now don't laugh - his English is a hell of a lot better than my Korean.
The thing is, base64 encoding and decoding in .NET is utterly trivial - as long as you know the base class library calls to make.  If you don't know they're there, you can spend a while implementing your own algorithm, and that's just Not Fun.
So, here's my reply.  Maybe it'll help someone else find the right functions to call.

Hi there,


I could send you the file, but I don't think it would help you much.  There's an awful lot of ASP.NET 'plumbing' code and very little base64 code.


Instead, here's a sample program I just wrote that shows how to do the base64 encoding and decoding in .NET.  It's in C# - scroll down for the VB.NET version:


using System; using System.Text;   public class Base64Decoder {       public static void Main ()       {             string inputText = "This is some text.";             Console.Out.WriteLine ("Input text: {0}", inputText);             byte [] bytesToEncode = Encoding.UTF8.GetBytes (inputText);               string encodedText = Convert.ToBase64String (bytesToEncode);             Console.Out.WriteLine ("Encoded text: {0}", encodedText);               byte [] decodedBytes = Convert.FromBase64String (encodedText);             string decodedText = Encoding.UTF8.GetString (decodedBytes);             Console.Out.WriteLine ("Decoded text: {0}", decodedText);               Console.Out.Write ("Press enter to finish.");             Console.In.ReadLine ();               return;       } }


Here's my attempt at translating that to VB.NET.  I'm not a VB.NET programmer, so I may have done something silly.  It seems to work though.


imports Microsoft.VisualBasic imports System Imports System.Text   public module Base64Decoder       sub Main             Dim inputText As String             inputText = "This is some text."             Console.Out.WriteLine ("Input text: {0}", inputText)               Dim bytesToEncode As Byte()             bytesToEncode = Encoding.UTF8.GetBytes (inputText)               Dim encodedText As String             encodedText = Convert.ToBase64String (bytesToEncode)             Console.Out.WriteLine ("Encoded text: {0}", encodedText)               Dim decodedBytes As byte()             decodedBytes = Convert.FromBase64String (encodedText)               Dim decodedText As String             decodedText = Encoding.UTF8.GetString (decodedBytes)             Console.Out.WriteLine ("Decoded text: {0}", decodedText)               Console.Out.Write ("Press enter to finish.")             Console.In.ReadLine ()       end sub end module 

If you don't want to mess around with byte arrays, you can just use Convert.ToBase64String and Convert.FromBase64String, but doing the conversion to/from a byte array gives you a bit more control if you're faced with strange character encodings.

Hope this helps,



Unfortunately that reply (to a Korean email address) bounced with the error "PERM_FAILURE: SMTP Error (state 13): 551 User not local". Maybe the person who originally asked the question will find the answer in the blog post.

Tags: .NET
Created by on Logo15659OpinionatedGeek Ltd.Logo15659

Some base64 tips

I just saw a post on Novell's 'Cool Blogs' blog that has a useful tip on base64 decoding.  Apparently, if you save your base64 text to a file with an extension of '.b64', Winzip can open the file and allow you to extract the content.   That...
Created by anonymous on Saturday, 12 January 2008 at 2:48PM (source)

Thank u

hi this is vetri. really this encoding and decoding information very useful in my project. i've little bit confusion where to implement and how to implement these concept... now i've cleared...
Created by anonymous on Monday, 15 March 2010 at 3:03PM

HElp me

Hi Please help me i have problem to decode html code in javascript
actually it's encoded at serverside and i want to decode at client side
Created by anonymous on Thursday, 1 April 2010 at 6:14AM

Javascript encoding/decoding

A quick search turned up this as a way of doing base64 decoding client-side:
Created by OpinionatedGeek on Thursday, 1 April 2010 at 9:58AM (source)

Covert a file (pdf, doc etc ) to Base64

   Please help to do Base64 Encoding and decoding of a file like pdf of doc.

Created by anonymous on Tuesday, 28 September 2010 at 1:37PM

Converting files

If you're looking for a quick, simple way to do it, you can go to:

Click 'Choose file' and pick the file you want converted. Then click the 'Encode' button.

The encoded version of the file then appears in the Encoded Output pane. Save all of that text to a file, giving it whatever file name and extension you want).

The blog post you commented on gives more detail on how to write a program to do this, if you're interested.

Hope this helps,

Created by OpinionatedGeek on Sunday, 3 October 2010 at 4:00PM (source)

Convert To Japannese

thanks for tutorial. But how to convert japannese character
Created by anonymous on Saturday, 10 November 2012 at 5:29AM (source)


Created by OpinionatedGeek on Sunday, 11 November 2012 at 6:33PM (source)

Password encryption/ decryption code in .NET | ...

Excerpt could not be generated.
Created by anonymous on Saturday, 16 February 2013 at 2:11PM (source)

How do you URL Base64 Encode and Decode a conca...

Excerpt could not be generated.
Created by anonymous on Tuesday, 9 December 2014 at 9:50PM (source)

json converter

Thanks for sharing your thoughts & excitements about future of the web. good post guys and thanks for your article this is simply awesome. thanks for your info! 

Created by apkoii on Tuesday, 17 January 2017 at 8:16AM (source)