# Intro to Ciphers and Env Set up

## What is Cryptography?

Formally Defined as the art of message concealment.

It’s the art of turning a message into none-sense for someone who is trying to eves-drop

Informally Defined as the coolest category to learn.

## Common Terms

**Encryption**: Turning a message into none-sense

**Decryption**: Turning a none-sense into message

**Key**: A magic word or number that we use for encryption and/or decryption

**Plaintext**: Human readable text

**Ciphertext**: Non-human readable text

## Encryption vs Encoding vs Hashing

#### 1. **Encryption**

* **What it does**: Scrambles data to keep it secret.
* **How it works**: Takes readable data (plaintext) and turns it into unreadable text (ciphertext) using a key. Only someone with the right key can turn it back into the original message.
* **Why it's used**: To protect sensitive information, like passwords or messages, from being read by others.
* **Can it be undone?**: **Yes**, but only with the right key.
* **Example**: Locking a file with a password.

#### 2. **Encoding**

* **What it does**: Converts data into a different format to make it easier to handle or transfer.
* **How it works**: Changes data into a form that can be easily read by different systems, but it’s not meant to hide or protect it.
* **Why it's used**: To make data compatible with different devices or software, like when you encode a URL or a video.
* **Can it be undone?**: **Yes**, it's easily reversible.
* **Example**: Turning an image into a text format (Base64) so it can be sent over the internet.

#### 3. **Hashing**

* **What it does**: Creates a unique "fingerprint" for a piece of data.
* **How it works**: Takes some data and produces a fixed-length code (hash) that looks random. Even the tiniest change in the original data will create a completely different hash.
* **Why it's used**: To verify data hasn’t been changed, like when storing passwords securely or checking file integrity.
* **Can it be undone?**: **No**, it's designed to be a one-way process.
* **Example**: Storing a password as a hash so even if someone finds it, they can't turn it back into the original password.

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FBo1gpG0QshVX4RR4Ps2D%2Fimage.png?alt=media&#x26;token=32418e89-d658-4d2f-a158-2d578f0ea0d9" alt=""><figcaption></figcaption></figure>

| Plain         | Base64 Encoding      | Atbash Encryption | MD5 Hash                         |
| ------------- | -------------------- | ----------------- | -------------------------------- |
| Crypto is fun | Q3J5cHRvIGlzIGZ1bg== | Xibkgl rh ufm     | e6833e74f144196623f21b19e9efd1b3 |

## Some Ancient Ciphers

let’s play with some older (now easily breakable by modern computers) ciphers, before we do that, let’s look at one of the early ciphers used back in ancient times.

### Head Shaving Method

Kings used to shave a peasants head, write their message on his head and wait for his hair to grow back out, then they’d send the peasant to the receiving party.

Now we see how long and impractical it takes, it’s funny yet creative, we thought we’d mention it because why not 🤷🏽‍♂️.

***

Now to the real ciphers, the early practical ciphers that shaped our current cryptographic knowledge today.

There are two main categories for ancient ciphers

### Substitution Ciphers

A **substitution cipher** is a way of encrypting a message by replacing each letter in the original message with a different letter or symbol. Here's how it works:

1. You create a **rule** or **key** that tells you which letter will replace each letter of the alphabet.
   * For example, you might decide that **A** becomes **M**, **B** becomes **X**, and so on.
2. To encrypt a message, you replace every letter in your message with the letter from your rule.

For example, if your rule is: A → D, B → E, C → F, then the word "CAT" would become "FDW."

To decode the message, you just reverse the substitution by using the key again.

Substitution ciphers are more complex than Caesar's Cipher because each letter can be replaced with a completely different one, making it harder to guess.

So basically, you ***Substitute*** each letter in the plaintext with something else according to a set of rules, those rules can be represented by a ***substitution table***

#### Caesar Cipher

Caesar's Cipher is a simple way to encrypt messages by shifting the letters of the alphabet. Here's how it works:

1. Pick a **number** as your "shift" (let's say 3).
2. Replace each letter in your message with the letter that is **3 places** down the alphabet. For example:
   * A becomes D,
   * B becomes E,
   * C becomes F,
   * and so on.
3. If you reach the end of the alphabet, it "wraps around" to the beginning. So, Z would become C with a shift of 3.

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FZRvHJTrSIoMPLg22BX6I%2Fimage.png?alt=media&#x26;token=6b0340c5-a631-40d9-bd65-3a67ca7fa432" alt=""><figcaption></figcaption></figure>

For example, if we use a shift of 3, the word "HELLO" becomes "KHOOR."

To decode the message, you just shift the letters **back** by the same number.

It's called "Caesar's Cipher" because it was famously used by Julius Caesar to send secret messages.

**Substitution Table for Caesar Cipher where Shift = 3:**

| A | B | C | D | E | F | G | H | I | G | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
| - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - | - |
| D | E | F | G | H | I | G | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C |

#### Simple Substitution

A **simple substitution cipher** is a type of cipher where each letter in the plaintext (original message) is replaced with another unique letter. The key is a shuffled version of the alphabet, where each letter is mapped to a different letter, but the order of substitution can vary.

For example, a possible substitution rule could be:

* A → M
* B → Q
* C → W
* and so on for all letters.

Using this rule, the word "CAT" might become "WMR."

#### Relationship Between Caesar Cipher and Simple Substitution:

* **Caesar Cipher** is a special type of simple substitution cipher. The main difference is that in Caesar Cipher, the alphabet is shifted by a set number of places (like a shift of 3), so the substitution follows a fixed pattern (A → D, B → E, etc.).
* In a **simple substitution cipher**, the letter mapping doesn't follow any fixed shift. Instead, the mapping can be random, and every letter has a unique, shuffled substitute.

Both techniques are forms of substitution, but Caesar's Cipher is more structured, while simple substitution allows more flexibility in the letter replacement.

#### Cracking Method

Okay so let’s talk about cracking substitution ciphers in general, how can we break them? Depends on the cipher!

Let’s say we have a **Caesar cipher**, how can we break it without knowing the shift value? in order to answer that question we need to answer another question, how many possible shift values are there?

For the English alphabet, if we keep shifting and shifting we’ll eventually get back to the same alphabet after 26 shifts, meaning we have 26 possible keys!

It’s not hard to try decrypting with 26 keys with modern computers.

So we can **brute-force** the keys and find the plaintext when one of the decryptions provides a human readable text.

***

How about simple substitution?

Frequency analysis is a powerful technique for cracking ciphers (codes) and was invented by **Al-Kindi**, a 9th-century Arab mathematician and polymath. Al-Kindi is often called the "father of cryptanalysis" because he was the first to document this method in his work "Manuscript on Deciphering Cryptographic Messages."

#### How Frequency Analysis Was Invented

Al-Kindi realized that in any given language, certain letters appear more frequently than others. For example, in English, letters like "E," "T," and "A" show up a lot, while letters like "Z" and "Q" are rare. He noticed that even when a message is encrypted (scrambled using a cipher like a substitution cipher), the frequency of the letters doesn't change — it's just the letters themselves that are swapped.

This observation gave him a way to attack ciphers. By studying the frequency of letters in the encrypted message and comparing them to the usual frequency of letters in the language, he could start guessing which letters in the ciphertext corresponded to which letters in the original text.

#### How Frequency Analysis Works (In Simple Terms)

1. **Substitution Cipher Example**: Imagine you're using a substitution cipher, where each letter in your message is replaced by a different letter. For example, A might be replaced with F, B with X, and so on.
   * **Original message**: HELLO
   * **Encrypted message**: QFRRA (where H → Q, E → F, L → R, O → A)
2. **Letter Frequencies**: In English, some letters appear much more often than others. For example:
   * E is the most common letter, making up about 13% of any text.
   * T, A, O, I, N are also common.
   * J, Q, Z are rare.
3. **Step-by-Step Process**:
   * You start by counting how often each letter appears in the encrypted message. For example, if Q appears a lot, it might correspond to E in the original message.
   * You then make educated guesses based on these frequencies, replacing the most frequent letters in the ciphertext with the most frequent letters in the language you're decoding.
   * As you replace letters, parts of words start becoming recognizable, and this helps you figure out more letters and crack the rest of the code.

#### Example of Cracking a Cipher:

Let’s say you intercept this encrypted message:

```
ZQDDI ZHOZD XQQR
```

You notice that "Z" appears frequently, and you know that "E" is the most common letter in English. You might guess that Z = E. Then, you continue making similar guesses for other frequent letters and test your results by looking for common words or patterns like "THE," "AND," or "IS."

#### Why Is It So Effective?

The reason frequency analysis works well is that even though the letters are scrambled, the relative frequency of each letter stays roughly the same. In long messages, this pattern becomes obvious and allows you to start making good guesses, which snowball into cracking the whole cipher.

### Transposition Ciphers

A **transposition cipher** is a way of encrypting messages by simply rearranging the letters of the original message. Imagine taking the letters of a sentence and shuffling them around based on a certain pattern or rule, without changing the letters themselves. The secret to decoding the message is knowing how the letters were rearranged.

Here’s an example:

1. **Original message**: "HELLO"
2. You rearrange (transpose) the letters: "LEHLO"

In this case, the letters have been moved around, but they haven’t been changed to other letters.

To decrypt the message, you would just need to know how the letters were shuffled so you can reverse the process and get the original message back. The important thing about a transposition cipher is that **the letters stay the same, but their positions are swapped**.

#### Scytale

The **Scytale cipher** is a very simple, ancient method of encryption that was used by the Greeks and Spartans. It works by wrapping a strip of paper (or leather) around a stick (called a scytale) of a certain thickness. The message is written across the wrapped strip in straight lines, and when the paper is unwrapped, the letters are scrambled and look like a jumbled message.

Heres how it works in simple terms:

1. You have a **stick** of a certain width.
2. You take a **strip of paper** and wrap it tightly around the stick.
3. You write your message along the length of the stick so that each letter is on a different line.
4. When you **unwrap** the strip of paper, the letters are scrambled.

For example:

* You write the message "HELLO" on the stick like this:

```mathematica
H E
L L
O

```

* When you unwrap it, the letters appear in a different order, and it looks like a jumble: "HLELO."

To **decode** it, the person on the other end needs a stick of the **exact same thickness**. They wrap the strip of paper around their stick and the original message will reappear.

In summary, the Scytale cipher is a type of **transposition cipher** where the letters are scrambled based on how they are written around a stick.

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FFrPrDNj2dQkC0O0SArtX%2Fimage.png?alt=media&#x26;token=318826a5-33d4-4685-9874-1fe121afeb5e" alt=""><figcaption></figcaption></figure>

Now you can see how this is very impractical and insecure for our modern systems, giving our attacker our shuffled message does nothing but bring them joy!

## Setting up your Crypto Environment

Let's talk about setting up the environment we'll be using for the rest of our training. Since the focus is on crypto, you'll primarily be working with Python.

There are various ways to set up a Python environment, so if you already have one and feel comfortable using it, feel free to stick with that. However, if you're new to this or looking for a fresh setup, we'll be sharing our recommended configuration to help you get started easily.

### Installing Stuff

1. **Installing VSCode**: \*\*\*\*Go [here](https://code.visualstudio.com/download) and download a suitable version for you OS
2. **Installing Python**: Go [here](https://www.python.org/downloads/) and download a suitable version for you OS

### Set-up

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FhBw3Bl1EyQoJrNJKQdkp%2Fimage.png?alt=media&#x26;token=6ab24399-067d-4873-aff5-30ec24cdc4c8" alt=""><figcaption></figcaption></figure>

1. Go to the extensions tab highlighted in the picture above.

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FXOKJxPaUm2jfdyrMw3ok%2Fimage.png?alt=media&#x26;token=c24fa8e0-8972-4d68-a6cc-8ce6f0faecf7" alt=""><figcaption></figcaption></figure>

2. Search for “Python” and install the first 2 extensions.

<figure><img src="https://427275427-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FHsQs1QxfvG7fIv03WKVZ%2Fuploads%2FSZe4zIOQcAqtbA5CBmJ9%2Fimage.png?alt=media&#x26;token=c77d44d8-65c1-465e-91ef-0faf9b6f6c5b" alt=""><figcaption></figcaption></figure>

3. Go to the terminal in VSCode and run the following commands
   1. `pip install pycryptodome`
   2. `pip install sympy`
   3. `pip install gmpy2`

if `pip` doesn’t work use `pip3`.
