Last active
September 12, 2017 22:26
-
-
Save fernandozamoraj/26498e6a9fb082df3402f5701bacee44 to your computer and use it in GitHub Desktop.
FreakCracker.java
This file contains hidden or 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 com.fzj.app; | |
import java.util.*; | |
public class FreakCracker { | |
public String crack(String message){ | |
int i = 0 ; | |
StringBuffer buffer = new StringBuffer(message.length()); | |
int[] frequencies = new int[26]; | |
String mostcommon = "e"; //tnoria"; | |
char[] actualFrequency = "abcdefghijklmnopqrstuvwxyz".toCharArray(); | |
int len = message.length(); | |
for(i=0;i<len;i++){ | |
char ch = message.charAt(i); | |
if(ch >= 97 && ch <= 122){ | |
frequencies[ch-97]++; | |
} | |
} | |
boolean swapped; | |
do{ | |
swapped = false; | |
for( i=0; i<frequencies.length-1; i++){ | |
if(frequencies[i] < frequencies[i+1]){ | |
int temp = frequencies[i]; | |
frequencies[i] = frequencies[i+1]; | |
frequencies[i+1] = temp; | |
temp = actualFrequency[i]; | |
actualFrequency[i] = actualFrequency[i+1]; | |
actualFrequency[i+1] = (char)temp; | |
swapped = true; | |
} | |
} | |
}while(swapped); | |
Map<String, String> charMap = new HashMap<String, String>(); | |
for(i=0;i<mostcommon.length();i++){ | |
charMap.put(""+actualFrequency[i], ""+mostcommon.charAt(i)); | |
} | |
for(i=0;i<message.length();i++){ | |
char ch = message.charAt(i); | |
if(charMap.containsKey(""+ch)){ | |
buffer.append(charMap.get(""+ch)); | |
} | |
else{ | |
if(ch >= 97 && ch <= 122){ | |
buffer.append("_"); | |
} | |
else{ | |
buffer.append(ch); | |
} | |
} | |
} | |
//find the t | |
char tChar = findTheTFromThe(buffer.toString(), message); | |
System.out.println("tChar is " + tChar); | |
if(!charMap.containsKey(""+tChar)){ | |
charMap.put(""+tChar, "t"); | |
} | |
buffer = new StringBuffer(message.length()); | |
for(i=0;i<message.length();i++){ | |
char ch = message.charAt(i); | |
if(charMap.containsKey(""+ch)){ | |
buffer.append(charMap.get(""+ch)); | |
} | |
else{ | |
if(ch >= 97 && ch <= 122){ | |
buffer.append("_"); | |
} | |
else{ | |
buffer.append(ch); | |
} | |
} | |
} | |
//find the h | |
char hChar = findTheH(buffer.toString(), message); | |
System.out.println("hChar is " + hChar); | |
if(!charMap.containsKey(""+hChar)){ | |
charMap.put(""+hChar, "h"); | |
} | |
//find the a | |
char aChar = findTheA(buffer.toString(), message); | |
System.out.println("aChar is " + aChar); | |
if(!charMap.containsKey(""+aChar)){ | |
charMap.put(""+aChar, "a"); | |
} | |
//find the o | |
char oChar = findTheO(buffer.toString(), message); | |
System.out.println("oChar is " + oChar); | |
if(!charMap.containsKey(""+oChar)){ | |
charMap.put(""+oChar, "o"); | |
} | |
//crack prior to next steps | |
buffer = new StringBuffer(message.length()); | |
for(i=0;i<message.length();i++){ | |
char ch = message.charAt(i); | |
if(charMap.containsKey(""+ch)){ | |
buffer.append(charMap.get(""+ch)); | |
} | |
else{ | |
if(ch >= 97 && ch <= 122){ | |
buffer.append("_"); | |
} | |
else{ | |
buffer.append(ch); | |
} | |
} | |
} | |
//find the l | |
char lChar = findDoubleL(buffer.toString(), message); | |
if(!charMap.containsKey(""+lChar)){ | |
charMap.put(""+lChar, "l"); | |
} | |
//find the n | |
char nChar = findTheNFromAnd(buffer.toString(), message); | |
if(!charMap.containsKey(""+nChar)){ | |
charMap.put(""+nChar, "n"); | |
} | |
//find the d | |
char dChar = findTheDFromAnd(buffer.toString(), message); | |
if(!charMap.containsKey(""+dChar)){ | |
charMap.put(""+dChar, "d"); | |
} | |
//find the r | |
char rChar = findTheRFromAre(buffer.toString(), message); | |
if(!charMap.containsKey(""+rChar)){ | |
charMap.put(""+rChar, "r"); | |
} | |
//find the i | |
char iChar = findTheIFromIt(buffer.toString(), message); | |
if(!charMap.containsKey(""+iChar)){ | |
charMap.put(""+iChar, "i"); | |
} | |
//find the g | |
char gChar = findTheGfroHt(buffer.toString(), message); | |
if(!charMap.containsKey(""+gChar)){ | |
charMap.put(""+gChar, "g"); | |
} | |
//crack again | |
buffer = new StringBuffer(message.length()); | |
for(i=0;i<message.length();i++){ | |
char ch = message.charAt(i); | |
if(charMap.containsKey(""+ch)){ | |
buffer.append(charMap.get(""+ch)); | |
} | |
else{ | |
if(ch >= 97 && ch <= 122){ | |
buffer.append("_"); | |
} | |
else{ | |
buffer.append(ch); | |
} | |
} | |
} | |
return buffer.toString(); | |
} | |
private char findTheO(String cracked, String message){ | |
String target = " a "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
if(cracked.charAt(i) == 't'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else if(tempWord.length() == 3){ | |
if(cracked.charAt(i) == ' '){ | |
return message.charAt(i - 1); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
private char findTheA(String cracked, String message){ | |
String target = " a "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
if(cracked.charAt(i) != 'I'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += (""+cracked.charAt(i)); | |
return message.charAt(i-1); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
private char findTheDFromAnd(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
if(cracked.charAt(i) == 'a' || cracked.charAt(i) == 'A'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else if(tempWord.length() == 3){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else if(tempWord.length() == 4){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += (""+cracked.charAt(i)); | |
return message.charAt(i-1); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
private char findTheRFromAre(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
int[] frequencies = new int[256]; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
if(cracked.charAt(i) == 'a'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else if(tempWord.length() == 3){ | |
if(cracked.charAt(i) == 'e'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 4){ | |
if(cracked.charAt(i) == ' '){ | |
frequencies[message.charAt(i-2)]++; | |
tempWord = ""; | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
int greatest = 0; | |
int greatestIndex = -1; | |
//reason we use frequencies is because we want to avoid ate ale or | |
//something similar | |
for(i=0;i<frequencies.length;i++){ | |
if(greatest < frequencies[i]){ | |
greatest = frequencies[i]; | |
greatestIndex = i; | |
} | |
} | |
System.out.println("Greatest is " + greatestIndex + " with " + greatest + " count"); | |
if(greatestIndex > -1){ | |
return (char)greatestIndex; | |
} | |
return '_'; | |
} | |
private char findTheTFromThe(String cracked, String message){ | |
System.out.println("\nSearching for t...."); | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
int[] frequencies = new int[256]; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(i > 4 && cracked.charAt(i) == ' ' && cracked.charAt(i-1) == 'e' && | |
cracked.charAt(i-4) == ' '){ | |
frequencies[message.charAt(i-3)]++; | |
} | |
} | |
int greatest = 0; | |
int greatestIndex = -1; | |
//reason we use frequencies is because we want to avoid ate ale or | |
//something similar | |
for(i=0;i<frequencies.length;i++){ | |
if(greatest < frequencies[i]){ | |
greatest = frequencies[i]; | |
greatestIndex = i; | |
} | |
} | |
System.out.println("Greatest is " + greatestIndex + " with " + greatest + " count"); | |
if(greatestIndex > -1){ | |
return (char)greatestIndex; | |
} | |
return '_'; | |
} | |
private char findTheNFromAnd(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(i > 4 && cracked.charAt(i) == ' ' && cracked.charAt(i-3) == 'a' | |
&& cracked.charAt(i-1) == 'd'){ | |
return message.charAt(i-2); | |
} | |
} | |
return '_'; | |
} | |
private char findDoubleL(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(i == 3 && cracked.charAt(i) == ' ' && (cracked.charAt(i-3) == 'a' || cracked.charAt(i-3) == 'A') | |
&& (cracked.charAt(i-1) == cracked.charAt(i-2))){ | |
return message.charAt(i-2); | |
} | |
else if(i > 3 && cracked.charAt(i-4) == ' ' && cracked.charAt(i) == ' ' && (cracked.charAt(i-3) == 'a' || cracked.charAt(i-3) == 'A') | |
&& (cracked.charAt(i-1) == cracked.charAt(i-2))){ | |
return message.charAt(i-2); | |
} | |
} | |
return '_'; | |
} | |
private char findTheIFromIt(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
//ignore the word at | |
if(cracked.charAt(i) != 'a'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
if(cracked.charAt(i) == 't'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 3){ | |
if(cracked.charAt(i) == ' '){ | |
return message.charAt(i-2); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
private char findTheH(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
if(cracked.charAt(i) == 't'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else if(tempWord.length() == 3){ | |
if(cracked.charAt(i) == 'e'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 4){ | |
if(cracked.charAt(i) == ' '){ | |
tempWord += (""+cracked.charAt(i)); | |
return message.charAt(i-2); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
// | |
//words such as fight, caught, ought, sight weight... | |
//will have a likely hood of having g precedint ht | |
private char findTheGfroHt(String cracked, String message){ | |
String target = " the "; | |
String tempWord = ""; | |
int i = 0; | |
for(i=0;i<cracked.length();i++){ | |
//very brute force | |
if(tempWord.length() == 0){ | |
if(cracked.charAt(i) == 'h'){ | |
tempWord += " "; | |
} | |
} | |
else if(tempWord.length() == 1){ | |
//ignore the word at | |
if(cracked.charAt(i) == 't'){ | |
tempWord += (""+cracked.charAt(i)); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
else if(tempWord.length() == 2){ | |
if(cracked.charAt(i) == ' '){ | |
return message.charAt(i-3); | |
} | |
else{ | |
tempWord = ""; | |
} | |
} | |
} | |
return '_'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment