JAVA Information Security Principles ad Practices Write a pr
JAVA
Information Security: Principles ad Practices.
Write a program to help an analyst decrypt a simple substitution cipher.
Your program should take the ciphertext as input, compute letter frequency
counts, and display these for the analyst. The program should
then allow the analyst to guess a key and display the results of the
corresponding \"decryption\" with the putative key.
BELOW IS WHAT I GOT SO FAR. ANALYST IS NOT GUESSING A KEY OR PUTATIVE KEY IS NOT COMING OUT.
IF YOU HAVE BETTER PROGRAM, PLEASE IGNORE THE SOURCE BELOW. THANKS.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class SubstitutionCipher
{
Map<Character, Integer> letterFrequencyMap;
Map<Character, Character> key;
String ciphertext;
String plaintext;
BufferedReader br;
private void getCiphertext() // get cipher text as input from analyst
{
System.out.println(\"----- Substitution Cipher ----\");
System.out.println(\" Enter the cipher text: \");
br = new BufferedReader(new InputStreamReader(System.in));
try
{
ciphertext = br.readLine();
}
catch(Exception e)
{
System.err.println(\"Exception in cipher text reading \" + e );
e.printStackTrace();
}
ciphertext = ciphertext.toUpperCase();
System.out.println(\"The ciphertext Entered is - \" + ciphertext);
}
private void displayLetterFrequency()
{
letterFrequencyMap = new LinkedHashMap<Character, Integer>();
for (int i = 0; i < ciphertext.length(); i++)
{
Character ch = ciphertext.charAt(i);
if (!( ch >= \'A\' && ch <=\'Z\')) continue; // check if letter is not in between A to Z
Integer cnt = (Integer) letterFrequencyMap.get(ch);
if(cnt == null)
{
letterFrequencyMap.put(ch,1);
}
else
{
letterFrequencyMap.put(ch,cnt + 1);
}
}
System.out.println(letterFrequencyMap);
}
private void keyGuess()
{
System.out.println(\"Hey Analyst , Guess a key from a to z\");
String putativeKey = null;
try
{
putativeKey = br.readLine();
}
catch(Exception e)
{
System.out.println(\"Excpetion in reading the putative key\");
e.printStackTrace();
}
createKeyMap(putativeKey);
}
private void createKeyMap(String putativeKey)
{
if(putativeKey.length() < 26)
{
System.out.println(\"putative key length is less then 26 letters. Invalid Entry. \");
return;
}
key = new HashMap<Character, Character>();
putativeKey = putativeKey.toUpperCase();
for(int j = 0;j<putativeKey.length();j++)
{
key.put(putativeKey.charAt(j), (char) (j + \'A\'));
}
}
private void getPlainText()
{
StringBuffer plainBuffer = new StringBuffer();
for( int k = 0; k < ciphertext.length(); k++)
{
Character chr = (Character) key.get(ciphertext.charAt(k));
if(chr == null)
{
System.out.println(\"Error Character - \"+ciphertext.charAt(k) + \"in ciphertext - is not mapped to any letter \");
plainBuffer.append(ciphertext.charAt(k));
}
else
{
plainBuffer.append(chr);
}
}
plaintext = plainBuffer.toString();
System.out.println(\"PlainText is - \" + plaintext);
}
private void closeInputbuffer()
{
if(br !=null)
{
try
{
br.close();
}
catch(Exception e)
{
System.out.println(\"Exception in closeInputbuffer \"+e);
e.printStackTrace();
}
}
}
public void decrypter()
{
try
{
getCiphertext();
displayLetterFrequency();
keyGuess();
getPlainText();
}
catch(Exception e)
{
System.out.println(\"Exception in Decryption \"+e);
e.printStackTrace();
}
finally
{
try
{
closeInputbuffer();
}
catch(Exception e)
{
System.out.println(\"Exception in closing input buffer\" +e);
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
SubstitutionCipher cipher = new SubstitutionCipher();
cipher.decrypter();
}
}
Solution
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
class SubstitutionCipher
 {
    Map<Character, Integer> letterFrequencyMap;
    Map<Character, Character> key;
    String ciphertext;
    String plaintext;
    BufferedReader br;
    private void getCiphertext() // get cipher text as input from analyst
    {
        System.out.println(\"----- Substitution Cipher ----\");
        System.out.println(\" Enter the cipher text: \");
        br = new BufferedReader(new InputStreamReader(System.in));
        try
        {
            ciphertext = br.readLine();
        }
        catch(Exception e)
        {
            System.err.println(\"Exception in cipher text reading \" + e );
            e.printStackTrace();
        }
        ciphertext = ciphertext.toUpperCase();
        System.out.println(\"The ciphertext Entered is - \" + ciphertext);
    }
   private void displayLetterFrequency()
    {
        letterFrequencyMap = new LinkedHashMap<Character, Integer>();
        for (int i = 0; i < ciphertext.length(); i++)
        {
            Character ch = ciphertext.charAt(i);
            if (!( ch >= \'A\' && ch <=\'Z\')) continue; // check if letter is not in between A to Z
                Integer cnt = (Integer) letterFrequencyMap.get(ch);
            if(cnt == null)
            {
                letterFrequencyMap.put(ch,1);
            }
            else
            {
                letterFrequencyMap.put(ch,cnt + 1);
            }
        }
        System.out.println(letterFrequencyMap);
    }
   private void keyGuess()
    {
        System.out.println(\"Hey Analyst , Guess a key from a to z\");
        String putativeKey = null;
        try
        {
            putativeKey = br.readLine();
        }
        catch(Exception e)
        {
            System.out.println(\"Excpetion in reading the putative key\");
            e.printStackTrace();
        }
        createKeyMap(putativeKey);
    }
   private void createKeyMap(String putativeKey)
    {
        if(putativeKey.length() < 26)
        {
            System.out.println(\"putative key length is less then 26 letters. Invalid Entry. \");
            return;
        }
        key = new HashMap<Character, Character>();
        putativeKey = putativeKey.toUpperCase();
        for(int j = 0;j<putativeKey.length();j++)
        {
            key.put(putativeKey.charAt(j), (char) (j + \'A\'));
        }
    }
   private void getPlainText()
    {
        StringBuffer plainBuffer = new StringBuffer();
        if(key== null)return;
        for( int k = 0; k < ciphertext.length(); k++)
        {
           
            Character chr = (Character) key.get(ciphertext.charAt(k));
            if(chr == null)
            {
                System.out.println(ciphertext);
                System.out.println(\"Error Character - \"+ciphertext.charAt(k) + \"in ciphertext - is not mapped to any letter \");
                plainBuffer.append(ciphertext.charAt(k));
            }
            else
            {
                plainBuffer.append(chr);
            }
        }
        plaintext = plainBuffer.toString();
        System.out.println(\"PlainText is - \" + plaintext);
    }
   private void closeInputbuffer()
    {
        if(br !=null)
        {
            try
            {
                br.close();
            }
            catch(Exception e)
            {
                System.out.println(\"Exception in closeInputbuffer \"+e);
                e.printStackTrace();
            }
        }
    }
   public void decrypter()
    {
        try
        {
            getCiphertext();
            displayLetterFrequency();
            keyGuess();
            getPlainText();
        }
        catch(Exception e)
        {
            System.out.println(\"Exception in Decryption \"+e);
            e.printStackTrace();
        }
        finally
        {
            try
            {
                closeInputbuffer();
            }
            catch(Exception e)
            {
                System.out.println(\"Exception in closing input buffer\" +e);
                e.printStackTrace();
            }
        }
    }
 }
  
 public class Test {
   public static void main(String[] args)
    {
        SubstitutionCipher cipher = new SubstitutionCipher();
        cipher.decrypter();
    }
 }
    








