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 One−Time Pad Cipher.md

Abdul Wahab Junaid500463672ae4
diff --git "a/Cryptography/Cryptography Course/Course/One\342\210\222Time Pad Cipher.md" "b/Cryptography/Cryptography Course/Course/One\342\210\222Time Pad Cipher.md"
new file mode 100644
index 0000000..166d46c
--- /dev/null
+++ "b/Cryptography/Cryptography Course/Course/One\342\210\222Time Pad Cipher.md"
@@ -0,0 +1,179 @@
+### **Cryptography - One-Time Pad Cipher**
+
+The **One-Time Pad (OTP)** is a theoretically **unbreakable encryption system** that was first described by **Claude Shannon** in 1949. It is an example of a **symmetric-key cipher** where the encryption key is as long as the message itself and is used only once. When used correctly, the One-Time Pad guarantees **perfect secrecy**.
+
+### **How the One-Time Pad Works**
+
+The One-Time Pad works by using a **randomly generated key** that is **as long as the message** being encrypted. This key is combined with the plaintext (the original message) using a simple operation, typically **XOR (exclusive or)**. Because the key is random and used only once, there is no pattern for an attacker to exploit, making the cipher theoretically impossible to break.
+
+#### **Steps for Encryption:**
+
+1. **Key Generation:**
+ - The key must be **truly random** and **as long as the message**. If the message is 1000 characters long, the key must also be 1000 characters long.
+ - Each character of the key is independent and random, ensuring there are no patterns that could be exploited by an attacker.
+
+2. **XOR Operation:**
+ - For encryption, the plaintext message is **converted to binary** (e.g., using ASCII or Unicode encoding).
+ - The key is also converted to binary.
+ - The plaintext and the key are combined using the **XOR operation**:
+ - If the plaintext bit is 0 and the key bit is 0, the result is 0.
+ - If the plaintext bit is 1 and the key bit is 1, the result is 0.
+ - If the plaintext bit is 0 and the key bit is 1, the result is 1.
+ - If the plaintext bit is 1 and the key bit is 0, the result is 1.
+
+ This operation produces the ciphertext.
+
+3. **Transmission of Ciphertext:**
+ - The ciphertext, which is the result of the XOR operation, is sent to the receiver.
+
+#### **Decryption:**
+Decryption is simply the reverse of encryption. The receiver needs the same key to decrypt the message:
+1. The ciphertext is combined with the same key using the **XOR operation**.
+2. Because of the properties of XOR (i.e., XOR-ing a value with itself results in 0), applying the key a second time will recover the original plaintext.
+
+---
+
+### **Example of One-Time Pad Encryption and Decryption**
+
+Let’s walk through an example using the **XOR operation** to encrypt and decrypt a short message.
+
+#### **Plaintext Message**: "HELLO"
+- Convert the message to binary:
+ - H = 01001000
+ - E = 01000101
+ - L = 01001100
+ - L = 01001100
+ - O = 01001111
+
+#### **Key (Randomly Generated)**:
+ Suppose the key is generated randomly as follows:
+ - Key = "XMCKL"
+ - X = 01011000
+ - M = 01101101
+ - C = 01000011
+ - K = 01001011
+ - L = 01001100
+
+#### **Encryption Process:**
+- XOR each bit of the plaintext with the corresponding bit of the key.
+
+**Plaintext 'H' (01001000) XOR Key 'X' (01011000):**
+```
+01001000
+XOR
+01011000
+--------------
+ 00010000 (this is 16 in decimal, which corresponds to 'P')
+```
+
+**Plaintext 'E' (01000101) XOR Key 'M' (01101101):**
+```
+01000101
+XOR
+01101101
+--------------
+ 00101000 (this is 40 in decimal, which corresponds to '8')
+```
+
+Repeat this for the rest of the characters in the message.
+
+#### **Ciphertext:**
+After applying XOR to all characters, the ciphertext will be a series of random bits, which are essentially unreadable without the key.
+
+#### **Decryption Process:**
+- To decrypt, the receiver takes the ciphertext and applies the **same key** using the XOR operation:
+ - XOR the ciphertext with the same key to retrieve the original plaintext.
+
+---
+
+### **Properties of One-Time Pad**
+
+1. **Perfect Secrecy:**
+ - When the key is truly random, as long as the message, and used only once, the One-Time Pad provides **perfect secrecy**. This means that no amount of computational power can break the encryption.
+
+2. **Unbreakable:**
+ - The key’s randomness ensures that there is no discernible pattern for an attacker to exploit. Without the key, the ciphertext cannot be decrypted.
+
+3. **No Key Reuse:**
+ - The key must **never be reused** for any other message. Reusing a key allows attackers to compare ciphertexts and potentially find patterns to break the cipher.
+
+4. **Key Management Challenge:**
+ - One major issue with the One-Time Pad is the need to securely generate, distribute, and store **long, random keys**. The key must be shared securely between the sender and receiver and be kept secret. If an attacker obtains the key, they can decrypt the message.
+
+---
+
+### **Advantages of One-Time Pad**
+
+1. **Perfect Security**:
+ - If implemented correctly with truly random keys that are only used once, the One-Time Pad offers **unbreakable security**.
+
+2. **Simplicity**:
+ - The encryption and decryption processes are simple and involve basic bitwise operations, which makes the cipher easy to implement at a fundamental level.
+
+---
+
+### **Disadvantages of One-Time Pad**
+
+1. **Key Distribution and Management**:
+ - The key must be as long as the message and securely distributed. This presents a significant challenge, especially for long messages.
+
+2. **Key Storage**:
+ - Storing large random keys securely and ensuring they are not exposed to unauthorized individuals is a major challenge.
+
+3. **Practical Issues**:
+ - In practice, it is very difficult to ensure that the key is both random and used only once, particularly over large communication networks.
+
+4. **Scalability**:
+ - The One-Time Pad does not scale well for modern communication systems, especially in environments where large amounts of data are exchanged.
+
+---
+
+### **Python Implementation of One-Time Pad**
+
+Here’s a simple Python implementation of the One-Time Pad using XOR encryption and decryption:
+
+```python
+import os
+
+# Generate a random key of the same length as the message
+def generate_key(length):
+ return os.urandom(length)
+
+# XOR encryption function
+def encrypt(plaintext, key):
+ ciphertext = bytes([p ^ k for p, k in zip(plaintext.encode(), key)])
+ return ciphertext
+
+# XOR decryption function
+def decrypt(ciphertext, key):
+ plaintext = bytes([c ^ k for c, k in zip(ciphertext, key)])
+ return plaintext.decode()
+
+# Example usage
+plaintext = "HELLO"
+key = generate_key(len(plaintext)) # Generate a random key of the same length
+
+print(f"Plaintext: {plaintext}")
+print(f"Key: {key}")
+
+ciphertext = encrypt(plaintext, key)
+print(f"Ciphertext: {ciphertext}")
+
+decrypted_text = decrypt(ciphertext, key)
+print(f"Decrypted: {decrypted_text}")
+```
+
+### **Example Output:**
+
+```
+Plaintext: HELLO
+Key: b'\xf1\x91\x80\xaf\xcd'
+Ciphertext: b'\x15\xd9\xdb\xe8\xda'
+Decrypted: HELLO
+```
+
+---
+
+### **Conclusion**
+
+The **One-Time Pad** is a theoretically unbreakable encryption system that offers **perfect security** when the key is truly random, as long as the message, and used only once. However, its practical application is limited by challenges related to key generation, distribution, and management. Despite these limitations, the One-Time Pad remains a key concept in cryptography, demonstrating the importance of randomness and key security in encryption.