Base-64 encoding is a commonly used encoding format useful for displaying and transporting encrypted data, where often appear bytes without visible representation.
DidiSoft OpenSSL Library for .NET offers a set of classes that provide Base-64 encoding and decoding methods:
DidiSoft.Base64.Base64Util – provides static methods for base-64 encoding decoding files, strings and byte arrays
DidiSoft.Base64.Base64InputStream – input Stream that reads transparently base-64 encoded data as if it wasn’t encoded at all
DidiSoft.Base64.Base64OutputStream – output Stream that writes transparently base-64 encoded data
Table of samples
- Read Base-64 encoded data with Base64InputStream
- Write Base-64 encoded data with Base64OuptutStream
- Convert file to/from Base-64
- Convert string to/from Base-64
- Convert byte array to/from Base-64
Read Base-64 encoded data with Stream with Base64InputStream
DidiSoft.Base64.Base64InputStream is a System.IO.Stream sub class that can wrap another Stream containing base-64 encoded data and its read operations will return the decoded data transparently for the invoking code. Here is an example:
C# example
1 2 3 4 5 6 7 8 9 10 11 12 13 | Stream base64DataStream = ... // stream containing base-64 encoded data // wrap the base-64 encoded Stream using (Stream decoderStream = new Base64InputStream(dataStream)) // destination where decoded data will be written using (Stream outputStream = File.Create("decoded.dat")) { byte[] buffer = new byte[1024]; int i = 0; while ((i = decoderStream.Read(buffer, 0, buffer.Length)) > 0) { outputStream.Write(buffer, 0, i); } } |
VB.NET example
1 2 3 4 5 6 7 8 9 10 11 12 13 | Dim dataStream As Stream = ... ' base64 encoded data stream ' wrap the base-64 encoded Stream Using decoderStream As New Base64InputStream(dataStream) ' destination where decoded data will be written Using decodedStream As File.Create("decoded.dat") Dim buffer As Byte() = New Byte(1023) {} Dim i As Integer = 0 While (InlineAssignHelper(i, decoderStream.Read(buffer, 0, buffer.Length))) > 0 decodedStream.Write(buffer, 0, i) End While End Using End Using |
Write Base-64 encoded data with Stream with Base64OuptutStream
DidiSoft.Base64.Base64OuptutStream is a System.IO.Stream subclass designed to wrap output streams and convert the data passed to the Write methods into base-64 format. The examples below illustrate how to utilize this class:
C# example
1 2 3 4 5 6 7 8 9 10 11 12 13 | Stream dataStream = ... // input data // destination where encoded data will be written using (Stream outputStream = File.Create("base64.txt")) // wrap base-64 encoded Stream using (Stream encoderStream = new Base64OutputStream(outputStream)){ byte[] buffer = new byte[1024]; int i = 0; while ((i = dataStream.Read(buffer, 0, buffer.Length)) > 0) { encoderStream.Write(buffer, 0, i); } } |
VB.NET example
1 2 3 4 5 6 7 8 9 10 11 12 | Dim dataStream As Stream = ... ' input date ' destination where encoded data will be written Using outputStream As File.Create("base64.txt") ' wrap as base-64 encoded Stream Using encoderStream As Stream = New Base64OutputStream(outputStream) Dim buffer As Byte() = New Byte(1023) {} Dim i As Integer = 0 While (InlineAssignHelper(i, dataStream.Read(buffer, 0, buffer.Length))) > 0 encoderStream.Write(buffer, 0, i) End While End Using End Using |
Convert file to/from Base-64
The class DidiSoft.Base64.Base64Util provides Encode/Decode methods that work with files, strings, Streams and byte arrays. The example code below illustrates how to convert to and back from base-64 encoded format:
C# example
1 2 3 4 5 6 7 8 9 10 11 | using DidiSoft.Base64; public class Base64FileDemo { public void Demo() { Base64Util.Encode(@"Data\Input.txt", @"Data\Output.txt"); // Decoded.txt contains the same data as Input.txt Base64Util.Decode(@"Data\Output.txt", @"Data\Decoded.txt"); } } |
VB.NET example
1 2 3 4 5 6 7 8 9 | Imports DidiSoft.Base64 Public Class Base64FileDemo Public Sub Demo() Base64Util.Encode("Data\Input.txt", "Data\Output.txt") ' Decoded.txt contains the same data as Input.txt Base64Util.Decode("Data\Output.txt", "Data\Decoded.txt") End Sub End Class |
Convert string to/from Base-64
Converting a string to and from base-64 format is similar to the example above. The specific point when working with strings is that we need an extra step when deconstructing a string message back from base-64 format. This is due to the fact that the Base64Util.Decode method cannot know for sure of the result from the decoding is a valid string message and this this extra step at the end of the sample code:
C# example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using DidiSoft.Base64; public class Base64StringDemo { public void Demo() { string message = "Hello World"; string encoded = Base64Util.Encode(message); // bae-64 decoding returns byte array as we don't know in advance // is the decoded data a valid String message byte[] decoded = Base64Util.Decode(encoded); // we can now recreate the decoded String message string decodedMessage = UTF8Encoding.UTF8.GetString(decoded); } } |
VB.NET example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Imports DidiSoft.Base64 Public Class Base64StringDemo Public Sub Demo() Dim message As String = "Hello World" Dim encoded As String = Base64Util.Encode(message) ' bae-64 decoding returns byte array as we don't know in advance ' is the decoded data a valid String message Dim decoded As Byte() = Base64Util.Decode(encoded) ' we can now recreate the decoded String message Dim decodedMessage As String = UTF8Encoding.UTF8.GetString(decoded) End Sub End Class |
Convert byte array to/from Base-64
Converting a byte array to and back from base 64 encoded format is trivial as you can see from the sample code below:
C# example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | using DidiSoft.Base64; public class Base64ByteArrayDemo { public void Demo() { byte[] input = ... // input data string encoded = Base64Util.Encode(input); // decoded data is the same as the input byte[] decoded = Base64Util.Decode(encoded); } } |
VB.NET example
1 2 3 4 5 6 7 8 9 10 11 12 | Imports DidiSoft.Base64 Public Class Base64ByteArrayDemo Public Sub Demo() Dim input As Byte() = ... ' input data Dim encoded As String = Base64Util.Encode(input) ' decoded data is the same as the input Dim decoded As Byte() = Base64Util.Decode(encoded) End Sub End Class |
Summary
This page illustrated how to perform Base-64 encoding of data stored in files, streams, string variables or byte arrays with C# and VB.NET examples. The sample code above can easily be adopted for any programming language supported on the .NET Framework or .NET Core.