This is a Rust implementation of the Playfair Cipher
The Playfair Cipher is a digraph substitution cipher, meaning it encrypts pairs of letters (digraphs) instead of single letters. Here's how it works:
-
Encryption Table:
- A 5x5 matrix is generated using a keyword.
- The keyword is entered into the matrix, skipping duplicate letters and treating
JasI. - The remaining letters of the alphabet are added to fill the table.
-
Encryption Rules:
- The plaintext is divided into pairs of letters.
- If a pair contains the same letter (e.g., "LL"), an
Xis inserted between them. - If the plaintext length is odd, an
Xis appended to the end. - Each pair is encrypted based on their positions in the matrix:
- Same Row: Replace each letter with the one to its right (wrapping around to the first column if necessary).
- Same Column: Replace each letter with the one below it (wrapping around to the top row if necessary).
- Rectangle Rule: Swap the column positions of the letters.
-
Decryption Rules:
- Decryption follows the same logic as encryption but shifts positions in the opposite direction:
- Same Row: Replace each letter with the one to its left.
- Same Column: Replace each letter with the one above it.
- Rectangle Rule: Swap the column positions of the letters.
- Decryption follows the same logic as encryption but shifts positions in the opposite direction:
-
Case Insensitivity:
- The cipher is not case-sensitive. All input is converted to uppercase, and spaces or non-alphabetic characters are ignored.
-
Decryption Output:
- Decrypted text may include
Xbetween repeated characters or at the end of a message, which were added during encryption for padding or disambiguation.
- Decrypted text may include
-
Encryption Table Generation:
- The
generate_playfair_tablefunction creates the 5x5 matrix using the given key. - Duplicate letters and
Jare handled appropriately.
- The
-
Message Encryption/Decryption:
- The
playfair_cipherfunction processes the text according to the rules of the Playfair Cipher. - It handles repeated characters and odd-length messages by inserting
Xas needed.
- The
To compile the code, ensure you have the Rust toolchain installed. Then, run:
cargo build --releaseThis will generate the executable in the target/release directory.
Alternatively, you can use the precompiled binaries:
- Linux:
playfair-x86_64-linux - Windows:
playfair-win.exe
You can run the program witj the precomiled binaries:
./playfair-x86_64-linux --key KEYWORD --input TEXT [--decrypt]or if you compiled with cargo build --release
./target/release/playfair --key KEYWORD --input TEXT [--decrypt]For Windows, use:
playfair-win.exe --key KEYWORD --input TEXT [--decrypt]-
Encrypt a Message:
./playfair-x86_64-linux -k KEYWORD -i "HELLO WORLD"Output:
Generated Playfair Table: ['K', 'E', 'Y', 'W', 'O'] ['R', 'D', 'A', 'B', 'C'] ['F', 'G', 'H', 'I', 'L'] ['M', 'N', 'P', 'Q', 'S'] ['T', 'U', 'V', 'X', 'Z'] Encrypted Text: GYIZSCOKCFBU -
Decrypt a Message:
./playfair-x86_64-linux -k KEYWORD -i GYIZSCOKCFBU -d
Output:
Generated Playfair Table: ['K', 'E', 'Y', 'W', 'O'] ['R', 'D', 'A', 'B', 'C'] ['F', 'G', 'H', 'I', 'L'] ['M', 'N', 'P', 'Q', 'S'] ['T', 'U', 'V', 'X', 'Z'] Decrypted Text: HELXLOWORLDX
You can get the help message using the --help or-h flag:
./playfair-x86_64-linux --helpTo run the unit tests run:
cargo test-
Decrypted Text with
X:- Decrypted text may include
Xcharacters:- Between repeated characters (for ex. "BALLOON" -> "BALXLOON").
- At the end of an odd-length message.
This is a limitation of the Playfair Cipher, and not a bug.
- Decrypted text may include
-
Non-Alphabetic Characters:
- Non-alphabetic characters (for ex. numbers) are ignored.
Developed by martian58.