Table of Contents

What is Base64? (Cryptography Cipher)

About

Base64 is a cipher (without key and reversible) that converts (encode) a data (message) in 64 characters of the ASCII string.

This encoding doesn't protect the value from anything other than an over-the-shoulder glance.

Decoder / Encoder

See also the Google encoder/decoder

Usage

Example: base64 is used in the basic authentication scheme to encode the value.

Steps

Encode

A text message with only string

Decode

The decode steps are the same as the encode steps but in reverse. You need to use the encoded character as input.

Syntax

A base64 value has the following format:

base64Conversion=

where:

Alphabet

The alphabet is the conversion table between binary data (base 2) and its equivalent:

base64url (base64 for URL) replaces:

because + and / are reserved characters in a URL

Binary Number Base 64 Number Character Base64 Character Base64Url
0000 0 A A
0001 1 B B
0010 2 C C
0011 3 D D
0100 4 E E
0101 5 F F
0110 6 G G
0111 7 H H
1000 8 I I
1001 9 J J
1010 10 K K
11 L L
12 M M
13 N N
14 O O
15 P P
16 Q Q
17 R R
18 S S
19 T T
20 U U
21 V V
22 W W
23 X X
24 Y Y
25 Z Z
26 a a
27 b b
28 c c
29 d d
30 e e
31 f f
32 g g
33 h h
34 i i
35 j j
36 k k
37 l l
38 m m
39 n n
40 o o
41 p p
42 q q
43 r r
44 s s
45 t t
46 u u
47 v v
48 w w
49 x x
50 y y
51 z z
52 0 0
53 1 1
54 2 2
55 3 3
56 4 4
57 5 5
58 6 6
59 7 7
60 8 8
61 9 9
62 + -
0011 1111 63 / _

Management

Javascript Node

const base64 =  Buffer.from('Hello Base 64').toString('base64');

atob / btoa javascript function are deprecated.

Javascript

const base64 = window.btoa("Hello World");
console.log(`Base64: ${base64}`);
const plainText = window.atob(base64);
console.log(`Plain Text: ${plainText}`);

To decode a base64 URL encoding:

function base64Decode(s){
  // from base64 url to base 64
  s = s
       .replace(/-/g, '+')
       .replace(/_/g, '/');
  return window.atob(s);
}

See the decoder/encoder script above.

Ref:

base64 command utility

cat my-file | base64

Open Ssl

Openssl

echo Hello World ! | openssl base64
SGVsbG8gV29ybGQgIQo=

Java

String encode = Base64.getEncoder().encodeToString("Foo".getBytes());
byte[] decode = Base64.getDecoder().decode(encode);
System.out.println(new String(decode,StandardCharsets.UTF_8));
Foo

Documentation / Reference