Cryptography - Base64 Cipher

1 - About

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

3 - Decoder / Encoder


<p>Enter a <a href="/plaintext">plaintext</a></p>
<textarea id="plaintext" cols=50 rows=3>Hello World !</textarea>


<p>The Base 64 <a href="/ciphertext">CipherText</a> is:</p>
<textarea id="ciphertext" cols=50 rows=3>CipherText</textarea>
<br>
<button onclick="encode()">Encode</button>
<button onclick="decode()">Decode</button>
<div >


let encode = function (){
    let plaintext = document.getElementById("plaintext").value;
    // btoa need a binary string
    // https://developer.mozilla.org/en-US/docs/Web/API/DOMString/Binary
    // invoking it on a string that contains codepoints greater than 255 will cause a Character Out Of Range error.
    let cipher = window.btoa(plaintext);
    document.getElementById("ciphertext").value = cipher;
}

let decode = function (){
    let cipher = document.getElementById("ciphertext").value;
    let plaintext = window.atob(cipher);
    document.getElementById("plaintext").value = plaintext ;
}

encode();

See also the Google encoder/decoder

4 - Usage

  • datastore accept only ASCII string format
  • to have a portable version of the data between system (get rid of EOL difference for instance).
  • having data encoded on only 64 characters (ie without the point for instance)

5 - Steps

5.1 - 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

6 - Syntax


base64Conversion=

where:

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

7 - Alphabet

The alphabet is the conversion table between:

base64url (base64 for URL) replaces:
  • the + with -
  • and / with _

because + and / are characters that have meaning 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 / _

8 - Management

8.1 - Javascript

8.2 - base64 utility


cat my-file | base64

8.3 - Open Ssl

Openssl


echo Hello World ! | openssl base64


SGVsbG8gV29ybGQgIQo=

8.4 - Java


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


Nico

9 - Documentation / Reference

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

Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap