Created
June 9, 2010 15:01
-
-
Save sebjacobs/431596 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.io.Console; | |
public class SubCipher{ | |
private String keyPhrase,keyCipher; | |
public String alphabet=""; | |
public SubCipher(String key){ | |
keyCipher=""; | |
keyPhrase=key; | |
this.generateAlphabet(); | |
this.generateCipher(); | |
} | |
public void generateAlphabet(){ | |
//used to generate string of alphabet a to z | |
for(char c = 'a'; c <= 'z'; c++){ | |
alphabet+=c; | |
} | |
} | |
private void generateCipher(){ | |
// remove spaces | |
String [] phraseNoSpaces = keyPhrase.split(" "); | |
for(int i=0;i<phraseNoSpaces.length;i++){ | |
keyCipher+=phraseNoSpaces[i]; | |
} | |
//remove duplicate characters | |
String [] cipherCharRemoved; | |
for(int i=0;i<keyCipher.length();i++){ | |
String firstChar=keyCipher.substring(i,(i+1)); | |
String subString1= keyCipher.substring(0,(i+1)); | |
String subString2 = keyCipher.substring((i+1),keyCipher.length()); | |
cipherCharRemoved = subString2.split(firstChar); | |
keyCipher=subString1; | |
for(int j=0;j<cipherCharRemoved.length;j++){ | |
keyCipher+=cipherCharRemoved[j]; | |
} | |
} | |
for(char c = 'z'; c >= 'a'; c--){ //loop through alphabet, adding characters which aren't in the cipherKey | |
if(keyCipher.indexOf(c)==-1){ | |
keyCipher+=c; | |
} | |
} | |
} | |
public String encrypt(String plainText){ // encrypts plaintext with generated cipher | |
String cipherText=""; | |
for(int i=0;i<plainText.length();i++){ | |
char currentChar=plainText.charAt(i); | |
if(Character.isLetter(currentChar)){ | |
if(Character.isUpperCase(currentChar)){ | |
cipherText+=String.valueOf(Character.toUpperCase(keyCipher.charAt(alphabet.indexOf(Character.toLowerCase(currentChar))))); | |
} | |
else{ | |
cipherText+=String.valueOf(keyCipher.charAt(alphabet.indexOf(currentChar))); | |
} | |
} | |
else{ | |
cipherText+=String.valueOf(currentChar); | |
} | |
} | |
return cipherText; | |
} | |
public String decrypt(String cipherText){ // decrypts ciphertext with generated cipher | |
String plainText=""; | |
for(int i=0;i<cipherText.length();i++){ | |
char currentChar=cipherText.charAt(i); | |
if(Character.isLetter(currentChar)){ | |
if(Character.isUpperCase(currentChar)){ | |
plainText+=String.valueOf((Character.toUpperCase(alphabet.charAt(keyCipher.indexOf(Character.toLowerCase(currentChar)))))); | |
} | |
else{ | |
plainText+=String.valueOf(alphabet.charAt(keyCipher.indexOf(currentChar))); | |
} | |
} | |
else{ | |
plainText+=String.valueOf(currentChar); | |
} | |
} | |
return plainText; | |
} | |
public static void main(String[]args){ | |
if(args.length>0){ | |
Console c = System.console(); //declare and assign console which will be used to get input from user | |
String keyPhrase = c.readLine("input a key phrase: "); //prompt user to input key phrase | |
SubCipher sb=new SubCipher(keyPhrase); //create subcipher with keyphrase provided by user | |
if(args[0].equals("-e")){ // if user wishes to perform encryption | |
String plainText = c.readLine("input plaintext: "); //prompt user to input plaintext to be encrypted | |
System.out.println("Encrypted text: "+sb.encrypt(plainText)); //output ciphertext generated from plaintext | |
} | |
else if(args[0].equals("-d")){ if user wishes to perform decryption | |
String cipherText = c.readLine("input ciphertext: "); //prompt user to input ciphertext to be decrypted | |
System.out.println("Decrypted text: "+sb.decrypt(cipherText)); //output plaintext generated from ciphertext | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment