Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
Implement method as Vigenere parameter and data analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsingh87 committed Mar 4, 2023
1 parent 91e40b6 commit 12db59a
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 32 deletions.
28 changes: 27 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,27 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "DataVisualizer",
"request": "launch",
"mainClass": "dataanalysis.DataVisualizer",
"projectName": "com.varunsingh.frequencyanalysissimulator"
},
{
"type": "java",
"name": "DataCollector",
"request": "launch",
"mainClass": "dataanalysis.DataCollector",
"projectName": "com.varunsingh.frequencyanalysissimulator"
},
{
"type": "java",
"name": "Launch DataPopulater",
"request": "launch",
"mainClass": "dataanalysis.DataPopulater",
"projectName": "com.varunsingh.frequencyanalysissimulator"
"projectName": "com.varunsingh.frequencyanalysissimulator",
"args": "${input:method}"
},
{
"type": "java",
Expand Down Expand Up @@ -53,6 +68,17 @@
"id": "text",
"description": "Choose the input for the program",
"type": "promptString"
},
{
"id": "method",
"description": "Choose the key length calculation algorithm",
"options": [
"kasiski",
"friedman",
"index_of_coincidence",
"all"
],
"type": "pickString"
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ or for short, use `mvn`
Run the following command for data collection of a single input (~480 runs/data points):

```
mvn compile exec:java -Dexec.mainClass="dataanalysis.DataAnalysis"
mvn compile exec:java -Dexec.mainClass="dataanalysis.DataCollector"
```

## Process
Expand Down Expand Up @@ -67,7 +67,7 @@ mvn compile exec:java -Dexec.mainClass="dataanalysis.DataAnalysis"

Copyright © 2023 Varun Singh

## Acknowledgments
## Acknowledgments and Sources of Research

* Inspiration from _The Code Book_ by Simon Singh
* _The Cryptanalyst_ by Helen Fouche Gaines
Expand Down
39 changes: 39 additions & 0 deletions data/inputs/2D.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
She came at last to the end of the line of tents. She stopped and looked
back. A glow of light was on the camp, and the soft overtone of a
multitude of speakers. Now and then a harsher voice cut through. The
smell of smoke filled the air. Someone played a harmonica softly, trying
for an effect, one phrase over and over. Ma stepped in among the willows beside the stream. She moved off the
trail and waited, silently, listening to hear any possible follower. A
man walked down the trail toward the camp, boosting his suspenders and
buttoning his jeans as he went. Ma sat very still, and he passed on
without seeing her. She waited five minutes and then she stood up and
crept on up the trail beside the stream. She moved quietly, so quietly
that she could hear the murmur of the water above her soft steps on the
willow leaves. Trail and stream swung to the left and then to the right
again until they neared the highway. In the gray starlight she could see
the embankment and the black round hole of the culvert where she always
left Tom’s food. She moved forward cautiously, thrust her package into
the hole, and took back the empty tin plate which was left there. She
crept back among the willows, forced her way into a thicket, and sat
down to wait. Through the tangle she could see the black hole of the
culvert. She clasped her knees and sat silently. In a few moments the
thicket crept to life again. The field mice moved cautiously over the
leaves. A skunk padded heavily and unself-consciously down the trail,
carrying a faint effluvium with him. And then a wind stirred the willows
delicately, as though it tested them, and a shower of golden leaves
coasted down to the ground. Suddenly a gust boiled in and racked the
trees, and a cricking downpour of leaves fell. Ma could feel them on her
hair and on her shoulders. Over the sky a plump black cloud moved,
erasing the stars. The fat drops of rain scattered down, splashing
loudly on the fallen leaves, and the cloud moved on and unveiled the
stars again. Ma shivered. The wind blew past and left the thicket quiet,
but the rushing of the trees went on down the stream. From back at the
camp came the thin penetrating tone of a violin feeling about for a
tune. Ma heard a stealthy step among the leaves far to her left, and she grew
tense. She released her knees and straightened her head, the better to
hear. The movement stopped, and after a long moment began again. A vine
rasped harshly on the dry leaves. Ma saw a dark figure creep into the
open and draw near to the culvert. The black round hole was obscured for
a moment, and then the figure moved back. She called softly, “Tom!" The
figure stood still, so still, so low to the ground that it might have
been a stump. She called again, “Tom, oh, Tom!" Then the figure moved.
6 changes: 3 additions & 3 deletions data/inputs/3A.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Swich sorwe he maketh, that the grete tour
Resouneth of his youling and clamour.
The pure fettres on his shines grete
Weren of his bittre salte teres wete.
Allas! quod he, Arcita, cosin myn,
'Allas!' quod he, 'Arcita, cosin myn,
Of al our stryf, God woot, the fruyt is thyn.
Thow walkest now in Thebes at thy large,
And of my wo thou yevest litel charge.
Expand All @@ -21,12 +21,12 @@ More than is myn, that sterve here in a cage.
For I mot wepe and wayle, whyl I live,
With al the wo that prison may me yive,
And eek with peyne that love me yiveth also,
That doubleth al my torment and my wo.
That doubleth al my torment and my wo.'
Ther-with the fyr of Ielousye up-sterte
With-inne his brest, and hente him by the herte
So woodly, that he lyk was to biholde
The box-tree, or the asshen dede and colde.
Tho seyde he; O cruel goddes, that governe
Tho seyde he; 'O cruel goddes, that governe
This world with binding of your word eterne,
And wryten in the table of athamaunt
Your parlement, and your eterne graunt,
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dataanalysis/DataCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.FileWriter;
import java.io.IOException;

import frequencyanalysissimulator.crypto.KeyLengthMethod;
import frequencyanalysissimulator.crypto.Vigenere;

public class DataCollector {
Expand All @@ -13,10 +14,11 @@ public class DataCollector {
* @param args
* arg[0]: Input must be one line
* arg[1]: Trial Id for output file
* arg[2]: (Optional): key
* arg[2]: (Optional) key length calculation algorithm
* arg[3]: (Optional): key
*/
public static void main(String[] args) {
final String key = args.length > 2 ? args[2] : "DONQUIXOTECOYOTEWILL";
final String key = args.length > 3 ? args[3] : "DONQUIXOTECOYOTEWILL";
String expectedText = args[0].toUpperCase();
String output = String.format("Len,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,Avg(%s)\n", args[1]);

Expand All @@ -27,7 +29,8 @@ public static void main(String[] args) {
String input = expectedText.substring(0, cipherlen);
String subKey = key.substring(0, i);
String ciphertext = Vigenere.encrypt(input, subKey);
Vigenere v = new Vigenere(ciphertext);
Vigenere v = args[2] != null ? new Vigenere(ciphertext, KeyLengthMethod.valueOf(args[2].toUpperCase()))
: new Vigenere(ciphertext);
String decryptedText = v.decrypt();
double accuracy = percentageSimilarity(decryptedText, input);

Expand All @@ -39,7 +42,7 @@ public static void main(String[] args) {
}

try (FileWriter writer = new FileWriter(
new File(String.format("data/generatedOutputs/Trial %s.csv", args[1])))) {
new File(String.format("data/outputs/%s/Trial %s.csv", args[2], args[1])))) {
writer.append(output);
} catch (IOException e) {
e.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dataanalysis/DataFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static void main(String... args) {
try {
String expectedText = Files.readString(Path.of(String.format("data/inputs/%s.txt", args[0])));
expectedText = expectedText.replace(System.getProperty("line.separator"), " ");
DataCollector.main(new String[] { expectedText, args[0] });
DataCollector.main(new String[] { expectedText, args[0], args[1] });
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/dataanalysis/DataPopulater.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.io.File;

import frequencyanalysissimulator.crypto.KeyLengthMethod;

/**
* Runs DataAnalysis without arguments, taking contents of every input file and feeding this to
* DataFileWriter, which in return feeds the content to DataAnalysis
* Used for populating all data, takes ~20 seconds to run and collects 5000 data points
*/
public class DataPopulater {
public static void main(String[] args) throws InterruptedException {
DataPopulater(String method) {
File inputFolder = new File("data/inputs");
File[] allInputs = inputFolder.listFiles();

Expand All @@ -17,10 +19,26 @@ public static void main(String[] args) throws InterruptedException {

String fileNameWithoutExtension = allInputs[i].getName().substring(0,
allInputs[i].getName().lastIndexOf("."));
DataFileReader.main(fileNameWithoutExtension);
DataFileReader.main(fileNameWithoutExtension, method);

System.out.print("\rData collection complete. Moving to next file...\n");
}
}

/**
*
* @param args
* args[0]: key length calculation algorithm
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
if (args[0] == null || args[0].equals("all")) {
for (KeyLengthMethod method : KeyLengthMethod.values()) {
new DataPopulater(method.name());
}
} else
new DataPopulater(args[0]);

System.out.print("\rData population complete");
}
}
11 changes: 10 additions & 1 deletion src/main/java/dataanalysis/DataVisualizer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package dataanalysis;

import java.util.Arrays;

import javax.swing.JFrame;

public class DataVisualizer {
public DataVisualizer() {

JFrame t = new JFrame();
t.setDefaultCloseOperation(0);
GraphPanel g = new GraphPanel(Arrays.asList(10.0, 20.0, 30.0, 40.0, 56.0, 10.0, 20.0, 30.0));
t.setVisible(true);
t.setSize(500, 500);
t.add(g);
}

public static void main(String[] args) {
Expand Down
Loading

0 comments on commit 12db59a

Please sign in to comment.