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 - Playfair Cipher
The Playfair Cipher is a digraph (two-letter) substitution cipher that was invented by Charles Wheatstone in 1854 but became famous when it was promoted by Lord Playfair, hence the name. Unlike simpler substitution ciphers like the Caesar cipher or the monoalphabetic cipher, the Playfair cipher encrypts digraphs (pairs of letters) instead of individual letters, providing greater security by adding an additional layer of complexity.
How the Playfair Cipher Works
The Playfair Cipher uses a 5x5 matrix of letters as the key. The matrix is filled with a keyword or phrase (without repeated letters), followed by the remaining letters of the alphabet (usually excluding J, which is combined with I). The message is encrypted by breaking the plaintext into digraphs (pairs of letters), and each pair of letters is then encrypted according to their positions in the matrix.
Steps for Encryption:
Construct the Key Matrix:
- Choose a keyword or phrase and remove any duplicate letters.
- Fill the 5x5 matrix with the letters of the keyword.
- The remaining spaces are filled with the remaining letters of the alphabet (typically excluding J).
Example: If the keyword is "PLAYFAIR", the matrix might look like this:
P L A Y F I R B C D E G H K M N O Q S T U V W X ZPrepare the Plaintext:
- If the plaintext has an odd number of letters, add an extra letter (usually X) to make it even.
- If the plaintext has two of the same letter in a digraph (e.g., LL), insert an extra letter (usually X) between them (e.g., LX).
For example, if the message is "HELLO", we change it to "HE LX LO".
Encrypt the Digraphs: The encryption depends on the position of the two letters in the 5x5 matrix:
Case 1: Same Row: If the letters of the digraph appear in the same row of the matrix, replace each letter with the letter to its immediate right. If the letter is at the end of the row, wrap around to the beginning of the row.
- For example: "HE" → "EG" (since H and E are in the same row).
Case 2: Same Column: If the letters appear in the same column of the matrix, replace each letter with the letter immediately below it. If the letter is at the bottom of the column, wrap around to the top.
- For example: "IL" → "RI" (since I and L are in the same column).
Case 3: Different Row and Column: If the letters form a rectangle, replace each letter with the letter on the same row but in the column of the other letter of the digraph.
- For example: "HE" → "EG" (if they form a rectangle).
Form the Ciphertext: After applying the encryption rules to each digraph, you will obtain the ciphertext.
Example of Playfair Cipher Encryption
Let's walk through an example to illustrate how the Playfair cipher works.
Plaintext: "HELLO"
Keyword: "KEYWORD"
Construct the 5x5 Matrix:
The keyword is KEYWORD, so we remove duplicate letters and complete the matrix with the remaining letters of the alphabet (excluding J):
K E Y W O R A B C D F G H I L M N P Q S T U V X ZPrepare the Plaintext:
The plaintext "HELLO" has repeating L's. We split it into digraphs, changing LL to LX:
"HE", "LX", "LO"Encrypt the Digraphs:
- "HE": H is in row 3, column 3, and E is in row 1, column 2. They form a rectangle. So, replace H with the letter on the same row as E (which is G) and replace E with the letter on the same row as H (which is L). Result: "GL".
- "LX": L is in row 3, column 5, and X is in row 5, column 4. They form a rectangle. Replace L with I (same row as X) and X with S (same row as L). Result: "IS".
- "LO": L is in row 3, column 5, and O is in row 1, column 5. They are in the same column. So, replace L with I (below L) and O with R (below O). Result: "IR".
Ciphertext: The final ciphertext is:
"GL IS IR"
Decryption Process
To decrypt the ciphertext using the Playfair Cipher, the same key matrix is used. The decryption rules are simply the reverse of the encryption rules:
- Same Row: If the letters of the digraph are in the same row, replace each letter with the letter immediately to its left. If the letter is at the beginning of the row, wrap around to the end of the row.
- Same Column: If the letters are in the same column, replace each letter with the letter immediately above it. If the letter is at the top, wrap around to the bottom.
- Different Row and Column: If the letters form a rectangle, replace each letter with the letter on the same row but in the column of the other letter of the digraph.
Python Implementation of Playfair Cipher
Here is a Python implementation of the Playfair Cipher:
def create_playfair_matrix(key):
# Remove duplicates and convert to uppercase
key = ''.join(sorted(set(key), key=key.index)).upper()
# Initialize the 5x5 matrix
alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"
matrix = []
# Fill the matrix with the key letters
for char in key:
if char in alphabet:
matrix.append(char)
alphabet = alphabet.replace(char, "")
# Append the rest of the alphabet (excluding 'J')
matrix += list(alphabet)
return [matrix[i:i+5] for i in range(0, 25, 5)]
def find_position(matrix, char):
# Find the row and column of the letter in the matrix
for i, row in enumerate(matrix):
if char in row:
return i, row.index(char)
return -1, -1
def playfair_encrypt(plaintext, key):
# Prepare the key matrix
matrix = create_playfair_matrix(key)
# Prepare the plaintext (remove spaces and J -> I)
plaintext = plaintext.replace('J', 'I').upper()
if len(plaintext) % 2 != 0:
plaintext += 'X'
# Encrypt the plaintext
ciphertext = ""
for i in range(0, len(plaintext), 2):
a, b = plaintext[i], plaintext[i+1]
row1, col1 = find_position(matrix, a)
row2, col2 = find_position(matrix, b)
if row1 == row2:
ciphertext += matrix[row1][(col1 + 1) % 5]
ciphertext += matrix[row2][(col2 + 1) % 5]
elif col1 == col2:
ciphertext += matrix[(row1 + 1) % 5][col1]
ciphertext += matrix[(row2 + 1) % 5][col2]
else:
ciphertext += matrix[row1][col2]
ciphertext += matrix[row2][col1]
return ciphertext
# Example usage
plaintext = "HELLO"
key = "KEYWORD"
ciphertext = playfair_encrypt(plaintext, key)
print(f"Encrypted: {ciphertext}")
Example Output:
Encrypted: GLISIR
Advantages of Playfair Cipher
- Stronger Security: By encrypting pairs of letters instead of single letters, the Playfair cipher adds more complexity and is harder to break compared to monoalphabetic ciphers.
- Resistance to Frequency Analysis: Since it works on pairs of letters, frequency analysis (which works well for single-letter ciphers) is less effective.
Disadvantages of Playfair Cipher
- Key Management: The key must be kept secret. If an attacker knows the key, they can easily decrypt the message.
- No Large-Scale Usage: The Playfair cipher is not
suitable for encrypting large amounts of data, as it was designed for simple manual encryption. 3. Vulnerable to Pattern Recognition: It is still vulnerable to more sophisticated cryptanalytic techniques, especially when the key is short or the message has common patterns.
Conclusion
The Playfair Cipher is an important historical cipher that enhances security by encrypting pairs of letters instead of single letters. Though it provides a greater level of security than simpler ciphers like the Caesar cipher, it is still vulnerable to cryptanalysis and is no longer widely used today. However, it laid the groundwork for more complex cryptographic systems and remains a useful cipher for learning about classical cryptography.