Last active
August 29, 2015 14:25
-
-
Save rahul-rkt/9d38f9ac6710a98fd4ae to your computer and use it in GitHub Desktop.
REPL ---> http://repl.it/yHs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.rahulthakur; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
class Main { | |
private static final char[] ciphertext = "OTWEWNGWCBPQABIZVQAPMLJGZWTTQVOBQUMAPMIDGZCABEQVBMZLZIXMLAXZQVOQVLMMXAVWEIVLLIZSNZWABJQZLWNLMTQOPBVIUMLGWCBPAEQNBTGTMNBBPMVMABITIAKWCTLVBBQUMQBEPQTMQBEIAQVUGBZCAB".toCharArray(); | |
private static final char[] alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); | |
private static final char[] targets = "ETAOINSHRDLCUMWFGYPBVKJQXZ".toCharArray(); | |
private static short loopCounter = 0; | |
private static Scanner reader = null; | |
public static void main(String[] args) { | |
System.out.println("Press 1: Initiate Statistical Attack\nPress 2: Initiate Brute Force Attack"); | |
System.out.print("Your choice: "); | |
try { | |
reader = new Scanner(System.in); | |
switch (reader.nextInt()) { | |
case 1: | |
statisticalAttack(); | |
break; | |
case 2: | |
bruteForceAttack(); | |
break; | |
default: | |
System.out.println("Bad Input!"); | |
break; | |
} | |
} catch (Exception e) { | |
} finally { | |
try { | |
reader.close(); | |
} catch (Exception e) { | |
} | |
} | |
} | |
private static void statisticalAttack() { | |
System.out.println("\n\nInitiating Statistical Attack\n=============================="); | |
Map<String, Integer> frequencyMap = new HashMap<String, Integer>(); | |
for (char a : alphabets) | |
frequencyMap.put(String.valueOf(a), 0); | |
for (char c : ciphertext) | |
frequencyMap.put(String.valueOf(c), frequencyMap.get(String.valueOf(c)) + 1); | |
System.out.println("Generating frequency map for the ciphertext:"); | |
for (String s : frequencyMap.keySet()) | |
System.out.println(s + " => " + frequencyMap.get(s)); | |
loopCounter = 0; | |
for (char t : targets) | |
for (char a : alphabets) { | |
loopCounter++; | |
String s = Collections.max(frequencyMap.entrySet(), (entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1).getKey(); | |
System.out.println("\n\nAttempt " + loopCounter + " with target " + t + ":"); | |
transform(s.charAt(0), t); | |
exitOrContinue(); | |
frequencyMap.put(s, 0); | |
} | |
System.out.println("This is impossible! I'm out of here!"); | |
} | |
private static void bruteForceAttack() { | |
System.out.println("\n\nInitiating Brute Force Attack\n=============================="); | |
loopCounter = 0; | |
for (char a : alphabets) { | |
if (a == 'A') | |
continue; | |
loopCounter++; | |
System.out.println("\n\nAttempt " + loopCounter + " with target " + a + ":"); | |
transform(a, 'A'); | |
exitOrContinue(); | |
} | |
System.out.println("This is impossible! I'm out of here!"); | |
} | |
private static void transform(char value, char target) { | |
System.out.println("Assuming " + value + " as " + target + " --->"); | |
int offset = 0; | |
if (target > value) | |
offset = value - target; | |
else | |
offset = target - value; | |
for (char c : ciphertext) { | |
int x = c + offset; | |
if (x < Integer.valueOf('A')) | |
x = 'Z' - ('A' - x) + 1; | |
System.out.print(Character.toChars(x)); | |
} | |
System.out.print("\n\n"); | |
} | |
private static void exitOrContinue() { | |
System.out.print("Does that make sense?(1 to exit/0 to continue):"); | |
if (reader.nextInt() == 0) | |
System.out.println("\nMoving on then.."); | |
else { | |
System.out.println("\nWe did it, and in only " + loopCounter + " attempts!"); | |
System.exit(0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment