Skip to content

Commit

Permalink
Multiple fixes, option number 3 is XOR cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
Petros committed Mar 6, 2020
1 parent 7847a78 commit a546af9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
Binary file modified CryptanalysisHelper.jar
Binary file not shown.
17 changes: 8 additions & 9 deletions src/ColumnTransAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ static List<Integer> getDivisorsList(int n) {
* @param str The String to get all permutations of
* @param permutations The list to add all permutations to
*/
private static void getPermutations(String str, List<String> permutations) {
getPermutations(Constants.EMPTY_STRING, str, permutations);
private static void getStringPermutations(String str, List<String> permutations) {
getStringPermutations(Constants.EMPTY_STRING, str, permutations);
}

/**
Expand All @@ -31,7 +31,7 @@ private static void getPermutations(String str, List<String> permutations) {
* @param str The substring that will determine if a permutation has been formed (via its length)
* @param permutationList The list that all possible permutations will be added to.
*/
private static void getPermutations(String permutation, String str, List<String> permutationList) {
private static void getStringPermutations(String permutation, String str, List<String> permutationList) {

//Get length of 'str' (which is a substring of the originally parsed string)
int n = str.length();
Expand All @@ -40,7 +40,7 @@ private static void getPermutations(String permutation, String str, List<String>
permutationList.add(permutation);
else {
for (int i = 0; i < n; i++) {
getPermutations(
getStringPermutations(
permutation + str.charAt(i),
str.substring(0, i) + str.substring(i + 1, n),
permutationList);
Expand All @@ -50,15 +50,15 @@ private static void getPermutations(String permutation, String str, List<String>

private static List<String> getKeyPermutations(String str){
List<String> permutationList = new ArrayList<>();
getPermutations(str, permutationList);
getStringPermutations(str, permutationList);

return permutationList;
}

private static String getWordIndices(String word){
private static String getWordIndices(int len){
StringBuilder sb = new StringBuilder();

for(int i=0; i<word.length(); i++){
for(int i=0; i<len; i++){
sb.append(i);
}

Expand Down Expand Up @@ -134,8 +134,7 @@ static void applyColumnTrans(List<String> input){
System.out.println(startPermutationsMessage);
//If user says yes, start applying permutations. If user says no, continue loop
//Start applying all possible keys, one by one
String singleRow = anagram.substring(0, keyLength); //get first row of cipher
String rowAsIndices = getWordIndices(singleRow); //convert each char to an index indicator e.g."0123"
String rowAsIndices = getWordIndices(keyLength); //get string where each char is an index e.g."0123"
List<String> keys = getKeyPermutations(rowAsIndices); //get all permutations possible (a.k.a all keys)

// int allTimeHighestFitness = 0, loopHighestFitness = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Constants {
//final static String DIRECTORY = "static-data/spellcheckerDirectory";
final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
final static String LINE = "-------------------------------------------------------------------------------";
final static String OUTPUT_FILENAME = "output.txt";
final static String OUTPUT_FILENAME = "CryptanalysisHelper_output.txt";
final static String EMPTY_STRING = "";
final static String COMMA_SEPARATOR = ",";
final static String SPACE_SEPARATOR = " ";
Expand All @@ -21,7 +21,7 @@ class Constants {

//Available Tools Constants. The order of tools is important and unfortunately hardcoded below.
final static String TOOLS_AVAILABLE = "Caesar Cipher," +//Must be separated with comas, in correct order.
"Columnar Transposition Cipher,Simple Stream Cipher,Frequency Analysis";
"Columnar Transposition Cipher,Simple XOR Cipher,Frequency Analysis";
final static int CAESAR_CIPHER = 1;
final static int COLUMN_TRANSPOSITION_CIPHER = 2;
final static int ECBS_CIPHER = 3;
Expand All @@ -30,8 +30,8 @@ class Constants {
//Stream Cipher Constants
final static int MAX_SYMBOL_VALUE = 127; //currently only supports ascii (128 out of 917503)
final static int SYMBOLS_PER_ROW = 16; //from 0 to F (hexadecimal)
final static int MIN_UPPER_LETTERS_ROW_VALUE = 64;
final static int MAX_UPPER_LETTERS_ROW_VALUE = 95;
final static int MIN_UPPER_LETTERS_ROW_VALUE = 48;
final static int MAX_UPPER_LETTERS_ROW_VALUE = 126;

//Columnar Transposition Constants
final static int MAX_KEY_LENGTH_COL_TRANS = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void main(String[] args){
ColumnTransAPI.applyColumnTrans(input);
break;
case Constants.ECBS_CIPHER:
StreamAPI.applySimpleStream(input);
SimpleXorAPI.applySimpleXor(input);
break;
case Constants.FREQUENCY_ANALYSIS:
FreqAnalysisAPI.freqAnalysis(input);
Expand Down
9 changes: 4 additions & 5 deletions src/StreamAPI.java → src/SimpleXorAPI.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import java.util.*;
import java.util.stream.Collectors;

public class StreamAPI {
public class SimpleXorAPI {

//FIXME the character '_' needs to be any character, so it can be removed.
static String handleDecryptionLineFeed(String decryptedText){
String processed;
int platformLineFeedIndex;
Expand All @@ -27,7 +28,7 @@ else if (decryptedText.contains("\r")) {
return processed;
}

static void applySimpleStream(List<String> lines){
static void applySimpleXor(List<String> lines){
Scanner scanner = new Scanner(System.in);
ArrayList<String> plaintext = new ArrayList<>();
List<String> platformSpecificMsg;
Expand Down Expand Up @@ -55,9 +56,8 @@ static void applySimpleStream(List<String> lines){
System.out.println();
}
//char hint = 'L'; //decimal=76
System.out.print("Please enter the decimal value of the hint (see above table): ");
System.out.print("Please enter the decimal value of the first plaintext character (see above table): ");
int hint = scanner.nextInt(); //get hint from user in order to get key
//Use unix style '\n' as default string
int firstCPoint = platformSpecificMsg.get(0).codePointAt(0);
int key1 = (firstCPoint ^ hint);

Expand Down Expand Up @@ -181,7 +181,6 @@ else if (message.contains(Constants.UNIX_NEWLINE_SEPARATOR))

static void autoFindKey2(List<String> platformSpecificMsg, List<String> plaintextContainer, int key1){
HashSet<String> dictionary = SpellChecker.loadDictionary();
//Key = Key 2 (value of z in loop) Value = fitness score of message using specified key 2
LinkedHashMap<Integer, Integer> fitnessScores = new LinkedHashMap<>();
HashMap<String, Map.Entry<Integer, Integer>>finalistMessages = new HashMap<>();
HashMap<Integer, String> temporaryContainer = new HashMap<>();//holds key2 as key and decrypted message as value
Expand Down

0 comments on commit a546af9

Please sign in to comment.