A Beginner's Guide to Base64 Encoding and Decoding in C#

Introduce how to encode and decode strings using Base64 in C#, in order to transmit binary data in scenarios where only text can be transferred.
On this page

A Beginner's Guide to Base64 Encoding and Decoding in C#

Excerpt

If you’re working with data in C#, you may come across the need to encode or decode strings using Base64. This can be especially useful for transmitting binary data over channels that can only handle text. In this blog post, we’ll go over how to encode and decode strings using Base64 in C#.

Encoding String to Base64

To encode a string to Base64 in C#, you can use the Convert class’s ToBase64String method. Here’s an example code snippet:

1string originalString = "Hello, world!";
2byte[] originalBytes = Encoding.UTF8.GetBytes(originalString);
3string encodedString = Convert.ToBase64String(originalBytes);
4Console.WriteLine("Encoded string: " + encodedString);

In this example, we first convert the original string to a byte array using the Encoding.UTF8.GetBytes method. We then pass this byte array to the Convert.ToBase64String method, which returns the encoded string. Finally, we print out the encoded string to the console.

Decoding Base64 to String

To decode a Base64 string back to its original string representation, you can use the Convert class’s FromBase64String method. Here’s an example code snippet:

1string encodedString = "SGVsbG8sIHdvcmxkIQ==";
2byte[] encodedBytes = Convert.FromBase64String(encodedString);
3string originalString = Encoding.UTF8.GetString(encodedBytes);
4Console.WriteLine("Original string: " + originalString);

In this example, we first have our encoded string. We then pass this encoded string to the Convert.FromBase64String method, which returns a byte array representing the original string. Finally, we use the Encoding.UTF8.GetString method to convert this byte array back to the original string. Once again, we print out the original string to the console.

Precautions When Using

While using Base64 encoding and decoding can be useful, there are a few precautions you should take:

  1. Don’t use Base64 for encryption. Base64 is not a form of encryption and can be easily decoded. If you need to encrypt your data, use a proper encryption scheme.

  2. Be aware of the increased size of Base64 data. Because Base64 encodes binary data as ASCII text, the resulting size of the encoded data can be up to 33% larger than the original data. Keep this in mind when transmitting data over channels with limited bandwidth.

  3. Be careful when decoding untrusted data. Decoding Base64 data can potentially introduce security vulnerabilities if the input data is not properly validated. Make sure you’re only decoding trusted data to avoid these issues.

By keeping these precautions in mind, you can use Base64 encoding and decoding in a safe and effective manner.

In conclusion, using Base64 encoding and decoding in C# can be a valuable tool when working with data. By following the steps outlined in this blog post and taking appropriate precautions, you can use Base64 effectively and safely in your applications.

An online base64 tool to quickly verify your answers