September 24, 2013

Substitution Cipher - Java

Downloadable file : Substitution Cipher


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class subs_cipper { 
      public static void main(String[] args) { 
  
      System.out.println("This is to demonstrate the Substitution Cipher");
      System.out.println("Each alphanumeric char is mapped to a String (arbitrarily, of two chars length)");
      System.out.println("For example, 'A' is mapped to \"OA\", 'B' is mapped to \"9B\", and so on...");
      System.out.print("\nTest String = ");
      String plaintext = "WHYAMIDOINGTHISWHYAREWEHEREWHATAREWEDOING";
      //string to be ciphered
      //String plaintext = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
      //use this to check. With this, 
      //...plaintext = Keys, and Ciphertext = Values
      System.out.print(plaintext);
      
      //Encryption
      System.out.println("\n\nCiphertext = ");
      Cipher ObjCipher = new Cipher();
      String ciphertext = ObjCipher.Encrypt(plaintext);  
      //output ciphered string
      System.out.println(ciphertext);
      
      //Decryption
      System.out.print("\nPlaintext = \n");
      String outplaintext = ObjCipher.Decrypt(ciphertext);  
      //output deciphered string
      System.out.print(outplaintext);
   }
}


//the class that that implements the actual encryption
class Cipher {
String keys, values;  
     //Keys & Values for HashMap
String plaintext, ciphertext;
HashMap<String, String> map = new HashMap<String, String>(); 
     //HashMap to do the mapping


//constructor, instantiates the HashMap
public Cipher(){
//one-to-one mapping (Keys - Values) of different length. In this example, 
//A:OA, B:9B, C:8C &  so on...
 keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
values = "0A9B8C7F6E5R4F3T2J1K4LX2VN18K9LC42B7" +
         "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";


//use for loop to fill up & map the Keys to the respective Values
for (int i=0; i<keys.length(); i++){
char charkey = keys.charAt(i);  
       //get each char from the Keys & convert to String
String tempkey = Character.toString(charkey);

char charval1 = values.charAt(i*2);  
       //get every two chars from the Values & convert to String
String tempval1 = Character.toString(charval1);  
       //...and concatenate both chars to form a single String
char charval2 = values.charAt(i*2+1);
String tempval2 = Character.toString(charval2);
String tempval = tempval1 + tempval2;

map.put(tempkey, tempval);  
       //fill the HashMap
}

}


//Encrypt method, do the Encryption, takes Plaintext as argument
public String Encrypt(String plaintext){
String output = "";
char ch;

for (int i=0; i<plaintext.length(); i++){  
   //go through the plaintext 
ch = plaintext.charAt(i);    
       //take each chars as 'Key'
String str = Character.toString(ch);  
       //..and map to its corresponding 'Value'
output = output + map.get(str);
}
 
   return output;  
   //returns the the encrypted String
}


//Decrypt method, do the Decryption, takes Ciphertext as argument
public String Decrypt(String ciphertext){
String output = "";
char ch;
String str = "";

for (int i=0; i<ciphertext.length(); i = i+2){  
  //get next two adjacent chars from the Ciphertext
ch = ciphertext.charAt(i);    
       //concatenates them into a single String
str = Character.toString(ch);
ch = ciphertext.charAt(i+1); 
str = str + Character.toString(ch);

Iterator j = map.entrySet().iterator();  
       //look up through HashMap by the Value 

while (j.hasNext()) {  
       //...to find the corresponding Key
Map.Entry entry = (Map.Entry) j.next();
if (str.equals((String)entry.getValue())){  
                //concatenates the output with each Key found
output = output + (String)entry.getKey();
}
}
}

return output;  
  //return the Decrypted String
}

}


No comments:

Post a Comment

Write a reply