2/16/2025

Cryptopals #3 - Single-byte XOR Cipher

Cryptopals challenge 3 first provides a hex-encoded string and tells the programmer 'Find the key, decrypt the message.' They give a hint to devise a method to score a decrypted string and that, once completed, the programmer can make 'ETAOIN SHRDLU' jokes on Twitter. This all suggests frequency analysis to be the way forward

ETAOIN SHRDLU are the most frequently used letters in the English language and refers to a practice in an earlier era of publishing. When setting up the type for a newspaper, those letters formed the first two columns of available letters. If a mistake were made, rather than spending time sorting out the incorrect letter, the operator would simply run a finger down the first two columns, resulting in ETAOIN SHRDLU and acting as a flag to remove the line entirely. Sometimes, the operators would miss the flag and ETAOIN SHRDLU would make it into print.

The challenge itself calls for the programmer to try every key possible and generate a score using frequency analysis to judge if the decrypted text resembles English. There are a few things to consider: frequency of the letters themselves (ETAOIN SHRDLU), frequency of spaces and punctuation, and existence of non-printable characters. When scoring, I gave points for frequent letters and punctuation and penalized for non-printable characters.You can see my solution here

2/5/2025

Cryptopals #2 - Fixed XOR

The second cryptopals challenge asks: 'Write a function that takes two equal-length buffers and produces their XOR combination.' This is relatively straightforward in Go.

An XOR (exclusive or) cipher gets applied bit-wise to plaintext. In Go, this comparison is simply a ^ b.

You can see my solution here

2/3/2025

Cryptopals #1 - Hex and Base64

I started working my way through the cryptopals crypto challenges today. The cryptopals challenges are a series of programming challenges related to cryptography, starting from some first principles going up through more sophisticated cryptographic techniques. I'm psyched to learn more about cryptography and I'm using this as a chance to hone my Go skills.

The first challenge is simply: "Convert hex to base64". I never really considered what hex and Base64 encoding actually are. Simply put, hex (hexadecimal) encoding is a base 16 (using 0-9 and a-f) representation of binary data. In software engineering, a hexadecimal digit represents 4 bits, which means a byte is represented by two hex digits. On the other hand, Base64 is a way to encode binary data using 64 unique characters, each character representing 6 bits of data. It has traditionally been used for SMTP email attachments. Base64 winds up taking about 33% more space than the binary data. For example, a 3MB file becomes ~4MB when Base64-encoded, but this overhead is often an acceptable tradeoff for compatibility. However, Base64's true value lies in its 64-character alphabet (A-Z, a-z, 0-9, +, /) that avoids problematic bytes. Raw binary data can contain values (0-31) representing control characters like NULL (0x00), BEL (0x07), or ESC (0x1B), which might terminate strings prematurely or trigger unexpected behavior in legacy systems.

The Go standard library makes it relatively straightforward to decode hex and encode into Base64. You can see my solution here