From 8aeb2b3e963e3e2b43a44b878dbdba95217d76eb Mon Sep 17 00:00:00 2001 From: Shelby Huffman Date: Thu, 1 Oct 2020 22:22:49 -0500 Subject: [PATCH] Affine Cipher Break Implemented --- README.md | 2 +- .../src/com/Ciphers/Affine_Cipher.java | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 258832e..db2180e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ | Cipher | Encrypt | Decrypt | Break | | ------ | :-----: | :-----: | :---: | | Caesar | YES | YES | YES | -| Affine | YES | YES | NO | +| Affine | YES | YES | YES | | ROT13 | YES | YES | N/A | | Atbash | YES | YES | N/A | | Enigma | NO | NO | NO | diff --git a/java_ciphers/Encryption_Package/src/com/Ciphers/Affine_Cipher.java b/java_ciphers/Encryption_Package/src/com/Ciphers/Affine_Cipher.java index 5579706..0f5c252 100644 --- a/java_ciphers/Encryption_Package/src/com/Ciphers/Affine_Cipher.java +++ b/java_ciphers/Encryption_Package/src/com/Ciphers/Affine_Cipher.java @@ -12,7 +12,8 @@ public class Affine_Cipher { private static HashMap affineAlphabet = new HashMap(); public static int[] breakAffineCipher(String msg) { - ArrayList keys = new ArrayList(); + ArrayList Akeys = new ArrayList(); + ArrayList Bkeys = new ArrayList(); // Calculate the expected frequencies of the letters in a given message // I.E Based on the prob a letter will appear in 1000 letters of english @@ -36,21 +37,20 @@ public static int[] breakAffineCipher(String msg) { // Finally, utilizing the chi square test to calculate chi-square double chiSquare = Cipher_Utility.chiSquareTest(expectedLettersFrequencies, lettersFrequencies); chiSquares.add(chiSquare); - keys.add(i); - keys.add(j); + Akeys.add(i); + Bkeys.add(j); } } } - - int probableOffset = 0; + int probableChi = 0; int[] result = new int[2]; for (int k = 0; k < chiSquares.size(); k++) { - if (chiSquares.get(k) < chiSquares.get(probableOffset)) { - probableOffset = k; - result[0] = keys.get(k); - result[1] = keys.get(k + 1); - System.out.println(String.format("Chi-Square for offset %d: %.2f", k, chiSquares.get(k))); + if (chiSquares.get(k) < chiSquares.get(probableChi)) { + probableChi = k; + + result[0] = Akeys.get(k); + result[1] = Bkeys.get(k); } } return result;