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 - Polyalphabetic Cipher
A Polyalphabetic Cipher is a type of substitution cipher where multiple substitution alphabets are used to encrypt a plaintext message. Unlike the Monoalphabetic Cipher, which uses a single alphabet for encryption, the polyalphabetic cipher utilizes a sequence of different alphabets to provide a higher level of security. This makes it more resistant to frequency analysis.
The Vigenère cipher is one of the most well-known polyalphabetic ciphers. In a polyalphabetic cipher, the key is used to determine which alphabet to use for substitution at each position in the plaintext.
How Polyalphabetic Cipher Works
In a polyalphabetic cipher:
- A keyword or key phrase is chosen.
- Each letter of the plaintext is encrypted using a corresponding letter from the key.
- The key repeats itself to cover the length of the plaintext.
- Each letter in the plaintext is substituted according to the position of the corresponding letter in the key.
For example, if the key is "KEY" and the plaintext is "HELLO", we will encrypt the plaintext by applying different shifts to each letter, depending on the letter of the key at that position.
Example:
Let’s walk through an example of encrypting the plaintext "HELLO" with the keyword "KEY".
Key: "KEY" (repeated to match the length of the plaintext)
- Plaintext:
"HELLO" - Key:
"KEYKE"
- Plaintext:
To encrypt each letter, we shift each plaintext letter by the position of the corresponding letter in the key (where A = 0, B = 1, C = 2, ..., Z = 25):
- H (shifted by K = 10) → R
- E (shifted by E = 4) → I
- L (shifted by Y = 24) → J
- L (shifted by K = 10) → V
- O (shifted by E = 4) → S
So, the ciphertext becomes "RIJVS".
Encryption Process
The encryption process involves the following steps:
- Choose a keyword (e.g., "KEY").
- Repeat the key so that its length matches the plaintext.
- Shift each letter of the plaintext according to the corresponding letter in the key.
Formula for Encryption:
If $( P )$ is the plaintext letter, $( K )$ is the key letter, and $( C )$ is the ciphertext letter, the encryption process can be represented as:
$[ C_i = (P_i + K_i) \mod 26 ]$
Where:
- $( P_i )$ is the index of the plaintext letter.
- $( K_i )$ is the index of the key letter.
- $( C_i )$ is the index of the ciphertext letter.
- $( \mod 26 )$ ensures that the result is wrapped around to stay within the alphabet.
Decryption Process
To decrypt the ciphertext, the same key is used to reverse the encryption process:
$[ P_i = (C_i - K_i) \mod 26 ]$
Where:
- $( C_i )$ is the index of the ciphertext letter.
- $( K_i )$ is the index of the key letter.
- $( P_i )$ is the index of the plaintext letter.
By applying this decryption formula, we can recover the original message.
Example of Decryption:
Using the previously encrypted message "RIJVS" and the key "KEY":
- Key: "KEYKE"
- For each letter:
- R (shifted by K = 10) → H
- I (shifted by E = 4) → E
- J (shifted by Y = 24) → L
- V (shifted by K = 10) → L
- S (shifted by E = 4) → O
Thus, the original plaintext is "HELLO".
Advantages of Polyalphabetic Cipher
- Stronger Security: The use of multiple substitution alphabets makes the polyalphabetic cipher much harder to break than the monoalphabetic cipher.
- Resistance to Frequency Analysis: Unlike the monoalphabetic cipher, the polyalphabetic cipher distributes letter frequencies more evenly, making it much harder to perform frequency analysis.
- Flexibility: The length of the key can vary, adding additional complexity to the cipher.
Disadvantages of Polyalphabetic Cipher
- Key Management: The key must be kept secret. If an attacker obtains the key, they can easily decrypt the message.
- Key Length: If the key is short, it can repeat and become vulnerable to cryptanalysis (e.g., Kasiski examination).
- Vulnerable to Known Plaintext Attacks: If the attacker knows a part of the plaintext (e.g., common greetings), they can still exploit this to break the cipher.
- Complexity: The polyalphabetic cipher is more complex than simpler ciphers like the Caesar cipher, both in terms of encryption and decryption processes.
Security Improvements and Weaknesses
Although polyalphabetic ciphers, such as the Vigenère cipher, were once considered very secure, they were eventually broken using techniques like the Kasiski Examination and Frequency Analysis of repeated key sequences.
However, polyalphabetic ciphers are still historically important and form the basis for modern encryption algorithms. More advanced ciphers like AES (Advanced Encryption Standard) or RSA use concepts from the polyalphabetic cipher while adding modern cryptographic methods to ensure stronger security.
Python Code Example: Vigenère Cipher (Polyalphabetic Cipher)
Here is a simple Python implementation of the Vigenère cipher:
Encryption:
def vigenere_encrypt(plaintext, key):
# Make the plaintext and key uppercase
plaintext = plaintext.upper()
key = key.upper()
# Repeat the key to match the length of the plaintext
key = (key * (len(plaintext) // len(key))) + key[:len(plaintext) % len(key)]
ciphertext = ""
for i in range(len(plaintext)):
if plaintext[i].isalpha():
# Find the shift amount using the key
shift = ord(key[i]) - ord('A')
# Encrypt the letter
encrypted_char = chr((ord(plaintext[i]) - ord('A') + shift) % 26 + ord('A'))
ciphertext += encrypted_char
else:
ciphertext += plaintext[i]
return ciphertext
# Example usage
plaintext = "HELLO"
key = "KEY"
ciphertext = vigenere_encrypt(plaintext, key)
print(f"Encrypted: {ciphertext}")
Decryption:
def vigenere_decrypt(ciphertext, key):
# Make the ciphertext and key uppercase
ciphertext = ciphertext.upper()
key = key.upper()
# Repeat the key to match the length of the ciphertext
key = (key * (len(ciphertext) // len(key))) + key[:len(ciphertext) % len(key)]
plaintext = ""
for i in range(len(ciphertext)):
if ciphertext[i].isalpha():
# Find the shift amount using the key
shift = ord(key[i]) - ord('A')
# Decrypt the letter
decrypted_char = chr((ord(ciphertext[i]) - ord('A') - shift) % 26 + ord('A'))
plaintext += decrypted_char
else:
plaintext += ciphertext[i]
return plaintext
# Example usage
decrypted_text = vigenere_decrypt(ciphertext, key)
print(f"Decrypted: {decrypted_text}")
Example Output:
Encrypted: RIJVS
Decrypted: HELLO
Conclusion
The Polyalphabetic Cipher is a stronger encryption technique compared to monoalphabetic ciphers. It is more secure because it uses multiple cipher alphabets to encrypt a message, making frequency analysis much more difficult. While it is more secure than simpler ciphers, it is still vulnerable to advanced cryptanalysis techniques, and modern cryptographic systems have evolved to use even more complex algorithms like AES and RSA. Nonetheless, the polyalphabetic cipher laid the foundation for many of the cryptographic concepts used in modern encryption.