Basic Cryptography

Abda
K3RN3L4RMY
Published in
4 min readMar 9, 2021

We all seem to know basic cryptography, but in reality, just a few of us really knows what's happening behind the scenes and how it works. In this blog, I will try to explain in the simplest and easiest way possible how the basic ciphers and encodings in cryptography work and what happens behind the scenes.

This is what we have on the menu today:

  • Caesar cipher
  • Vigenère cipher
  • Base64 encoding

So let's start with the most common one, Caesar cipher.

In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipher, Caesar’s code, or Caesar’s shift, is one of the simplest and most widely known encryption techniques.

How can we recognize it?

On CTFs, any reference to Caesar, emperor of Rome, or more generally to antiquity and the Roman Empire are clues. Also, the presence of keywords like Julius (Iulius/Ivlivs), Brutus, Augustus, or a(Caesar) salad can remind us of the Caesar imperator.

How does it work?

Caesar code decryption replaces a letter another with an inverse alphabet shift: a previous letter in the alphabet. For example, let's take the word: NDUPB with a shift of 3.

To decrypt N, take the alphabet and look at 3 letters before. K. So N is decrypted with K. To decrypt D do the same thing again. And we got A. To successfully decrypt the message and proceed to do the same process until we got the final form. So NDUPB decrypts to KARMY.

Vigenère cipher

How to recognize Vigenere ciphertext?

Following Vigenere encryption, the message has a coincidence index that decreases between 0.05 and 0.04 depending on the length of the key, it decreases towards 0.04 the longer the key is.

How does it work?

To decrypt Vigenere with a double-entry square table, use the following grid (case alphabet is ABCDEFGHIJKLMNOPQRSTUVWXYZ):

To decrypt NGMNI, the key is KEY.

Locates the first letter of the key in the left column, and locates on the row the first letter of the ciphered message. Then go up in the column to read the first letter, it is the corresponding plain letter.

Locate the letter K on the first column, and on the row of it, find the cell of the letter N, the name of its column is D, it is the first letter of the plain message.
Continue with the next letters of the message and the next letters of the key, when arrived at the end of the key, go back to the first key of the key.
The original plain text is DCODE.

Website to encode/decode Vigenere cipher: https://www.cs.du.edu/~snarayan/crypt/vigenere.html

Base64 encoding

How to recognize Base64?

Encoded data will always have the following characteristic:

  • The length of a Base64-encoded string is always a multiple of 4
  • Only these characters are used by the encryption: “A” to “Z”, “a” to “z”, “0” to “9”, “+” and “/”
  • The end of a string can be padded up to two times using the “=”-character (this character is allowed in the end only)

How does it work?

Base64 encoding breaks binary data into 6-bit segments of 3 full bytes and represents those as printable characters in ASCII standard. It does that in essentially two steps.

The first step is to break the binary string down into 6-bit blocks. Base64 only uses 6 bits (corresponding to 2⁶ = 64 characters) to ensure encoded data is printable and humanly readable. None of the special characters available in ASCII are used. Another step is explained in the section How to recognize Base64?

For example, take three ASCII numbers 155, 162, and 233. These three numbers constitute a binary stream of 100110111010001011101001. A binary file, like an image, contains a binary stream running for tens or hundreds of thousands of zeroes and ones.

A Base64 encoder starts by chunking the binary stream into groupings of six characters: 100110 111010 001011 101001. Each of these groupings translates into the numbers 38, 58, 11, and 41.

A six-character binary stream converts between binary (or base-2) to decimal (base-10) characters by squaring each value represented by a 1 in the binary sequence with its positional square. Starting from the right and moving left, and starting with zero, the values in the binary stream represent 2⁰, then 2¹, then 2², then 2³, then 2⁴, then 2⁵.

  • We can decrypt it using an online decoder, or if you want to feel a little bit more of a hacker you can use a Linux terminal for that as well using the following commands:
echo SzNSTjNMNFJNWQ== | base64 -d
  • Ech: This command writes a string to standard output, which in less technical terms means that it literally prints whatever you write onto the screen again.
  • SzNSTjNMNFJNWQ== : Encoded string
  • |: This is a pipe that tells the command that you want to redirect the output of the left-hand part into the right-hand part, essentially.
  • base64: This is a program that is in the coreutils (or GNU Core Utilities) a package that comes pre-packaged with your or Linux OS.
  • -d : A simple flag that stands for decode.

That would be it for this blog. I hope this blog has helped you better understand the basics of cryptography. So if someone asks you something about this you will no longer be ScriptKiddie and you will know what is going on behind the scenes and how it works, and you will look at these things from a different angle than before.

--

--