Skip to content

Commit

Permalink
Affine Cipher Break Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelby Huffman authored and Shelby Huffman committed Oct 2, 2020
1 parent ae6aa75 commit 8aeb2b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
20 changes: 10 additions & 10 deletions java_ciphers/Encryption_Package/src/com/Ciphers/Affine_Cipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class Affine_Cipher {
private static HashMap<String, Integer> affineAlphabet = new HashMap<String, Integer>();

public static int[] breakAffineCipher(String msg) {
ArrayList<Integer> keys = new ArrayList<Integer>();
ArrayList<Integer> Akeys = new ArrayList<Integer>();
ArrayList<Integer> Bkeys = new ArrayList<Integer>();

// 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
Expand All @@ -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;
Expand Down

0 comments on commit 8aeb2b3

Please sign in to comment.