Base64 Encoder and Decoder: Convert Text to/from Base64
Free online tool to encode text to Base64 or decode Base64 strings back to text. Supports standard and URL-safe Base64 encoding with instant conversion.
Base64 Encoder/Decoder
Convert text to and from Base64 encoding
Documentation
Base64 Encoder and Decoder
Introduction
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is designed to carry data stored in binary formats across channels that only reliably support text content. Base64 encoding converts binary data into a set of 64 characters (hence the name) that can be safely transmitted over text-based protocols without data corruption.
The Base64 character set consists of:
- Uppercase letters A-Z (26 characters)
- Lowercase letters a-z (26 characters)
- Digits 0-9 (10 characters)
- Two additional characters, typically "+" and "/" (2 characters)
This tool allows you to easily encode text to Base64 format or decode Base64 strings back to their original text. It's particularly useful for developers, IT professionals, and anyone working with data that needs to be transmitted safely across text-based channels.
How Base64 Encoding Works
Encoding Process
Base64 encoding works by converting each group of three bytes (24 bits) of binary data into four Base64 characters. The process follows these steps:
- Convert the input text to its binary representation (using ASCII or UTF-8 encoding)
- Group the binary data into chunks of 24 bits (3 bytes)
- Split each 24-bit chunk into four 6-bit groups
- Convert each 6-bit group to its corresponding Base64 character
When the input length is not divisible by 3, padding with "=" characters is added to maintain the 4:3 ratio of output to input lengths.
Mathematical Representation
For a sequence of bytes , the corresponding Base64 characters are calculated as:
Where represents the -th character in the Base64 alphabet.
Decoding Process
Base64 decoding reverses the encoding process:
- Convert each Base64 character to its 6-bit value
- Concatenate these 6-bit values
- Group the bits into 8-bit chunks (bytes)
- Convert each byte to its corresponding character
Padding
When the number of bytes to encode is not divisible by 3, padding is applied:
- If there's one byte remaining, it's converted to two Base64 characters followed by "=="
- If there are two bytes remaining, they're converted to three Base64 characters followed by "="
Example
Let's encode the text "Hello" to Base64:
- ASCII representation of "Hello": 72 101 108 108 111
- Binary representation: 01001000 01100101 01101100 01101100 01101111
- Grouping into 6-bit chunks: 010010 000110 010101 101100 011011 000110 1111
- The last chunk only has 4 bits, so we pad with zeros: 010010 000110 010101 101100 011011 000110 111100
- Converting to decimal: 18, 6, 21, 44, 27, 6, 60
- Looking up in the Base64 alphabet: S, G, V, s, b, G, 8
- The result is "SGVsbG8="
Note the "=" padding at the end because the input length (5 bytes) is not divisible by 3.
Formula
The general formula for calculating the length of a Base64 encoded string is:
Where represents the ceiling function (rounding up to the nearest integer).
Use Cases
Base64 encoding is widely used in various applications:
-
Email Attachments: MIME (Multipurpose Internet Mail Extensions) uses Base64 to encode binary attachments in email.
-
Data URLs: Embedding small images, fonts, or other resources directly in HTML, CSS, or JavaScript using the
data:
URL scheme. -
API Communications: Safely transmitting binary data in JSON payloads or other text-based API formats.
-
Storing Binary Data in Text Formats: When binary data needs to be stored in XML, JSON, or other text-based formats.
-
Authentication Systems: Basic Authentication in HTTP uses Base64 encoding (though it's not for security, just for encoding).
-
Cryptography: As part of various cryptographic protocols and systems, often for encoding keys or certificates.
-
Cookie Values: Encoding complex data structures to be stored in cookies.
Alternatives
While Base64 is widely used, there are alternatives that might be more appropriate in certain situations:
-
URL-safe Base64: A variant that uses "-" and "_" instead of "+" and "/" to avoid URL encoding issues. Useful for data that will be included in URLs.
-
Base32: Uses a 32-character set, resulting in longer output but with better human readability and case insensitivity.
-
Hex Encoding: Simple conversion to hexadecimal, which is less efficient (doubles the size) but very simple and widely supported.
-
Binary Transfer: For large files or when efficiency is crucial, direct binary transfer protocols like HTTP with appropriate Content-Type headers are preferable.
-
Compression + Base64: For large text data, compressing before encoding can mitigate the size increase.
-
JSON/XML Serialization: For structured data, using native JSON or XML serialization might be more appropriate than Base64 encoding.
History
Base64 encoding has its roots in early computing and telecommunications systems where binary data needed to be transmitted over channels designed for text.
The formal specification of Base64 was first published in 1987 as part of RFC 989, which defined Privacy Enhanced Mail (PEM). This was later updated in RFC 1421 (1993) and RFC 2045 (1996, as part of MIME).
The term "Base64" comes from the fact that the encoding uses 64 different ASCII characters to represent binary data. This choice of 64 characters was deliberate, as 64 is a power of 2 (2^6), which makes the conversion between binary and Base64 efficient.
Over time, several variants of Base64 have emerged:
- Standard Base64: As defined in RFC 4648, using A-Z, a-z, 0-9, +, / and = for padding
- URL-safe Base64: Uses - and _ instead of + and / to avoid URL encoding issues
- Filename-safe Base64: Similar to URL-safe, designed for use in filenames
- Modified Base64 for IMAP: Used in the IMAP protocol with a different set of special characters
Despite being over three decades old, Base64 remains a fundamental tool in modern computing, particularly with the rise of web applications and APIs that rely heavily on text-based data formats like JSON.
Code Examples
Here are examples of Base64 encoding and decoding in various programming languages:
1// JavaScript Base64 Encoding/Decoding
2function encodeToBase64(text) {
3 return btoa(text);
4}
5
6function decodeFromBase64(base64String) {
7 try {
8 return atob(base64String);
9 } catch (e) {
10 throw new Error("Invalid Base64 string");
11 }
12}
13
14// Example usage
15const originalText = "Hello, World!";
16const encoded = encodeToBase64(originalText);
17console.log("Encoded:", encoded); // SGVsbG8sIFdvcmxkIQ==
18
19try {
20 const decoded = decodeFromBase64(encoded);
21 console.log("Decoded:", decoded); // Hello, World!
22} catch (error) {
23 console.error(error.message);
24}
25
1# Python Base64 Encoding/Decoding
2import base64
3
4def encode_to_base64(text):
5 # Convert string to bytes and then encode
6 text_bytes = text.encode('utf-8')
7 base64_bytes = base64.b64encode(text_bytes)
8 return base64_bytes.decode('utf-8')
9
10def decode_from_base64(base64_string):
11 try:
12 # Convert base64 string to bytes and then decode
13 base64_bytes = base64_string.encode('utf-8')
14 text_bytes = base64.b64decode(base64_bytes)
15 return text_bytes.decode('utf-8')
16 except Exception as e:
17 raise ValueError(f"Invalid Base64 string: {e}")
18
19# Example usage
20original_text = "Hello, World!"
21encoded = encode_to_base64(original_text)
22print(f"Encoded: {encoded}") # SGVsbG8sIFdvcmxkIQ==
23
24try:
25 decoded = decode_from_base64(encoded)
26 print(f"Decoded: {decoded}") # Hello, World!
27except ValueError as e:
28 print(e)
29
1// Java Base64 Encoding/Decoding
2import java.util.Base64;
3import java.nio.charset.StandardCharsets;
4
5public class Base64Example {
6 public static String encodeToBase64(String text) {
7 byte[] textBytes = text.getBytes(StandardCharsets.UTF_8);
8 byte[] encodedBytes = Base64.getEncoder().encode(textBytes);
9 return new String(encodedBytes, StandardCharsets.UTF_8);
10 }
11
12 public static String decodeFromBase64(String base64String) {
13 try {
14 byte[] base64Bytes = base64String.getBytes(StandardCharsets.UTF_8);
15 byte[] decodedBytes = Base64.getDecoder().decode(base64Bytes);
16 return new String(decodedBytes, StandardCharsets.UTF_8);
17 } catch (IllegalArgumentException e) {
18 throw new IllegalArgumentException("Invalid Base64 string: " + e.getMessage());
19 }
20 }
21
22 public static void main(String[] args) {
23 String originalText = "Hello, World!";
24 String encoded = encodeToBase64(originalText);
25 System.out.println("Encoded: " + encoded); // SGVsbG8sIFdvcmxkIQ==
26
27 try {
28 String decoded = decodeFromBase64(encoded);
29 System.out.println("Decoded: " + decoded); // Hello, World!
30 } catch (IllegalArgumentException e) {
31 System.err.println(e.getMessage());
32 }
33 }
34}
35
1<?php
2// PHP Base64 Encoding/Decoding
3function encodeToBase64($text) {
4 return base64_encode($text);
5}
6
7function decodeFromBase64($base64String) {
8 $decoded = base64_decode($base64String, true);
9 if ($decoded === false) {
10 throw new Exception("Invalid Base64 string");
11 }
12 return $decoded;
13}
14
15// Example usage
16$originalText = "Hello, World!";
17$encoded = encodeToBase64($originalText);
18echo "Encoded: " . $encoded . "\n"; // SGVsbG8sIFdvcmxkIQ==
19
20try {
21 $decoded = decodeFromBase64($encoded);
22 echo "Decoded: " . $decoded . "\n"; // Hello, World!
23} catch (Exception $e) {
24 echo "Error: " . $e->getMessage() . "\n";
25}
26?>
27
1// C# Base64 Encoding/Decoding
2using System;
3using System.Text;
4
5class Base64Example
6{
7 public static string EncodeToBase64(string text)
8 {
9 byte[] textBytes = Encoding.UTF8.GetBytes(text);
10 return Convert.ToBase64String(textBytes);
11 }
12
13 public static string DecodeFromBase64(string base64String)
14 {
15 try
16 {
17 byte[] base64Bytes = Convert.FromBase64String(base64String);
18 return Encoding.UTF8.GetString(base64Bytes);
19 }
20 catch (FormatException)
21 {
22 throw new FormatException("Invalid Base64 string");
23 }
24 }
25
26 static void Main()
27 {
28 string originalText = "Hello, World!";
29 string encoded = EncodeToBase64(originalText);
30 Console.WriteLine($"Encoded: {encoded}"); // SGVsbG8sIFdvcmxkIQ==
31
32 try
33 {
34 string decoded = DecodeFromBase64(encoded);
35 Console.WriteLine($"Decoded: {decoded}"); // Hello, World!
36 }
37 catch (FormatException e)
38 {
39 Console.WriteLine($"Error: {e.Message}");
40 }
41 }
42}
43
1# Ruby Base64 Encoding/Decoding
2require 'base64'
3
4def encode_to_base64(text)
5 Base64.strict_encode64(text)
6end
7
8def decode_from_base64(base64_string)
9 begin
10 Base64.strict_decode64(base64_string)
11 rescue ArgumentError => e
12 raise "Invalid Base64 string: #{e.message}"
13 end
14end
15
16# Example usage
17original_text = "Hello, World!"
18encoded = encode_to_base64(original_text)
19puts "Encoded: #{encoded}" # SGVsbG8sIFdvcmxkIQ==
20
21begin
22 decoded = decode_from_base64(encoded)
23 puts "Decoded: #{decoded}" # Hello, World!
24rescue StandardError => e
25 puts "Error: #{e.message}"
26end
27
1// Go Base64 Encoding/Decoding
2package main
3
4import (
5 "encoding/base64"
6 "fmt"
7)
8
9func encodeToBase64(text string) string {
10 return base64.StdEncoding.EncodeToString([]byte(text))
11}
12
13func decodeFromBase64(base64String string) (string, error) {
14 bytes, err := base64.StdEncoding.DecodeString(base64String)
15 if err != nil {
16 return "", fmt.Errorf("invalid Base64 string: %v", err)
17 }
18 return string(bytes), nil
19}
20
21func main() {
22 originalText := "Hello, World!"
23 encoded := encodeToBase64(originalText)
24 fmt.Println("Encoded:", encoded) // SGVsbG8sIFdvcmxkIQ==
25
26 decoded, err := decodeFromBase64(encoded)
27 if err != nil {
28 fmt.Println("Error:", err)
29 } else {
30 fmt.Println("Decoded:", decoded) // Hello, World!
31 }
32}
33
1// Swift Base64 Encoding/Decoding
2import Foundation
3
4func encodeToBase64(_ text: String) -> String? {
5 if let data = text.data(using: .utf8) {
6 return data.base64EncodedString()
7 }
8 return nil
9}
10
11func decodeFromBase64(_ base64String: String) -> String? {
12 if let data = Data(base64Encoded: base64String) {
13 return String(data: data, encoding: .utf8)
14 }
15 return nil
16}
17
18// Example usage
19let originalText = "Hello, World!"
20if let encoded = encodeToBase64(originalText) {
21 print("Encoded: \(encoded)") // SGVsbG8sIFdvcmxkIQ==
22
23 if let decoded = decodeFromBase64(encoded) {
24 print("Decoded: \(decoded)") // Hello, World!
25 } else {
26 print("Error: Could not decode Base64 string")
27 }
28} else {
29 print("Error: Could not encode text")
30}
31
1' Excel VBA Base64 Encoding/Decoding
2' Note: This requires a reference to Microsoft XML, v6.0
3Function EncodeToBase64(text As String) As String
4 Dim xmlObj As Object
5 Set xmlObj = CreateObject("MSXML2.DOMDocument")
6
7 Dim xmlNode As Object
8 Set xmlNode = xmlObj.createElement("b64")
9
10 xmlNode.DataType = "bin.base64"
11 xmlNode.nodeTypedValue = StrConv(text, vbFromUnicode)
12
13 EncodeToBase64 = xmlNode.text
14
15 Set xmlNode = Nothing
16 Set xmlObj = Nothing
17End Function
18
19Function DecodeFromBase64(base64String As String) As String
20 On Error GoTo ErrorHandler
21
22 Dim xmlObj As Object
23 Set xmlObj = CreateObject("MSXML2.DOMDocument")
24
25 Dim xmlNode As Object
26 Set xmlNode = xmlObj.createElement("b64")
27
28 xmlNode.DataType = "bin.base64"
29 xmlNode.text = base64String
30
31 DecodeFromBase64 = StrConv(xmlNode.nodeTypedValue, vbUnicode)
32
33 Set xmlNode = Nothing
34 Set xmlObj = Nothing
35 Exit Function
36
37ErrorHandler:
38 DecodeFromBase64 = "Error: Invalid Base64 string"
39End Function
40
41' Usage in a worksheet:
42' =EncodeToBase64("Hello, World!")
43' =DecodeFromBase64("SGVsbG8sIFdvcmxkIQ==")
44
1# R Base64 Encoding/Decoding
2# Requires the 'base64enc' package
3# install.packages("base64enc")
4library(base64enc)
5
6encode_to_base64 <- function(text) {
7 # Convert text to raw bytes, then encode
8 text_raw <- charToRaw(text)
9 base64_encoded <- base64encode(text_raw)
10 return(rawToChar(base64_encoded))
11}
12
13decode_from_base64 <- function(base64_string) {
14 tryCatch({
15 # Convert base64 string to raw, then decode
16 base64_raw <- charToRaw(base64_string)
17 decoded_raw <- base64decode(base64_raw)
18 return(rawToChar(decoded_raw))
19 }, error = function(e) {
20 stop(paste("Invalid Base64 string:", e$message))
21 })
22}
23
24# Example usage
25original_text <- "Hello, World!"
26encoded <- encode_to_base64(original_text)
27cat("Encoded:", encoded, "\n") # SGVsbG8sIFdvcmxkIQ==
28
29tryCatch({
30 decoded <- decode_from_base64(encoded)
31 cat("Decoded:", decoded, "\n") # Hello, World!
32}, error = function(e) {
33 cat("Error:", e$message, "\n")
34})
35
1% MATLAB Base64 Encoding/Decoding
2function demo_base64()
3 originalText = 'Hello, World!';
4
5 % Encode
6 encoded = encode_to_base64(originalText);
7 fprintf('Encoded: %s\n', encoded); % SGVsbG8sIFdvcmxkIQ==
8
9 % Decode
10 try
11 decoded = decode_from_base64(encoded);
12 fprintf('Decoded: %s\n', decoded); % Hello, World!
13 catch e
14 fprintf('Error: %s\n', e.message);
15 end
16end
17
18function encoded = encode_to_base64(text)
19 % Convert text to uint8 array and encode
20 bytes = uint8(text);
21 encoded = base64encode(bytes);
22end
23
24function decoded = decode_from_base64(base64String)
25 try
26 % Decode base64 string to uint8 array
27 bytes = base64decode(base64String);
28 decoded = char(bytes);
29 catch
30 error('Invalid Base64 string');
31 end
32end
33
1// C Base64 Encoding/Decoding using OpenSSL
2#include <stdio.h>
3#include <string.h>
4#include <openssl/bio.h>
5#include <openssl/evp.h>
6#include <openssl/buffer.h>
7#include <stdint.h>
8
9char* encode_to_base64(const char* input) {
10 BIO *bio, *b64;
11 BUF_MEM *bufferPtr;
12
13 b64 = BIO_new(BIO_f_base64());
14 bio = BIO_new(BIO_s_mem());
15 bio = BIO_push(b64, bio);
16
17 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
18 BIO_write(bio, input, strlen(input));
19 BIO_flush(bio);
20 BIO_get_mem_ptr(bio, &bufferPtr);
21
22 char* result = (char*)malloc(bufferPtr->length + 1);
23 memcpy(result, bufferPtr->data, bufferPtr->length);
24 result[bufferPtr->length] = '\0';
25
26 BIO_free_all(bio);
27
28 return result;
29}
30
31char* decode_from_base64(const char* input) {
32 BIO *bio, *b64;
33 size_t length = strlen(input);
34 char* buffer = (char*)malloc(length);
35
36 b64 = BIO_new(BIO_f_base64());
37 bio = BIO_new_mem_buf(input, -1);
38 bio = BIO_push(b64, bio);
39
40 BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
41 int decoded_length = BIO_read(bio, buffer, length);
42
43 if (decoded_length <= 0) {
44 free(buffer);
45 BIO_free_all(bio);
46 return NULL; // Invalid Base64 input
47 }
48
49 buffer[decoded_length] = '\0';
50
51 BIO_free_all(bio);
52
53 return buffer;
54}
55
56int main() {
57 const char* original_text = "Hello, World!";
58
59 char* encoded = encode_to_base64(original_text);
60 printf("Encoded: %s\n", encoded); // SGVsbG8sIFdvcmxkIQ==
61
62 char* decoded = decode_from_base64(encoded);
63 if (decoded) {
64 printf("Decoded: %s\n", decoded); // Hello, World!
65 free(decoded);
66 } else {
67 printf("Error: Invalid Base64 string\n");
68 }
69
70 free(encoded);
71
72 return 0;
73}
74
1// Rust Base64 Encoding/Decoding
2use base64::{encode, decode};
3use std::str;
4
5fn encode_to_base64(text: &str) -> String {
6 encode(text)
7}
8
9fn decode_from_base64(base64_string: &str) -> Result<String, String> {
10 match decode(base64_string) {
11 Ok(bytes) => {
12 match str::from_utf8(&bytes) {
13 Ok(text) => Ok(text.to_string()),
14 Err(e) => Err(format!("Invalid UTF-8 sequence: {}", e))
15 }
16 },
17 Err(e) => Err(format!("Invalid Base64 string: {}", e))
18 }
19}
20
21fn main() {
22 let original_text = "Hello, World!";
23 let encoded = encode_to_base64(original_text);
24 println!("Encoded: {}", encoded); // SGVsbG8sIFdvcmxkIQ==
25
26 match decode_from_base64(&encoded) {
27 Ok(decoded) => println!("Decoded: {}", decoded), // Hello, World!
28 Err(e) => println!("Error: {}", e)
29 }
30}
31
Edge Cases and Considerations
When working with Base64 encoding and decoding, be aware of these important considerations:
-
Unicode and Non-ASCII Characters: When encoding text with non-ASCII characters, ensure proper character encoding (usually UTF-8) before Base64 encoding.
-
Padding: Standard Base64 uses padding with "=" characters to ensure the output length is a multiple of 4. Some implementations allow omitting padding, which can cause compatibility issues.
-
Line Breaks: Traditional Base64 implementations insert line breaks (typically every 76 characters) for readability, but modern applications often omit these.
-
URL-Safe Base64: Standard Base64 uses "+" and "/" characters which have special meanings in URLs. For URL contexts, use URL-safe Base64 which replaces these with "-" and "_".
-
Whitespace: When decoding, some implementations are lenient and ignore whitespace, while others require exact input.
-
Size Increase: Base64 encoding increases the size of data by approximately 33% (4 output bytes for every 3 input bytes).
-
Performance: Base64 encoding/decoding can be computationally intensive for very large data. Consider streaming approaches for large files.
References
Feedback
Click the feedback toast to start giving feedback about this tool
Related Tools
Discover more tools that might be useful for your workflow