<p>I wrote a java program for a Ceasar Cipher (letter substitution).  A use
ID: 3638432 • Letter: #
Question
<p>I wrote a java program for a Ceasar Cipher (letter substitution).  A user will enter a word or sentence and the program will encrypt and decrypt the message.  Then it will output to the screen.  This works with all uppercase letters, but I want to use upper and lowercase letters.  I can't get the decrypt method to work correctly.  I keep getting an ArrayOutOfBounds Exception.</p><p>import java.io.*;<br />import java.util.Scanner;<br />public class CaesarCipher {<br />    public static final int ALPHASIZE = 52; // English alphabet, uppercase & lowercase<br />    public static final char[] alpha = {'a','b','c','d','e','f','g','h','i','j','k',<br />        'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D',<br />        'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W',<br />        'X','Y','Z'};    <br />    protected char[] encrypt = new char[ALPHASIZE]; // encryption array<br />    protected char[] decrypt = new char[ALPHASIZE]; // decryption array<br />    <br />    // constructor that initializes the encryption and decryption arrays<br />    public CaesarCipher(){<br />        for(int i = 0; i < ALPHASIZE; i++)<br />            encrypt[i] = alpha[(i+3) % ALPHASIZE]; // rotate alphabet by 3 spaces<br />        for(int i = 0; i < ALPHASIZE; i++)<br />            decrypt[encrypt[i-3] ] = alpha[i]; // - 'a' 'A'decrypt is reverse of encrypt<br />    }<br />        <br />    // encryption method<br />    public String encrypt(String secret){<br />        char[] mess = secret.toCharArray(); // the message array<br />        for(int i = 0; i < mess.length; i++) // encryption loop<br />            if(Character.isLetter(mess[i])) // we have a letter to change<br />                mess[i] = encrypt[mess[i]- 'a']; // use letter as an index<br />        return new String(mess);<br />    }<br />    <br />    // decryption method<br />    public String decrypt(String secret){<br />        char[] mess = secret.toCharArray(); // the message array<br />        for(int i = 0; i < mess.length; i++) // decryption loop<br />            if(Character.isLetter(mess[i])) // we have a letter to change<br />                mess[i] = decrypt[mess[i]- 'a']; //  use letter as an index<br />        return new String(mess);<br />    }<br />    <br />    // main method for testing the Caesar Cipher<br />    public static void main(String[] args) {<br />        Scanner s = new Scanner(System.in);<br />        <br />        // user asked to enter a sentence<br />        // the cipher will encrypt any combo of letters entered<br />        System.out.println("Enter a sentence: ");<br />        String secret = s.nextLine();<br />        System.out.print(" ");<br />        <br />        CaesarCipher cipher = new CaesarCipher(); // create Caesar Cipher object<br />        System.out.println("Encryption order = " + new String(cipher.encrypt));<br />        System.out.println("Decryption order = " + new String(cipher.decrypt));<br />                <br />        secret = cipher.encrypt(secret);<br />        System.out.println(secret); // the ciphertext<br />        secret = cipher.decrypt(secret);<br />        System.out.println(secret); // should be plain text again<br />    }</p>
Explanation / Answer
So if it works only for the upper case letter you can remaster it, and this is how.
1) Create an array of integers, which will keep the indexes of the lower case letters.
2) convert the string into uppercase and at the same time add the index of the letter which is lowercase and it is converted to uppercase.
later to retain the lowercase letter do the following:
lets call our new array arr.
for(i=0;i<len;i++) //len is the lenght of our new array which keeps the indexses of the lowercase letters
mess[arr[i]]=toLowercase(mess[arr[i]])
I hope you understand this trick.
should look something like this.( i havent compiled the code)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.