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.
JavaScript algorithmsartificial-intelligencebugbountycryptographycyber-threat-intelligencecybersecurity-projectsethical-hackinghacking-toolsmalwarenessusnetwork-monitoringnetwork-securitynmapportscannerpythonpythonprojectsthreat-intelligencevirus-scanning
Create Monoalphabetic Cipher in Cryptography.md
Abdul Wahab Junaid373ba4ba92f6
diff --git a/Cryptography/Cryptography Course/Course/Monoalphabetic Cipher in Cryptography.md b/Cryptography/Cryptography Course/Course/Monoalphabetic Cipher in Cryptography.mdnew file mode 100644index 0000000..70852ed--- /dev/null+++ b/Cryptography/Cryptography Course/Course/Monoalphabetic Cipher in Cryptography.md@@ -0,0 +1,132 @@+### **Cryptography - Monoalphabetic Cipher**++A **Monoalphabetic Cipher** is a type of substitution cipher where each letter of the plaintext is replaced by a corresponding letter in the ciphertext alphabet. The key in a monoalphabetic cipher is simply the alphabet mapping, where each letter of the plaintext is substituted with a unique letter of the ciphertext.++### **How Monoalphabetic Cipher Works**++In a monoalphabetic cipher, a fixed substitution rule is used to transform each letter of the plaintext into the corresponding letter of the ciphertext. The key defines the mapping between the plaintext alphabet and the ciphertext alphabet.++For example:+- **Plaintext Alphabet**: `ABCDEFGHIJKLMNOPQRSTUVWXYZ`+- **Ciphertext Alphabet**: `QWERTYUIOPASDFGHJKLZXCVBNM`++The letter **A** from the plaintext will be replaced by **Q** in the ciphertext, **B** will be replaced by **W**, and so on.++### **Encryption Process**+To encrypt a message using a monoalphabetic cipher:+1. Select a key (a shuffled alphabet or a random permutation of the original alphabet).+2. Replace each letter of the plaintext with its corresponding letter from the ciphertext alphabet.++### **Example:**+Let’s take an example where we encrypt the plaintext message **"HELLO"** using a simple monoalphabetic cipher with a key.++- **Plaintext**: `"HELLO"`+- **Key** (Ciphertext Alphabet): `"QWERTYUIOPASDFGHJKLZXCVBNM"`++Here, we replace each letter of the plaintext with the corresponding letter from the ciphertext alphabet:+- `"H"` → `"P"`+- `"E"` → `"Y"`+- `"L"` → `"R"`+- `"L"` → `"R"`+- `"O"` → `"F"`++So, the **ciphertext** becomes `"PYRRF"`.++### **Decryption Process**+To decrypt the ciphertext, we need to reverse the substitution process. Given the same key (the mapping between plaintext and ciphertext alphabets), we replace each letter of the ciphertext with the corresponding letter in the plaintext alphabet.++- **Ciphertext**: `"PYRRF"`+- **Key** (Ciphertext Alphabet): `"QWERTYUIOPASDFGHJKLZXCVBNM"`++Reversing the encryption:+- `"P"` → `"H"`+- `"Y"` → `"E"`+- `"R"` → `"L"`+- `"R"` → `"L"`+- `"F"` → `"O"`++Thus, the **plaintext** becomes `"HELLO"`.++### **Security of Monoalphabetic Cipher**+Monoalphabetic ciphers are considered **weak** because they can be easily broken using **frequency analysis**. In any given language, certain letters (like 'E', 'T', 'A', etc.) appear more frequently than others. By analyzing the frequency of letters in the ciphertext and comparing them to typical letter frequencies in the language, a cryptanalyst can easily guess the substitution pattern and break the cipher.++For example, in English:+- The letter **E** is the most frequent letter.+- The letter **T** comes next, followed by **A**, **O**, and so on.++If an attacker knows these frequency patterns, they can start making educated guesses about the substitutions in the cipher.++### **Example of Frequency Analysis**++Let’s say we have the ciphertext:++```+VXWRL VQ OIYIQ+```++We might notice that the letter **V** occurs frequently, and since **E** is the most common letter in the English language, it’s likely that **V** corresponds to **E**. Similarly, we can try to map other common letters and break the cipher.++### **Advantages of Monoalphabetic Cipher**+1. **Simplicity**: Monoalphabetic ciphers are very simple to implement and understand.+2. **Speed**: The encryption and decryption processes are fast.++### **Disadvantages of Monoalphabetic Cipher**+1. **Weak Security**: As mentioned, the cipher can be easily cracked using frequency analysis, making it unsuitable for modern encryption needs.+2. **No Diffusion**: A single letter in the plaintext will always map to the same letter in the ciphertext, meaning the structure of the message is preserved, which aids cryptanalysis.++### **Improving the Security of Monoalphabetic Ciphers**+To make the monoalphabetic cipher more secure, a **polyalphabetic cipher** (such as the **Vigenère cipher**) can be used. The key in polyalphabetic ciphers is not a single alphabet but a sequence of different alphabets, making frequency analysis much more difficult.++### **Implementation of Monoalphabetic Cipher in Python**++Here’s a simple Python implementation for monoalphabetic cipher encryption and decryption.++#### **1. Monoalphabetic Cipher Encryption**:++```python+def monoalphabetic_encrypt(plaintext, key):+ alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ # Create a dictionary for the cipher mapping+ cipher_dict = {alphabet[i]: key[i] for i in range(len(alphabet))}++ # Encrypt the plaintext+ ciphertext = ''.join([cipher_dict[char] if char in cipher_dict else char for char in plaintext.upper()])++ return ciphertext++# Example usage+plaintext = "HELLO"+key = "QWERTYUIOPASDFGHJKLZXCVBNM" # The key must be a permutation of the alphabet++ciphertext = monoalphabetic_encrypt(plaintext, key)+print(f"Encrypted: {ciphertext}")+```++#### **2. Monoalphabetic Cipher Decryption**:++```python+def monoalphabetic_decrypt(ciphertext, key):+ alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+ # Create a dictionary for the inverse cipher mapping+ cipher_dict = {key[i]: alphabet[i] for i in range(len(alphabet))}++ # Decrypt the ciphertext+ plaintext = ''.join([cipher_dict[char] if char in cipher_dict else char for char in ciphertext.upper()])++ return plaintext++# Example usage+decrypted_text = monoalphabetic_decrypt(ciphertext, key)+print(f"Decrypted: {decrypted_text}")+```++### **Example Output**:++```+Encrypted: PYRRF+Decrypted: HELLO+```++### **Conclusion**++The **Monoalphabetic Cipher** is a simple substitution cipher that is easy to implement and understand. However, due to its vulnerability to frequency analysis, it is not secure by modern cryptographic standards. It is mostly used today for educational purposes or in situations where security is not a primary concern. More advanced encryption methods, such as polyalphabetic ciphers and modern symmetric encryption algorithms (like AES), are far more secure and are widely used for protecting sensitive information.