Created
March 7, 2014 14:05
-
-
Save Vigenere36/9412090 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.*; | |
import java.io.*; | |
import java.util.HashSet; | |
import java.util.Iterator; | |
public class Vigenere { | |
public static HashSet dict_hash; | |
public static String encode(String toencode, String key) { | |
char[] toencodearr = toencode.toCharArray(); | |
char[] keyarr = key.toCharArray(); | |
int keylen = keyarr.length; | |
for(int i = 0; i < toencodearr.length; i++) { | |
int charval, keyval, newval; | |
if (Character.isUpperCase(toencodearr[i])) charval = (int)toencodearr[i] - 65; | |
else charval = (int)toencodearr[i] - 97; | |
if (Character.isUpperCase(keyarr[i % keylen])) keyval = (int)keyarr[i % keylen] - 65; | |
else keyval = (int)keyarr[i % keylen] - 97; | |
newval = (charval+keyval)%26; | |
toencodearr[i] = (char)(newval+65); | |
} | |
String toreturn = new String(toencodearr); | |
return toreturn; | |
} | |
public static String decode(String todecode, String key) { | |
char[] todecodearr = todecode.toCharArray(); | |
char[] keyarr = key.toCharArray(); | |
int keylen = keyarr.length; | |
for(int i = 0; i < todecodearr.length; i++) { | |
int charval, keyval, newval; | |
if (Character.isUpperCase(todecodearr[i])) charval = (int)todecodearr[i] - 65; | |
else charval = (int)todecodearr[i] - 97; | |
if (Character.isUpperCase(keyarr[i % keylen])) keyval = (int)keyarr[i % keylen] - 65; | |
else keyval = (int)keyarr[i % keylen] - 97; | |
newval = (charval-keyval)%26; | |
if (newval < 0) newval += 26; | |
todecodearr[i] = (char)(newval+65); | |
} | |
String toreturn = new String(todecodearr); | |
return toreturn; | |
} | |
public static void main(String[] args) { | |
//System.out.println(encode("TODAYISMYBIRTHDAY", "REDDIT")); | |
//System.out.println(decode("KSGDGBJQBEQKKLGDG", "REDDIT")); | |
dict_hash = new HashSet(100000); | |
//put contents of dictionary into a HashMap | |
try { | |
URL dict = new URL("http://docs.oracle.com/javase/tutorial/collections/interfaces/examples/dictionary.txt"); | |
BufferedReader input = new BufferedReader(new InputStreamReader(dict.openStream())); | |
String line; | |
while ((line = input.readLine()) != null) { | |
dict_hash.add(line); | |
} | |
} catch (Exception e) { | |
System.out.println("Some error has occurred"); | |
} | |
String todecode = "ZEJFOKHTMSRMELCPODWHCGAW"; | |
String testkey, decoded; | |
int counter=0; | |
Iterator<String> iter = dict_hash.iterator(); | |
while (iter.hasNext()) { | |
testkey = iter.next(); | |
if (testkey.length() <= 5) { | |
decoded = decode(todecode, testkey); | |
for (int i = 5; i < decoded.length(); i++) { | |
String substr = decoded.substring(0, i).toLowerCase(); | |
if (dict_hash.contains(substr)) System.out.println(testkey + " : " + decoded); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment