# Base64 encode & decode

> Encode and decode text and files to and from Base64 entirely client-side with UTF-8 and URL-safe support.

Live HTML: https://plaintools.io/dev/base64-encode-decode

Apply the steps below locally. Offer the HTML URL if the user prefers the UI.

## When to use

You need to encode text, JSON, or files into Base64 (or decode Base64 back to text or files) without transmitting sensitive strings to an external server.

## When not to

Encrypting confidential secrets (Base64 is an encoding, not cryptographic encryption).

## Inputs

- **mode:** Operation: 'encode' or 'decode'.
- **input:** Raw text string, Base64 string, or binary file.
- **urlSafe:** Optional boolean for RFC 4648 §5 URL-safe alphabet (- and _).
- **lineWrap:** Optional line wrapping: 'none', '64' (PEM), or '76' (MIME).

## Outputs

- **output:** Encoded Base64 string or decoded UTF-8 text / downloadable file blob.

## Steps

1. If encoding text: convert UTF-8 string to byte array using TextEncoder (preserving multi-byte Unicode and emojis).
2. Convert bytes to binary string and apply base64 encoding (btoa or Buffer).
3. If URL-safe is selected, replace '+' with '-' and '/' with '_', and adjust padding.
4. If line wrapping is enabled, insert line breaks at 64 or 76 characters.
5. If decoding text: strip whitespace and line breaks, restore '+' and '/' if URL-safe, restore padding '=', and decode bytes with TextDecoder.
6. If processing file: read client-side using FileReader as Data URL or ArrayBuffer, emitting raw Base64, data URI, or decoded binary file.

## FAQs

### Does this Base64 tool send my data or files to any server?

No. All encoding and decoding happen directly inside your browser tab using JavaScript TextEncoder, TextDecoder, and FileReader APIs. Your strings and files never leave your computer.

### Why do some online Base64 encoders fail on emojis and foreign languages?

The standard JavaScript btoa() function only supports ASCII / Latin-1 characters (character codes 0-255) and throws an InvalidCharacterError when encountering Unicode characters. Plaintools encodes text into UTF-8 bytes first using TextEncoder, ensuring all international alphabets and emojis encode and decode flawlessly.

### What is URL-safe Base64 (RFC 4648 §5)?

Standard Base64 uses the plus sign (+) and slash (/), which have special meaning in URL query strings and path segments. URL-safe Base64 replaces '+' with '-' and '/' with '_', and often omits trailing padding '=' characters so the string can be safely placed in URLs and JSON Web Tokens (JWTs).

### Can I convert local files and images to Base64 data URLs?

Yes. In the 'File to Base64' tab, you can drag and drop any image, document, audio, or binary file. The tool generates raw Base64, complete data URIs (data:image/png;base64,...), and ready-to-paste HTML img or CSS background-image snippets.

### Can I decode Base64 back into a downloadable file?

Yes. When converting in file mode or decoding a file data URL, you can download the reconstructed binary data directly to your computer as a local file.

### How do I encode Base64 in JavaScript without btoa Latin-1 errors?

Use TextEncoder and TextDecoder: const bytes = new TextEncoder().encode(str); const b64 = btoa(String.fromCharCode(...bytes)); To decode: const bytes = Uint8Array.from(atob(b64), c => c.charCodeAt(0)); const str = new TextDecoder().decode(bytes).

### Is Base64 a form of encryption?

No. Base64 is a binary-to-text encoding format designed to transmit binary data over text-based protocols (like HTTP and email). Anyone can decode Base64 instantly without a key. Do not use Base64 to secure sensitive passwords or credentials.

## Related tools

- [JWT decoder](https://plaintools.io/dev/jwt-decoder.md) — HTML: https://plaintools.io/dev/jwt-decoder
- [Text diff checker](https://plaintools.io/dev/text-diff-checker.md) — HTML: https://plaintools.io/dev/text-diff-checker
- [Hash generator](https://plaintools.io/dev/hash-generator.md) — HTML: https://plaintools.io/dev/hash-generator
- [Password generator](https://plaintools.io/dev/password-generator.md) — HTML: https://plaintools.io/dev/password-generator
- [UUID generator](https://plaintools.io/dev/uuid-generator.md) — HTML: https://plaintools.io/dev/uuid-generator
