Base64 in C#

How to encode and decode Base64 in C# — with copy-paste examples and a live converter to check your output.

The .NET Convert class provides ToBase64String and FromBase64String, operating on byte arrays. Use Encoding.UTF8 to convert between string and bytes.

Encode to Base64 in C#

C#
using System;
using System.Text;

string encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes("Hello, World!"));

Decode Base64 in C#

C#
using System;
using System.Text;

string text = Encoding.UTF8.GetString(Convert.FromBase64String(encoded));

Notes & gotchas

Convert.ToBase64String/FromBase64String use the standard alphabet only. For URL-safe output, replace "+"/"/" with "-"/"_" and strip padding yourself, or use Base64UrlEncoder from Microsoft.IdentityModel.Tokens.

Try it live

0 characters

Base64 in C# — FAQ

How do I Base64 encode a string in C#?+

Use the code shown above. Convert.ToBase64String/FromBase64String use the standard alphabet only. For URL-safe output, replace "+"/"/" with "-"/"_" and strip padding yourself, or use Base64UrlEncoder from Microsoft.IdentityModel.Tokens.

How do I decode Base64 back to text in C#?+

Use the decode snippet above. Base64 decoding is lossless, so you get the exact original bytes back; decode them with UTF-8 to recover text.

Is Base64 encoding the same across programming languages?+

Yes. Base64 is a standard (RFC 4648), so a string encoded in one language decodes correctly in any other. Only the API differs, not the output.

Does Base64 secure my data?+

No. Base64 is an encoding, not encryption — anyone can decode it. Never use it to protect secrets.