Skip to content

Commit

Permalink
Fix file referencing, jar should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsos17 committed May 9, 2019
1 parent 3426aaf commit 6eb0e6d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 571 deletions.
501 changes: 0 additions & 501 deletions CryptoApp/501_sample_ciphertexts.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public AttackTranspositionCipher() {
this.combi = new Combinatorics();
this.alphabet = "abcdefghijklmnopqrstuvwxyz";
this.ciph = new TranspositionCipher();
this.quad = new Ngrams(4, "src/main/resources/english_quadgrams.txt");
this.quad = new Ngrams(4);
this.quad.readInputStream(getClass().getResourceAsStream("/english_quadgrams.txt"));
this.hillClimber = new HillClimber(this.quad);
}

Expand Down
40 changes: 17 additions & 23 deletions CryptoApp/src/main/java/crypto/cryptanalysis/Ngrams.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
package crypto.cryptanalysis;

import crypto.datastructures.HashTable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
Expand All @@ -19,7 +23,6 @@
public class Ngrams {

private final int n;
private final String filename;
private long sampleSize;
private HashTable<String, Long> ngramStats;

Expand All @@ -28,35 +31,25 @@ public class Ngrams {
* @param n The desired n-gram substring length i.e. monogram is 1-gram and
* is related to frequencies of single letter in a text, bigram (2-gram) is
* related to frequencies of letter pairs in a text etc.
* @param filename A text file which contains data related to the specified
* n-gram. For each row the file should have the n-gram string, then a
* single space and finally the count of said n-grams in sample text. If the
* file violates this rule, then the results most likely will not be
* accurate.
*/
public Ngrams(int n, String filename) {
public Ngrams(int n) {
this.n = n;
this.filename = filename;
this.sampleSize = 0;
this.ngramStats = new HashTable<>();
readFile();
}

public int getN() {
return n;
}

/**
* Reads the n-gram statistical data from a text file and then stores it in
* a hash table. The text file is provided as a constructor parameter.
*
* @return The sample size of the statistical data contained in the text
* file
*/
private long readFile() {
try (Scanner scanner = new Scanner(new File(this.filename))) {
while (scanner.hasNextLine()) {
String[] line = scanner.nextLine().split(" ");
public void readInputStream(InputStream stream) {
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr);

try {
String lineString = br.readLine();
while (lineString != null) {
String[] line = lineString.split(" ");
if (line.length >= 2) {
long frequency = 0;
try {
Expand All @@ -67,12 +60,13 @@ private long readFile() {
System.err.println("The file is corrupted");
}
}

lineString = br.readLine();
}
} catch (FileNotFoundException exc) {
System.out.println("File read successfully");
} catch (IOException exc) {
System.err.println("File not found");
}

return this.sampleSize;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import crypto.cryptanalysis.IndexOfCoincidence;
import crypto.cryptanalysis.Ngrams;
import crypto.helpers.CharacterValue;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -73,7 +76,9 @@ public void start(Stage stage) throws Exception {
keyedVig = new KeyedVigenereCipher("");
autokeyVig = new AutokeyVigenereCipher();
transposition = new TranspositionCipher();
quadgrams = new Ngrams(4, "src/main/resources/english_quadgrams.txt");

quadgrams = new Ngrams(4);
quadgrams.readInputStream(getClass().getResourceAsStream("/english_quadgrams.txt"));
hillClimber = new HillClimber(quadgrams);
freq = new FrequencyAnalysis();
ic = new IndexOfCoincidence(freq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class HillClimberTest {

@Before
public void setUp() {
this.quad = new Ngrams(4, "src/main/resources/english_quadgrams.txt");
this.quad = new Ngrams(4);
this.quad.readInputStream(getClass().getResourceAsStream("/english_quadgrams.txt"));
this.hc = new HillClimber(quad);
}

Expand Down
59 changes: 19 additions & 40 deletions CryptoApp/src/test/java/crypto/cryptanalysis/NgramsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
*/
package crypto.cryptanalysis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;

/**
*
Expand All @@ -39,25 +36,14 @@ public void restoreStreams() {
System.setErr(originalErr);
}

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

private File testFile;
private Ngrams ngrams;

@Before
public void setUp() throws Exception {
this.testFile = testFolder.newFile("test_ngram.txt");
try (FileWriter file = new FileWriter(this.testFile.getAbsolutePath())) {
file.write("THET 3597105\nHEIR 2630839\nTOBE 1850003\nTHRO 1239338");

}
this.ngrams = new Ngrams(4, this.testFile.getAbsolutePath());
}

@After
public void tearDown() {
testFile.delete();
String str = "THET 3597105\nHEIR 2630839\nTOBE 1850003\nTHRO 1239338";
byte[] bytes = str.getBytes("UTF-8");
this.ngrams = new Ngrams(4);
this.ngrams.readInputStream(new ByteArrayInputStream(bytes));
}

@Test
Expand Down Expand Up @@ -112,39 +98,32 @@ public void fitnessTest3() {

@Test
public void corruptedFileTest1() throws Exception {
File testFile2 = testFolder.newFile("test_2.txt");
try (FileWriter file = new FileWriter(testFile2.getAbsolutePath())) {
file.write("THET TEST\n");
}
String str = "THET TEST\n";
byte[] bytes = str.getBytes("UTF-8");

Ngrams ngrams2 = new Ngrams(4, testFile2.getAbsolutePath());
Ngrams ngrams2 = new Ngrams(4);
ngrams2.readInputStream(new ByteArrayInputStream(bytes));
assertEquals("The file is corrupted\n", errContent.toString());
testFile2.delete();
}

@Test
public void corruptedFileTest2() throws Exception {
File testFile2 = testFolder.newFile("test_2.txt");
try (FileWriter file = new FileWriter(testFile2.getAbsolutePath())) {
file.write("THET\n");
}
String str = "THET TEST\n";
byte[] bytes = str.getBytes("UTF-8");

Ngrams ngrams2 = new Ngrams(4, testFile2.getAbsolutePath());
Ngrams ngrams2 = new Ngrams(4);
ngrams2.readInputStream(new ByteArrayInputStream(bytes));
assertEquals(0, ngrams2.getSampleSize());
testFile2.delete();
}

@Test
public void nonExistentFileTest() {
Ngrams ngrams2 = new Ngrams(4, "test3.txt");
assertEquals("File not found\n", errContent.toString());
}

@Test
public void monoBiTrigramsTest1() {
Ngrams mono = new Ngrams(1, "src/main/resources/english_monograms.txt");
Ngrams bi = new Ngrams(2, "src/main/resources/english_bigrams.txt");
Ngrams tri = new Ngrams(3, "src/main/resources/english_trigrams.txt");
Ngrams mono = new Ngrams(1);
mono.readInputStream(getClass().getResourceAsStream("/english_monograms.txt"));
Ngrams bi = new Ngrams(2);
bi.readInputStream(getClass().getResourceAsStream("/english_bigrams.txt"));
Ngrams tri = new Ngrams(3);
tri.readInputStream(getClass().getResourceAsStream("/english_trigrams.txt"));

assertEquals(1, mono.getN());
assertEquals(2, bi.getN());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
Expand Down Expand Up @@ -53,7 +51,8 @@ public static void main(String[] args) {
int algoRuns = 10;
int iterations = 500;
System.out.println("Encryption done");
Ngrams ngrams = new Ngrams(4, "src/main/resources/english_quadgrams.txt");
Ngrams ngrams = new Ngrams(4);
ngrams.readInputStream(TextProcessor.class.getResourceAsStream("/english_quadgrams.txt"));
HillClimber hill = new HillClimber(ngrams);
ArrayList<String> foundKeys = cryptanalysisTransposition("src/main/resources/501_sample_ciphertexts.txt", key.length(), hill, algoRuns, iterations);

Expand Down Expand Up @@ -165,7 +164,7 @@ public static void main(String[] args) {
} else {
// System.out.println("Len " + (i * 50 + 1) + "-" + ((i + 2) * 50) + " Correct: " + counts[i] + "/" + samples[i].size());
}

// System.out.println(counts[i] + " " + samples[i].size());
System.out.println(counts[i]);
// System.out.printf("%.1f", avgs[i]);
Expand Down

0 comments on commit 6eb0e6d

Please sign in to comment.