Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

Create Rail Fence Cipher.md

Abdul Wahab Junaid8bdfd12bac99
diff --git a/Cryptography/Cryptography Course/Course/Rail Fence Cipher.md b/Cryptography/Cryptography Course/Course/Rail Fence Cipher.md
new file mode 100644
index 0000000..d62c60f
--- /dev/null
+++ b/Cryptography/Cryptography Course/Course/Rail Fence Cipher.md
@@ -0,0 +1,120 @@
+### **Cryptography - Rail Fence Cipher**
+
+The **Rail Fence Cipher** is a form of **transposition cipher** in which the plaintext is written in a zigzag pattern across multiple "rails" (or rows), and then the ciphertext is obtained by reading the letters row by row. This cipher is called the **Rail Fence Cipher** because the zigzag pattern resembles a fence, and the plaintext is arranged in a way that resembles rails in a fence.
+
+#### **How Rail Fence Cipher Works:**
+
+1. **Plaintext Arrangement:** The plaintext message is written in a zigzag pattern, or "fence," with a number of rows (rails) defined by the key.
+2. **Reading the Rails:** Once the plaintext is written in the zigzag pattern, the ciphertext is created by reading each row from left to right.
+3. **Ciphertext Extraction:** The message is then extracted from the grid row by row to form the ciphertext.
+
+#### **Steps Involved:**
+1. Choose the number of **rails** (rows) for the cipher.
+2. Write the plaintext in a zigzag pattern across these rails.
+3. Read the rails in order, left to right, to form the ciphertext.
+
+### **Example of Rail Fence Cipher**
+
+Let’s encrypt the plaintext **"HELLO WORLD"** using **3 rails**.
+
+**Step 1: Write the plaintext in a zigzag pattern.**
+The message is written in a zigzag pattern across 3 rails:
+
+```
+Rail 1: H . . . O . . . R . .
+Rail 2: . E . L . W . L . D .
+Rail 3: . . L . . O . . . .
+```
+
+**Step 2: Read off the rails to create the ciphertext.**
+
+Reading row by row (from top to bottom):
+
+- Rail 1: **HOR**
+- Rail 2: **ELWL D**
+- Rail 3: **LO**
+
+The **ciphertext** is: **"HOR ELWL DLO"**
+
+---
+
+### **Python Code for Rail Fence Cipher**
+
+Here’s how you can implement the **Rail Fence Cipher** in Python:
+
+```python
+def rail_fence_encrypt(plaintext, rails):
+ # Create a matrix with 'rails' rows and columns equal to the length of the message
+ fence = [['' for _ in range(len(plaintext))] for _ in range(rails)]
+
+ # Variable to track the direction of writing (down or up)
+ row, step = 0, 1
+
+ for col in range(len(plaintext)):
+ fence[row][col] = plaintext[col]
+ if row == 0:
+ step = 1
+ elif row == rails - 1:
+ step = -1
+ row += step
+
+ # Read the matrix row by row to generate the ciphertext
+ ciphertext = ''.join([''.join(r) for r in fence])
+ return ciphertext
+
+def rail_fence_decrypt(ciphertext, rails):
+ # Calculate the number of columns and rows
+ num_columns = len(ciphertext) // rails
+ grid = [['' for _ in range(num_columns)] for _ in range(rails)]
+ index = 0
+
+ # Fill the grid with characters from the ciphertext
+ for row in range(rails):
+ for col in range(num_columns):
+ if grid[row][col] == '':
+ grid[row][col] = ciphertext[index]
+ index += 1
+
+ # Read the grid to decrypt the message
+ plaintext = ''.join([grid[row][col] for col in range(num_columns) for row in range(rails)])
+ return plaintext.strip()
+
+# Example usage
+plaintext = "HELLO WORLD"
+rails = 3
+ciphertext = rail_fence_encrypt(plaintext, rails)
+print(f"Ciphertext: {ciphertext}")
+
+decrypted_text = rail_fence_decrypt(ciphertext, rails)
+print(f"Decrypted Text: {decrypted_text}")
+```
+
+### **Explanation of the Code:**
+1. **`rail_fence_encrypt`**:
+ - We create a grid (a list of lists) with `rails` number of rows and the same number of columns as the length of the plaintext.
+ - We traverse through the plaintext and fill the grid by writing the message in a zigzag pattern (down when row is 0 and up when row is the last).
+ - Once the grid is filled, we concatenate the rows to form the ciphertext.
+
+2. **`rail_fence_decrypt`**:
+ - First, we calculate the number of rows and columns in the grid.
+ - Then, we fill the grid with the ciphertext characters.
+ - Finally, we read the grid in row-major order to get back the original message.
+
+### **Example Output:**
+
+```python
+Ciphertext: HOR ELWL DLO
+Decrypted Text: HELLO WORLD
+```
+
+---
+
+### **Key Points About the Rail Fence Cipher:**
+
+1. **Simplicity:** The Rail Fence Cipher is simple to implement and understand. However, it does not provide strong security by modern cryptographic standards.
+2. **Security:** It’s relatively easy to break with **frequency analysis** or other methods since it does not alter the characters in the message, only their positions.
+3. **Key Management:** The only key for the Rail Fence Cipher is the number of rails, so security is entirely based on keeping the number of rails secret.
+
+### **Conclusion:**
+
+The **Rail Fence Cipher** is a simple and historical encryption technique. While it is not secure by today’s standards, it serves as a basic introduction to the concepts of transposition and the importance of key management in cryptography.