Skip to content

Commit 75b1f0e

Browse files
authored
Add files via upload
1 parent b99c559 commit 75b1f0e

File tree

18 files changed

+19557
-0
lines changed

18 files changed

+19557
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import edu.duke.*;
2+
3+
public class CaesarCipher
4+
{
5+
public String encrypt(String input, int key)
6+
{
7+
StringBuilder encrypted = new StringBuilder(input);
8+
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9+
String shiftedAlphabet = alphabet.substring(key) + alphabet.substring(0, key);
10+
11+
for(int i=0; i<encrypted.length(); i++)
12+
{
13+
char currChar = encrypted.charAt(i);
14+
int idx = alphabet.indexOf(currChar);
15+
16+
if(idx != -1) //...currChar is an ALPHABET...//
17+
{
18+
char newChar = shiftedAlphabet.charAt(idx);
19+
encrypted.setCharAt(i, newChar);
20+
}
21+
22+
else //...for lowercase...//
23+
{
24+
currChar = Character.toUpperCase(currChar);
25+
idx = alphabet.indexOf(currChar);
26+
if(idx != -1)
27+
{
28+
char newChar = shiftedAlphabet.charAt(idx);
29+
newChar = Character.toLowerCase(newChar);
30+
encrypted.setCharAt(i, newChar);
31+
}
32+
}
33+
}
34+
return encrypted.toString();
35+
}
36+
37+
public String encryptTwoKeys (String input, int key1, int key2)
38+
{
39+
StringBuilder encrypted = new StringBuilder(input);
40+
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
41+
String shiftedAlphabetKey1 = alphabet.substring(key1) + alphabet.substring(0, key1);
42+
String shiftedAlphabetKey2 = alphabet.substring(key2) + alphabet.substring(0, key2);
43+
44+
for(int i=0; i<encrypted.length(); i++)
45+
{
46+
if(i%2==0)
47+
{
48+
char currChar = encrypted.charAt(i);
49+
int idx = alphabet.indexOf(currChar);
50+
51+
if(idx != -1) //...currChar is an ALPHABET...//
52+
{
53+
char newChar = shiftedAlphabetKey1.charAt(idx);
54+
encrypted.setCharAt(i, newChar);
55+
}
56+
57+
else //...for lowercase...//
58+
{
59+
currChar = Character.toUpperCase(currChar);
60+
idx = alphabet.indexOf(currChar);
61+
if(idx != -1)
62+
{
63+
char newChar = shiftedAlphabetKey1.charAt(idx);
64+
newChar = Character.toLowerCase(newChar);
65+
encrypted.setCharAt(i, newChar);
66+
}
67+
}
68+
}
69+
else
70+
{
71+
char currChar = encrypted.charAt(i);
72+
int idx = alphabet.indexOf(currChar);
73+
74+
if(idx != -1) //...currChar is an ALPHABET...//
75+
{
76+
char newChar = shiftedAlphabetKey2.charAt(idx);
77+
encrypted.setCharAt(i, newChar);
78+
}
79+
80+
else //...for lowercase...//
81+
{
82+
currChar = Character.toUpperCase(currChar);
83+
idx = alphabet.indexOf(currChar);
84+
if(idx != -1)
85+
{
86+
char newChar = shiftedAlphabetKey2.charAt(idx);
87+
newChar = Character.toLowerCase(newChar);
88+
encrypted.setCharAt(i, newChar);
89+
}
90+
}
91+
}
92+
93+
}
94+
return encrypted.toString();
95+
}
96+
97+
//////////////////////////////////////////////////////////////////////////////////////
98+
public void testCaesar()
99+
{
100+
//FileResource fr = new FileResource();
101+
//String message = fr.asString();
102+
//String encrypted = encrypt(message, 23);
103+
//System.out.println("key is " + 23 + "\n" + encrypted);
104+
//System.out.println("ENCRYPTED: " + encrypt("At noon be in the conference room with your hat on for a surprise party. YELL LOUD!",15));
105+
System.out.println("ENCRYPTED: " + encryptTwoKeys("At noon be in the conference room with your hat on for a surprise party. YELL LOUD!", 8, 21));
106+
}
107+
108+
//////////////////////////////////////////////////////////////////////////////////////
109+
public static void main (String[] args)
110+
{
111+
CaesarCipher CIPHER = new CaesarCipher();
112+
CIPHER.testCaesar();
113+
}
114+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import edu.duke.*;
2+
3+
public class WordPlay
4+
{
5+
public boolean isVowel(char ch)
6+
{
7+
String vowels = "aeiouAEIOU";
8+
if(vowels.indexOf(ch) != -1)
9+
{
10+
return true;
11+
}
12+
return false;
13+
}
14+
15+
public String replaceVowels (String phrase, char ch)
16+
{
17+
StringBuilder result = new StringBuilder(phrase);
18+
for(int i=0; i<result.length(); i++)
19+
{
20+
if(isVowel(result.charAt(i)))
21+
{
22+
result.setCharAt(i,ch);
23+
}
24+
}
25+
return result.toString();
26+
}
27+
28+
public String emphasize (String phrase, char ch)
29+
{
30+
StringBuilder result = new StringBuilder(phrase);
31+
for(int i=0; i<result.length(); i++)
32+
{
33+
if(result.charAt(i) == ch || result.charAt(i) == Character.toUpperCase(ch))
34+
{
35+
if(i%2 == 0) //...even index, but odd number location
36+
{
37+
result.setCharAt(i,'*');
38+
}
39+
else
40+
{
41+
result.setCharAt(i,'+');
42+
}
43+
}
44+
}
45+
return result.toString();
46+
}
47+
48+
//////////////////////////////////////////////////////////////////////////////////////
49+
public void testisVowel()
50+
{
51+
System.out.println("F is vowel..?" + isVowel('a'));
52+
}
53+
public void testreplaceVowels()
54+
{
55+
System.out.println("Hello World replaced vowels: " + replaceVowels("Hello World",'*'));
56+
}
57+
public void testemphasize()
58+
{
59+
System.out.println("Mary Bella Abracadabra: " + emphasize("Mary Bella Abracadabra",'a'));
60+
}
61+
62+
//////////////////////////////////////////////////////////////////////////////////////
63+
public static void main (String[] args)
64+
{
65+
WordPlay CIPHER = new WordPlay();
66+
CIPHER.testisVowel();
67+
CIPHER.testreplaceVowels();
68+
CIPHER.testemphasize();
69+
}
70+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//import edu.duke.FileResource;
2+
3+
public class CaesarBreaker
4+
{
5+
public String decrypt(String encrypted)
6+
{
7+
CaesarCipher cc = new CaesarCipher();
8+
int[] freqs = countLetters(encrypted);
9+
int maxDex = maxIndex(freqs);
10+
int dkey = maxDex-4;
11+
if(maxDex < 4)
12+
{
13+
dkey = 26-(4-maxDex);
14+
}
15+
System.out.println("Key is " + dkey);
16+
return cc.encrypt(encrypted, 26-dkey);
17+
}
18+
19+
public int[] countLetters(String message)
20+
{
21+
String alphabet = "abcdefghijklmnopqrstuvwxyz";
22+
int[]counts = new int[26];
23+
for(int i=0; i<message.length(); i++)
24+
{
25+
char ch = Character.toLowerCase(message.charAt(i));
26+
int dex = alphabet.indexOf(ch);
27+
if(dex != -1)
28+
{
29+
counts[dex]+=1;
30+
}
31+
}
32+
return counts;
33+
}
34+
35+
public int maxIndex(int[] counts)
36+
{
37+
int max = 0;
38+
for(int i=1; i<counts.length; i++)
39+
{
40+
if(counts[i] > counts[max])
41+
{
42+
max = i;
43+
}
44+
}
45+
return max;
46+
}
47+
//////////////////////////////////////////////////////////////////////////////////////////////////////
48+
public String halfOfString (String message, int start)
49+
{
50+
String res = "";
51+
for(int i=start; i<message.length(); i+=2)
52+
{
53+
res += message.charAt(i);
54+
}
55+
return res;
56+
}
57+
public int getKey(String s)
58+
{
59+
int[] freqs = countLetters(s);
60+
int maxDex = maxIndex(freqs);
61+
int dkey = maxDex-4;
62+
if(maxDex < 4)
63+
{
64+
dkey = 26-(4-maxDex);
65+
}
66+
return dkey;
67+
}
68+
69+
public String decryptTwoKeys (String encrypted)
70+
{
71+
String encrypted1 = halfOfString(encrypted, 0);
72+
String encrypted2 = halfOfString(encrypted, 1);
73+
int key1 = getKey(encrypted1);
74+
int key2 = getKey(encrypted2);
75+
76+
CaesarCipher cc = new CaesarCipher();
77+
return cc.encryptTwoKeys(encrypted, 26-key1, 26-key2);
78+
79+
}
80+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81+
void testCaesarBreaker()
82+
{
83+
System.out.println("DECRYPTED: " + decrypt("Yjhi p ithi higxcv lxiw adih du ttttttttttttttttth"));
84+
}
85+
void testhalfOfString()
86+
{
87+
System.out.println("HALF: " + halfOfString("Qbkm Zgis", 1));
88+
}
89+
void testdecryptTwoKeys()
90+
{
91+
System.out.println("HALF: " + decryptTwoKeys("Gwpv c vbuq pvokki yfve iqqu qc bgbgbgbgbgbgbgbgbu"));
92+
}
93+
94+
//////////////////////////////////////////////////////////////////////
95+
public static void main (String[] args)
96+
{
97+
CaesarBreaker TESTCASE = new CaesarBreaker();
98+
//TESTCASE.testCaesarBreaker();
99+
//TESTCASE.testhalfOfString();
100+
TESTCASE.testdecryptTwoKeys();
101+
}
102+
}

0 commit comments

Comments
 (0)