Base64
Encoding
Decoding
Programming
Web Development

How do I encode and decode a base64 string?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Base64 encoding is a method used to encode binary data, such as images or sound files, into ASCII text. This encoding helps to transport data over media that are designed to deal with textual data. This ensures that the data remains intact without modification during transport. Base64 is commonly used in numerous applications including email via MIME, and storing complex data in XML or JSON.

What is Base64 Encoding?

Base64 encoding schemes are commonly used when there is a need to encode binary data, especially when that data needs to be stored and transferred over media that are designed to deal with text. This encoding helps to ensure that the data remains intact without modification during transport. The process typically takes every three bytes of binary data and converts it into four bytes of text by interpreting every three bytes as four separate numbers.

How Does Base64 Encoding Work?

Base64 encoding divides the data into chunks of three bytes, or 24 bits. Each byte consists of 8 bits. Thus, three bytes contain a total of 24 bits. The base64 conversion process takes these 24 bits and divides them into four groups of six bits each. Each group of six bits is then used as an index into a set of 64 characters array. The 64 characters in the base64 alphabet are:

  • A-Z (capital letters)
  • a-z (lowercase letters)
  • 0-9 (numbers)
    • (plus sign)
  • / (forward slash)

The output of Base64 encoding is thus a string that may include any of these characters and possibly ending with one or two '=' characters. These '=' characters are padding which are added to ensure the string length is a multiple of four.

Encoding and Decoding Base64 in Different Programming Languages

Programming languages handle base64 encoding in different ways. Below are simple examples of encoding and decoding strings in Python, JavaScript, and Java.

Python

In Python, you can use the base64 module to encode and decode base64.

python
1import base64
2
3# Encoding
4data = "Base64 Encoding in Python"
5encoded_data = base64.b64encode(data.encode())
6print(encoded_data)
7
8# Decoding
9decoded_data = base64.b64decode(encoded_data).decode()
10print(decoded_data)

JavaScript

JavaScript provides built-in methods to handle base64: btoa() for encoding and atob() for decoding.

javascript
1// Encoding
2const data = "Base64 Encoding in JavaScript";
3const encodedData = btoa(data);
4console.log(encodedData);
5
6// Decoding
7const decodedData = atob(encodedData);
8console.log(decodedData);

Java

Java provides classes under java.util.Base64 to handle base64 encoding and decoding.

java
1import java.util.Base64;
2
3public class Main {
4    public static void main(String[] args) {
5        String originalInput = "Base64 Encoding in Java";
6        String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
7        System.out.println(encodedString);
8
9        byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
10        String decodedString = new String(decodedBytes);
11        System.out.println(decodedString);
12    }
13}

Summary Table

LanguageMethod for EncodingMethod for Decoding
Pythonbase64.b64encode()base64.b64decode()
JavaScriptbtoa()atob()
JavaBase64.getEncoder()Base64.getDecoder()

Security Considerations

While Base64 is useful for encoding data, it's important to note that it is not a secure way to protect or hide data, as it is easily reversible. Base64 should not be confused with encryption or hashing, as it offers no security features and is straightforward to decode.

Conclusion

Base64 encoding and decoding is a widely used utility that simplifies the transmission and storage of binary data on systems that predominantly handle text data. It's pivotal to understand the difference between encoding and true encryption or security methods as Base64 alone provides no data integrity or confidentiality.


Course illustration
Course illustration

All Rights Reserved.