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.
Cryptography - Simple Substitution Cipher
The Simple Substitution Cipher is one of the most basic encryption techniques in cryptography, where each letter of the plaintext is replaced by another letter or symbol. The key in a substitution cipher is a permutation of the alphabet. This means each letter of the plaintext is substituted by a corresponding letter from a scrambled alphabet, which serves as the key. The key must be kept secret to maintain the cipher's security.
How Simple Substitution Cipher Works
- Plaintext: This is the original message you want to encrypt.
- Alphabet: A fixed alphabet (e.g., A to Z) is used to map each letter of the plaintext.
- Key: The key is a scrambled version of the alphabet. Each letter of the plaintext is replaced by the corresponding letter from the key.
For example, if we use the following key:
| Plaintext | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Ciphertext | Q | W | E | R | T | Y | U | I | O | P | A | S | D | F | G | H | J | K | L | Z | X | C | V | B | N | M |
If the plaintext is "HELLO", using the above substitution key, we would substitute each letter of "HELLO" with the corresponding letter from the key:
- H → I
- E → T
- L → S
- L → S
- O → L
Thus, the ciphertext would be "ITSSL".
Decryption
To decrypt the message, the receiver must know the key (the scrambled alphabet) and reverse the substitution process. This means they replace each letter in the ciphertext with the letter from the plaintext alphabet that corresponds to the letter in the ciphertext alphabet.
For the ciphertext "ITSSL", using the same key:
- I → H
- T → E
- S → L
- S → L
- L → O
Thus, the plaintext is "HELLO".
Python Code for Simple Substitution Cipher
Here’s a Python implementation of both encryption and decryption for a simple substitution cipher:
# Function to encrypt the plaintext using a substitution cipher
def encrypt_substitution(plaintext, key):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
plaintext = plaintext.upper()
ciphertext = ''
for char in plaintext:
if char in alphabet: # Encrypt only alphabetic characters
index = alphabet.index(char)
ciphertext += key[index]
else:
ciphertext += char # Non-alphabet characters remain unchanged
return ciphertext
# Function to decrypt the ciphertext using a substitution cipher
def decrypt_substitution(ciphertext, key):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ciphertext = ciphertext.upper()
plaintext = ''
for char in ciphertext:
if char in key: # Decrypt only alphabetic characters
index = key.index(char)
plaintext += alphabet[index]
else:
plaintext += char # Non-alphabet characters remain unchanged
return plaintext
# Example of encryption and decryption
key = 'QWERTYUIOPASDFGHJKLZXCVBNM' # The scrambled alphabet used as the key
plaintext = "HELLO"
ciphertext = encrypt_substitution(plaintext, key)
print(f"Ciphertext: {ciphertext}")
decrypted_text = decrypt_substitution(ciphertext, key)
print(f"Decrypted Text: {decrypted_text}")
Explanation of the Code:
encrypt_substitutionfunction: This function takes the plaintext and the key as input. It loops over each character in the plaintext, and if it's a letter, it finds its corresponding letter from the key and appends it to the ciphertext.decrypt_substitutionfunction: This function takes the ciphertext and the key as input. It reverses the process by finding each character in the ciphertext's key and replacing it with the corresponding letter from the alphabet to get the original plaintext.
Example Output:
Ciphertext: ITSSL
Decrypted Text: HELLO
Advantages of Simple Substitution Cipher:
- Simple to Implement: The substitution cipher is easy to understand and implement.
- Fast Encryption/Decryption: It allows fast encryption and decryption since it's a direct substitution.
- Good for Educational Purposes: It's a useful cipher for learning about basic encryption concepts.
Drawbacks of Simple Substitution Cipher:
- Vulnerable to Frequency Analysis: The simple substitution cipher is vulnerable to frequency analysis, where attackers can look at the frequency of letters in the ciphertext and compare them to the known frequencies of letters in the language.
- For example, in the English language, 'E' is the most frequent letter, so by analyzing the frequency of letters in the ciphertext, an attacker can guess the most likely mapping.
- Small Key Space: The key space for a substitution cipher is relatively small because it's simply a permutation of the 26 letters of the alphabet, leading to a limited number of possible keys (26! = 403291461126605635584000000).
- Pattern Preservation: The pattern of the plaintext is preserved in the ciphertext. For example, repeating letters in the plaintext will appear as repeating letters in the ciphertext, which can be exploited by an attacker.
Conclusion
The Simple Substitution Cipher is an elementary encryption method that is easy to understand and implement, but it is not secure by modern standards due to its vulnerability to frequency analysis. While it is not suitable for securing sensitive information, it is still valuable for demonstrating the basic concepts of encryption and the importance of randomness and key complexity in cryptography.