The ADFGVX cipher is a fractionating transposition cipher that was used by the German Army during World War I. It combines a Polybius square substitution with a columnar transposition to enhance encryption strength. The name "ADFGVX" refers to the six letters (A, D, F, G, V, X) used as row
| 25 | * @author bennybebo |
| 26 | */ |
| 27 | public class ADFGVXCipher { |
| 28 | |
| 29 | // Constants used in the Polybius square |
| 30 | private static final char[] POLYBIUS_LETTERS = {'A', 'D', 'F', 'G', 'V', 'X'}; |
| 31 | private static final char[][] POLYBIUS_SQUARE = {{'N', 'A', '1', 'C', '3', 'H'}, {'8', 'T', 'B', '2', 'O', 'M'}, {'E', '5', 'W', 'R', 'P', 'D'}, {'4', 'F', '6', 'G', '7', 'I'}, {'9', 'J', '0', 'K', 'L', 'Q'}, {'S', 'U', 'V', 'X', 'Y', 'Z'}}; |
| 32 | |
| 33 | // Maps for fast substitution lookups |
| 34 | private static final Map<String, Character> POLYBIUS_MAP = new HashMap<>(); |
| 35 | private static final Map<Character, String> REVERSE_POLYBIUS_MAP = new HashMap<>(); |
| 36 | |
| 37 | // Static block to initialize the lookup tables from the Polybius square |
| 38 | static { |
| 39 | for (int i = 0; i < POLYBIUS_SQUARE.length; i++) { |
| 40 | for (int j = 0; j < POLYBIUS_SQUARE[i].length; j++) { |
| 41 | String key = "" + POLYBIUS_LETTERS[i] + POLYBIUS_LETTERS[j]; |
| 42 | POLYBIUS_MAP.put(key, POLYBIUS_SQUARE[i][j]); |
| 43 | REVERSE_POLYBIUS_MAP.put(POLYBIUS_SQUARE[i][j], key); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Encrypts a given plaintext using the ADFGVX cipher with the provided keyword. |
| 50 | * Steps: |
| 51 | * 1. Substitute each letter in the plaintext with a pair of ADFGVX letters. |
| 52 | * 2. Perform a columnar transposition on the fractionated text using the keyword. |
| 53 | * |
| 54 | * @param plaintext The message to be encrypted (can contain letters and digits). |
| 55 | * @param key The keyword for columnar transposition. |
| 56 | * @return The encrypted message as ciphertext. |
| 57 | */ |
| 58 | public String encrypt(String plaintext, String key) { |
| 59 | plaintext = plaintext.toUpperCase().replaceAll("[^A-Z0-9]", ""); // Sanitize input |
| 60 | StringBuilder fractionatedText = new StringBuilder(); |
| 61 | |
| 62 | for (char c : plaintext.toCharArray()) { |
| 63 | fractionatedText.append(REVERSE_POLYBIUS_MAP.get(c)); |
| 64 | } |
| 65 | |
| 66 | return columnarTransposition(fractionatedText.toString(), key); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Decrypts a given ciphertext using the ADFGVX cipher with the provided keyword. |
| 71 | * Steps: |
| 72 | * 1. Reverse the columnar transposition performed during encryption. |
| 73 | * 2. Substitute each pair of ADFGVX letters with the corresponding plaintext letter. |
| 74 | * The resulting text is the decrypted message. |
| 75 | * |
| 76 | * @param ciphertext The encrypted message. |
| 77 | * @param key The keyword used during encryption. |
| 78 | * @return The decrypted plaintext message. |
| 79 | */ |
| 80 | public String decrypt(String ciphertext, String key) { |
| 81 | String fractionatedText = reverseColumnarTransposition(ciphertext, key); |
| 82 | |
| 83 | StringBuilder plaintext = new StringBuilder(); |
| 84 | for (int i = 0; i < fractionatedText.length(); i += 2) { |