Skip to content

Instantly share code, notes, and snippets.

@sebjacobs
Created June 9, 2010 15:23
Show Gist options
  • Save sebjacobs/431634 to your computer and use it in GitHub Desktop.
Save sebjacobs/431634 to your computer and use it in GitHub Desktop.
import java.io.Console;
/*
Cryptography and Internet Security - Worksheet 1
Seb Jacobs
This class encrypts and decrypts a plaintext message using a substitution cipher.
The key for this substitution cipher is derived from a keyphrase provided by the user.
All the spaces and duplicate letters are removed from the keyphrase.
The remaining letters of the alphabet are appended to the end of the string in decreasing order.
*/
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
KeyCipher=this.removeDuplicates(KeyCipher);
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 removeDuplicates(String cipher){
String [] cipherCharRemoved;
for(int i=0;i<cipher.length();i++){
String firstChar=cipher.substring(i,(i+1));
String subString1= cipher.substring(0,(i+1));
String subString2 = cipher.substring((i+1),cipher.length());
cipherCharRemoved = subString2.split(firstChar);
cipher=subString1;
for(int j=0;j<cipherCharRemoved.length;j++){
cipher+=cipherCharRemoved[j];
}
}
return cipher;
}
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: ");
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