base64 cipher Algorithm

The base64 cipher algorithm is a widely used encoding scheme that is designed to represent binary data in an ASCII string format. It is primarily used to transmit binary data, such as images or files, over text-based communication protocols like email or HTTP. The algorithm works by taking every three bytes of binary data and converting them into four ASCII characters, resulting in a 33% increase in size. This increase in size is considered an acceptable trade-off for the ability to transmit binary data over text-based channels that might otherwise be corrupted or unreadable. The base64 encoding process involves dividing the input binary data into 6-bit blocks, then using a 64-character lookup table to map each 6-bit block to its corresponding ASCII character. The lookup table consists of the characters A-Z, a-z, 0-9, +, and /, as well as padding characters, which are usually represented by the equals sign (=). Padding is necessary to ensure that the output string has a consistent length, even if the input data is not evenly divisible by three bytes. The resulting encoded string is safe for transmission over text-based channels and can be easily decoded back into the original binary data by reversing the process, using the same lookup table to convert each character back into its corresponding 6-bit block and then concatenating those blocks to recreate the original binary data.
def encode_base64(text):
    r"""
    >>> encode_base64('WELCOME to base64 encoding 😁')
    'V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ=='
    >>> encode_base64('AΓ…αƒπ€πŸ€“')
    'QcOF4ZCD8JCAj/CfpJM='
    >>> encode_base64('A'*60)
    'QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB'
    """
    base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

    byte_text = bytes(text, "utf-8")  # put text in bytes for unicode support
    r = ""  # the result
    c = -len(byte_text) % 3  # the length of padding
    p = "=" * c  # the padding
    s = byte_text + b"\x00" * c  # the text to encode

    i = 0
    while i < len(s):
        if i > 0 and ((i / 3 * 4) % 76) == 0:
            r = r + "\r\n"  # for unix newline, put "\n"

        n = (s[i] << 16) + (s[i + 1] << 8) + s[i + 2]

        n1 = (n >> 18) & 63
        n2 = (n >> 12) & 63
        n3 = (n >> 6) & 63
        n4 = n & 63

        r += base64_chars[n1] + base64_chars[n2] + base64_chars[n3] + base64_chars[n4]
        i += 3

    return r[0 : len(r) - len(p)] + p


def decode_base64(text):
    r"""
    >>> decode_base64('V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==')
    'WELCOME to base64 encoding 😁'
    >>> decode_base64('QcOF4ZCD8JCAj/CfpJM=')
    'AΓ…αƒπ€πŸ€“'
    >>> decode_base64("QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\r\nQUFB")
    'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'
    """
    base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    s = ""

    for i in text:
        if i in base64_chars:
            s += i
            c = ""
        else:
            if i == "=":
                c += "="

    p = ""
    if c == "=":
        p = "A"
    else:
        if c == "==":
            p = "AA"

    r = b""
    s = s + p

    i = 0
    while i < len(s):
        n = (
            (base64_chars.index(s[i]) << 18)
            + (base64_chars.index(s[i + 1]) << 12)
            + (base64_chars.index(s[i + 2]) << 6)
            + base64_chars.index(s[i + 3])
        )

        r += bytes([(n >> 16) & 255]) + bytes([(n >> 8) & 255]) + bytes([n & 255])

        i += 4

    return str(r[0 : len(r) - len(p)], "utf-8")


def main():
    print(encode_base64("WELCOME to base64 encoding 😁"))
    print(decode_base64(encode_base64("WELCOME to base64 encoding 😁")))


if __name__ == "__main__":
    main()

LANGUAGE:

DARK MODE: