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

  • for datastore that accepts only ASCII string format
  • to have a portable version of the data between systems. ie get rid of:
  • having data encoded on only 64 characters (ie without the point for instance)

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

Steps

Encode

A text message with only string

  • Transform the text into binary data by applying the character set table conversion
  • Transform the binary into a succession of characters with the alphabet conversion table
  • add the optional trailing character =
  • and you get the base64 cipher

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:

  • base64Conversion is the conversion when the alphabet conversion is applied
  • = is one or several optional trailing character

Alphabet

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

base64url (base64 for URL) replaces:

  • the + with -
  • and / with _

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

  • rfc4648 - The Base16, Base32, and Base64 Data Encodings





Discover More
Card Puncher Data Processing
Ant - Mail

How to send a mail with Ant. The jar file mail.jar () must be in the lib dir of ant. where: the user and the password must be in clear text...
Authentication - HTTP Digest Access Authentication

Digest access authentication is an http authentication method based on authorization entry. It is intended (as a security trade-off) to replace unencrypted HTTP basic access authentication. It is not,...
Cpu Moore Law Transistor
Bit - Binary Data (Structure)

Binary Data is a computer file that contains binary data (0 or 1) Binary data may be described: at the bit level (base 2) at the byte level (base 2 - 8 bit) at the hexadecimal level (base 16)...
Cryptography - Algorithm (called a Cipher)

A cipher is an algorithm that encrypt a plain text message into a ciphertext. Cipher algorithm can be categorized by the key that they used as parameters. No key symmetric one (using one key) ...
Cryptography - Encryption (Enciphering)

Encryption is the function of a cipher that transforms usable data into an unreadable form. It provides confidentiality if a secret is used in the encryption. There is two class of encryption. ...
Cryptography - Key

A key is a parameter used in a cipher algorithm that determines: the encryption operation (forward) and the decryption operation (backward). It's the only secret parameter that protect the anonymity...
Cryptography - PKCS (Public Key Cryptography Standards)

PKCS #X (Public Key Cryptography Standards) are a group of public-key cryptography standards devised and published by RSA Security Inc, starting in the early 1990s. They defined the file format of key...
Cryptography - PKCS12 (PFX files)

PKCS12 is a pkcs version 12. PKCS12 (ie p12 extension) is intended to store both: the private key and public certificate parts It has the capability of being password protected to provide some...
Certificate Validity Period Not Before Not After Portecle
Cryptography Certificate - How to self-signed a Certificate (for a test or internal server)

When a certificate is used to sign itself, it is called a self signed certificate. All root CA certificates of the certificate chain are self signed. This article shows you how to create a self-signed...
Distinguished Encoding Rules (DER)

DER Distinguished Encoding Rules is a key file format for cryptographic data. It's the binary ASN.1 encoding of the data (not in plain text otherwise it's a PEM. pem is the base64 encoded version of...



Share this page:
Follow us:
Task Runner