Skip to content

Commit bb6132c

Browse files
authored
Add files via upload
1 parent 81ebc01 commit bb6132c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3703001
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import edu.duke.*;
2+
3+
public class CaesarCipher {
4+
private String alphabet;
5+
private String shiftedAlphabet;
6+
private int theKey;
7+
8+
public CaesarCipher(int key) {
9+
theKey = key;
10+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
11+
shiftedAlphabet = alphabet.substring(key) +
12+
alphabet.substring(0,key);
13+
alphabet = alphabet + alphabet.toLowerCase();
14+
shiftedAlphabet = shiftedAlphabet + shiftedAlphabet.toLowerCase();
15+
}
16+
17+
private char transformLetter(char c, String from, String to) {
18+
int idx = from.indexOf(c);
19+
if (idx != -1) {
20+
return to.charAt(idx);
21+
}
22+
return c;
23+
}
24+
25+
public char encryptLetter(char c) {
26+
return transformLetter(c, alphabet, shiftedAlphabet);
27+
}
28+
29+
public char decryptLetter(char c) {
30+
return transformLetter(c, shiftedAlphabet, alphabet);
31+
}
32+
33+
private String transform(String input, String from, String to){
34+
StringBuilder sb = new StringBuilder(input);
35+
for (int i = 0; i < sb.length(); i++) {
36+
char c = sb.charAt(i);
37+
c = transformLetter(c, from, to);
38+
sb.setCharAt(i, c);
39+
}
40+
return sb.toString();
41+
}
42+
43+
public String encrypt(String input) {
44+
return transform(input, alphabet, shiftedAlphabet);
45+
}
46+
47+
public String decrypt(String input) {
48+
return transform(input, shiftedAlphabet, alphabet);
49+
}
50+
51+
public String toString() {
52+
return "" + theKey;
53+
}
54+
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import edu.duke.*;
2+
3+
public class CaesarCracker {
4+
char mostCommon;
5+
6+
public CaesarCracker() {
7+
mostCommon = 'e';
8+
}
9+
10+
public CaesarCracker(char c) {
11+
mostCommon = c;
12+
}
13+
14+
public int[] countLetters(String message){
15+
String alph = "abcdefghijklmnopqrstuvwxyz";
16+
int[] counts = new int[26];
17+
for(int k=0; k < message.length(); k++){
18+
int dex = alph.indexOf(Character.toLowerCase(message.charAt(k)));
19+
if (dex != -1){
20+
counts[dex] += 1;
21+
}
22+
}
23+
return counts;
24+
}
25+
26+
public int maxIndex(int[] vals){
27+
int maxDex = 0;
28+
for(int k=0; k < vals.length; k++){
29+
if (vals[k] > vals[maxDex]){
30+
maxDex = k;
31+
}
32+
}
33+
return maxDex;
34+
}
35+
36+
public int getKey(String encrypted){
37+
int[] freqs = countLetters(encrypted);
38+
int maxDex = maxIndex(freqs);
39+
int mostCommonPos = mostCommon - 'a';
40+
int dkey = maxDex - mostCommonPos;
41+
if (maxDex < mostCommonPos) {
42+
dkey = 26 - (mostCommonPos-maxDex);
43+
}
44+
return dkey;
45+
}
46+
47+
public String decrypt(String encrypted){
48+
int key = getKey(encrypted);
49+
CaesarCipher cc = new CaesarCipher(key);
50+
return cc.decrypt(encrypted);
51+
52+
}
53+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.*;
2+
import edu.duke.*;
3+
4+
public class VigenereBreaker
5+
{
6+
public String sliceString(String message, int whichSlice, int totalSlices)
7+
{
8+
StringBuilder result = new StringBuilder();
9+
for(int i=whichSlice; i<message.length(); i+=totalSlices)
10+
{
11+
result.append(message.charAt(i));
12+
}
13+
return result.toString();
14+
}
15+
16+
public int[] tryKeyLength(String encrypted, int klength, char mostCommon)
17+
{
18+
CaesarCracker CC = new CaesarCracker(mostCommon);
19+
int[] key = new int[klength];
20+
for(int i=0; i<klength; i++)
21+
{
22+
String slice = sliceString(encrypted, i, klength);
23+
int resKey = CC.getKey(slice);
24+
System.out.println("KEY: " + resKey);
25+
key[i] = resKey;
26+
}
27+
28+
return key;
29+
}
30+
31+
public void breakVigenere ()
32+
{
33+
FileResource fr = new FileResource();
34+
String FILE = fr.asString();
35+
int[]key = tryKeyLength(FILE, 4, 'e');
36+
VigenereCipher VC = new VigenereCipher(key);
37+
String decrypt = VC.decrypt(FILE);
38+
System.out.println("DECRYPTED MESSAGE: " + decrypt);
39+
}
40+
41+
///////////////////////////////////////////////////////////
42+
public static void main (String []args)
43+
{
44+
VigenereBreaker VC = new VigenereBreaker();
45+
VC.breakVigenere();
46+
}
47+
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import edu.duke.*;
2+
import java.util.*;
3+
4+
public class VigenereCipher {
5+
CaesarCipher[] ciphers;
6+
7+
public VigenereCipher(int[] key) {
8+
ciphers = new CaesarCipher[key.length];
9+
for (int i = 0; i < key.length; i++) {
10+
ciphers[i] = new CaesarCipher(key[i]);
11+
}
12+
}
13+
14+
public String encrypt(String input) {
15+
StringBuilder answer = new StringBuilder();
16+
int i = 0;
17+
for (char c : input.toCharArray()) {
18+
int cipherIndex = i % ciphers.length;
19+
CaesarCipher thisCipher = ciphers[cipherIndex];
20+
answer.append(thisCipher.encryptLetter(c));
21+
i++;
22+
}
23+
return answer.toString();
24+
}
25+
26+
public String decrypt(String input) {
27+
StringBuilder answer = new StringBuilder();
28+
int i = 0;
29+
for (char c : input.toCharArray()) {
30+
int cipherIndex = i % ciphers.length;
31+
CaesarCipher thisCipher = ciphers[cipherIndex];
32+
answer.append(thisCipher.decryptLetter(c));
33+
i++;
34+
}
35+
return answer.toString();
36+
}
37+
38+
public String toString() {
39+
return Arrays.toString(ciphers);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)