ASCII, base64, and XOR

What is it?  ASCII is a modern cryptographic encryption scheme that serves as a character encoding standard for electronic communication. It is analogous to 8-bit binary and can be compressed using base64 encryption. The following table shows how ASCII hexadecimal and decimal all code for specific characters:

To learn about how base64 encryption works, check out this video. Every character shown in the table above can also be represented as 8-bit binary. For example:


Decimal: 65 | 66 | 67

8-bit binary: 01000001 | 01000010 | 01000011 --> C: 010000010100001001000011

Note that it is necessary that leading 0s are added so that every character is represented by exactly 8 bits.

This 8-bit binary translation is considered in the XOR cipher. The XOR logic gate has the following rules:

1 + 0 --> 1      0 + 1 --> 1         1 + 1 --> 0         0 + 0 --> 0

Based on this, we can demonstrate a sample XOR encryption. The key will be a random string of same length as the plaintext:


                                           8-bit binary       (This is where the XOR is happening)

                   P: ABC     ------------------------>     01000001 | 01000010 | 01000011     

               Key: 1+1     ------------------------>     00110001 |  00101011 | 00110001     

                                                                       01110000 | 01101001 | 01110010

                                                                             p                     i                  r

                 C: pir

Notice that though in this case, the ciphertext could easily be represented in terms of characters, sometimes, this can lead to data corruption. For example, what if one of my ciphertext characters is equal to the null character '00000000'? This is not printable. It is best to leave it in binary form or convert it to hex or base64. Additionally, since the rules for an XOR logic gate remain the same regardless of encryption or decryption, encryption is the same as decryption. If A XOR B = C, then C XOR B = A.

A single-byte XOR cipher is an XOR cipher in which the key consists of a single repeating character. For example, if I wanted to encrypt the message 'ABC' using a single-byte XOR cipher with a key of '2', then I would compute 'ABC' XOR '222' and get 'spq' as my ciphertext.

How do I recognize it?   Generally, the output of an XOR cipher will be given as either a hex-encoded string or a base-64 encoded string. If converting a hex-encoded string to ASCII results in gibberish, try a single-byte XOR on the string with every possible byte (brute-force method). You can use an online tool or a Python program -- see Tools. If there are '=' at the end of a message, then the string has most almost definitely been base64 encrypted, and you can use an online tool to convert it to ASCII. Below are sample frequencies for a base64 encrypted string:

How do I break it?  Try everything. If you are given hexadecimal or base64, then you are almost certainly working with a modern cryptographic scheme. If you are given binary, check the length of the message and check to see that it is divisible by 8. If that is the case, then you might be working in the ASCII system. Run a blocks analysis on the 8-grams and check to see if it could just be a simple substitution cipher. Technically, if the message has been XORed against a single byte, and it consists of only [A-Z], it can just be solved as a simple 8-gram substitution cipher (see fractionating ciphers). The XOR cipher is, therefore, much more secure if punctuation and case-sensitivity are maintained in the message. If nothing works, remember to always try reversing the message.

Takeaway: ASCII characters can be represented as 8-bit binary, hexadecimal, decimal, or even octal. They can be compressed using base64 and encrypted using the single-byte XOR cipher.

© 2020 by The Gunn Cryptology Club

Website created and managed by Arush Chhatrapati and Aaryan Agrawal