# BASE64

## BASE64 Online Decode

### Description

Base64 encoding is a method of encoding binary data using a set of 64 characters, which are represented as 6 bits each. This allows binary data to be represented as a series of ASCII characters, which can be transmitted over text-based channels such as email or text messaging.

To understand the mathematical basis of Base64 encoding, we can first look at how binary data is typically represented. Binary data consists of a series of 0s and 1s, which can be grouped into bytes of 8 bits each. Each byte can represent a number between 0 and 255 (28 - 1).

In Base64 encoding, three bytes of binary data are grouped together and divided into four 6-bit chunks. These chunks are then represented as a series of 64 characters, which are chosen from a predefined set of characters.

The mathematical formula for converting a 3-byte sequence (B1B2B3) into four Base64 characters (C1C2C3C4) is as follows:

` C1 = (B1 >> 2) & 0x3f C2 = ((B1 & 0x3) << 4) | ((B2 >> 4) & 0xf) C3 = ((B2 & 0xf) << 2) | ((B3 >> 6) & 0x3) C4 = B3 & 0x3f 	`

In this formula, the ">>" operator represents a bitwise right shift, the "&" operator represents a bitwise AND, and the "|" operator represents a bitwise OR.

The first line of the formula takes the first 6 bits of the first byte (B1) by shifting the byte 2 bits to the right, and then performs a bitwise AND with the value 0x3f (which represents the binary number 111111). This ensures that the resulting value is within the range of valid Base64 characters.

The second line takes the last 2 bits of the first byte (B1), shifts them 4 bits to the left, and then ORs them with the first 4 bits of the second byte (B2) that have been shifted 4 bits to the right. This forms the second Base64 character.

The third line takes the last 4 bits of the second byte (B2), shifts them 2 bits to the left, and then ORs them with the first 2 bits of the third byte (B3) that have been shifted 6 bits to the right. This forms the third Base64 character.

The fourth line takes the last 6 bits of the third byte (B3) and performs a bitwise AND with the value 0x3f to ensure that the resulting value is within the range of valid Base64 characters. This forms the fourth and final Base64 character.

To decode a Base64 string back into binary data, the reverse process is performed using a similar mathematical formula.