This Java program implements the Playfair Cipher, a classical encryption technique, to encrypt and decrypt messages using a key. The encrypted text is saved in a file (encrypted.txt), and it can be decrypted back to its original form using the same key.
The Playfair Cipher encrypts text using a 5x5 matrix of letters derived from a keyword. Here’s how it works:
-
Creating the Key Square:
- The given keyword (e.g.,
SECRET) is placed into a 5x5 grid. - The rest of the alphabet (excluding
J, which is replaced withI) fills the remaining spaces.
Example key square for
SECRET: S E C R T A B D F G H I K L M N O P Q U V W X Y Z - The given keyword (e.g.,
pgsql Copy Edit
Preparing the Text:
- Converts text to uppercase and removes spaces and special characters.
- If a letter pair repeats (e.g.,
HELLO→HE LX LO), anXis inserted. - If the text length is odd, an
Xis appended.
Encryption Rules:
- Same row: Each letter moves right (wrap around if needed).
- Same column: Each letter moves down (wrap around if needed).
- Rectangle rule: Swap letters diagonally within the matrix.
Decryption Rules:
- Similar to encryption but move left (for rows) and up (for columns).
Handles user input and controls encryption & decryption.
- Generates the 5x5 key square from the given key.
- Encrypts the input text using Playfair rules.
- Uses the same key square.
- Decrypts the message from
encrypted.txt.
mkdir PlayfairCipher cd PlayfairCipher touch Main.java Encrypt.java Decrypt.java README.md
bash Copy Edit Copy and paste the Java files into the directory.
javac Main.java Encrypt.java Decrypt.java
shell Copy Edit
java Main
pgsql Copy Edit
-
Encryption:
- Choose 1 (Encrypt a message).
- Enter a key (e.g.,
SECRET). - Enter the message (e.g.,
HELLO WORLD). - The encrypted text is saved to
encrypted.txt.
-
Decryption:
- Choose 2 (Decrypt a file).
- Enter the same key used for encryption.
- The program will output the decrypted message.
Playfair Cipher Encryption & Decryption
Encrypt a message
Decrypt a file Choose an option: 1 Enter the key: SECRET Enter the message to encrypt: HELLO WORLD Encrypted text saved to encrypted.txt
pgsql
Copy
Edit
(The encrypted text is stored in encrypted.txt.)
Playfair Cipher Encryption & Decryption
Encrypt a message
Decrypt a file Choose an option: 2 Enter the key: SECRET Decrypted message: HELXLOXWORLDX
yaml
Copy
Edit
(Note: X is used for repeated letters and padding.)
| Condition | Encryption | Decryption |
|---|---|---|
| Same Row | Move right | Move left |
| Same Column | Move down | Move up |
| Rectangle | Swap diagonally | Swap diagonally |
✅ Works with any keyword for encryption.
✅ Automatically removes spaces & special characters.
✅ Handles repeated letters by inserting X.
✅ Supports file-based encryption & decryption.
🔹 Future Improvements:
- Allow users to choose a custom file name.
- Improve handling of special characters.
Wallace Scott
🛠️ Ethical Hacker, Software Engineer, and Security Enthusiast.
This README provides everything needed to understand, set up, and run the Playfair Cipher encryption & decryption in Java! 🚀 Let me know if you want any modifications.
██ ██ ███████
██ ██ ██
██ █ ██ ███████
██ ███ ██ ██
███ ███ ███████
| wallacescott.netlify.app | https://www.linkedin.com/in/wallace-dsouza/ | https://github.com/WallaceScott240