Base64 encoding is a widely used method for converting binary data into a text-based representation that can be easily transmitted over networks or stored in databases. In this tutorial, we will explore the basics of Base64 encoding and decoding, including how to encode and decode strings using various approaches.
Introduction to Base64 Encoding
Base64 encoding works by dividing the input data into 6-bit chunks, which are then mapped to a set of 64 characters that can be safely transmitted over text-based protocols. The resulting encoded string consists of these characters, with optional padding added to ensure that the length is a multiple of 4.
Encoding Strings
To encode a string using Base64, you can use the following approach:
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
This code converts the input string to a byte array using UTF-8 encoding, and then uses the ToBase64String
method to encode the bytes into a Base64-encoded string.
Decoding Strings
To decode a Base64-encoded string back into its original form, you can use the following approach:
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}
This code uses the FromBase64String
method to convert the encoded string back into a byte array, and then converts the bytes back into a string using UTF-8 encoding.
URL-Safe Base64 Encoding
When transmitting Base64-encoded data over URLs or other text-based protocols, it’s often necessary to use a URL-safe variant of the encoding. This can be achieved by replacing certain characters with their URL-safe equivalents:
public static class Base64Url
{
public static string Encode(string text)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text)).TrimEnd('=').Replace('+', '-')
.Replace('/', '_');
}
public static string Decode(string text)
{
text = text.Replace('_', '/').Replace('-', '+');
switch (text.Length % 4)
{
case 2:
text += "==";
break;
case 3:
text += "=";
break;
}
return Encoding.UTF8.GetString(Convert.FromBase64String(text));
}
}
This code provides a URL-safe variant of the Base64 encoding and decoding methods, using the TrimEnd
method to remove any trailing padding characters and replacing certain characters with their URL-safe equivalents.
Individual Digit Encoding and Decoding
In some cases, it may be necessary to encode or decode individual Base64 digits. This can be achieved using the following approach:
public static int DecodeBase64Digit(char digit, string digit62 = "+-.~", string digit63 = "/_,")
{
if (digit >= 'A' && digit <= 'Z') return digit - 'A';
if (digit >= 'a' && digit <= 'z') return digit + (26 - 'a');
if (digit >= '0' && digit <= '9') return digit + (52 - '0');
if (digit62.IndexOf(digit) > -1) return 62;
if (digit63.IndexOf(digit) > -1) return 63;
return -1;
}
public static char EncodeBase64Digit(int digit, char digit62 = '+', char digit63 = '/')
{
digit &= 63;
if (digit < 52)
return (char)(digit < 26 ? digit + 'A' : digit + ('a' - 26));
else if (digit < 62)
return (char)(digit + ('0' - 52));
else
return digit == 62 ? digit62 : digit63;
}
This code provides methods for encoding and decoding individual Base64 digits, using a combination of arithmetic operations and string indexing to determine the correct mapping.
Conclusion
In this tutorial, we have explored the basics of Base64 encoding and decoding, including how to encode and decode strings using various approaches. We have also examined URL-safe variants of the encoding and decoding methods, as well as individual digit encoding and decoding. By understanding these concepts, developers can effectively use Base64 encoding in a wide range of applications.