#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Smile Help cant seem to fix


    Okay i have been stuck in a peice of code for a couple of hours now and need your help! I have located the problem but can't fix.

    1. I'm simply taking a users input from a keyboard using a scanner.
    2. I then display that string back to the user
    3. then an encryption method takes place
    4. the the encrypted string is shown to the user.
    5. following this a method which decrypts the string is called
    6. then the decrypted string is then shown back to the user.

    The encryption is based on ceaser cipher encryption.

    I'm wanting an array of both lowercase and uppercase letters which is encrypted.

    I'm trying to get it so that the string being encrypted and decrypted can be both upper and lowercase, and when decrypted the string will display the original case inputted by the user.

    It works using just uppercase letters and by using 26 values in the array but doesn't work for 52 including the lowercase letters.

    Here is my code for encryption.java class

    Code:
    package Encryption;
    
    import java.util.Random;
    
    public class encrpytion {
    		
    	public static final int ALPHASIZE = 52; // English alphabet(uppercase and lowercase)
    
    	public static final char[] alpha ={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    
    	protected char[] encrypt = new char[ALPHASIZE]; // Encryption array
    	protected char[] decrypt = new char[ALPHASIZE]; // Decryption array
    
    	Random randomNum = new Random();
    	int random = randomNum.nextInt(15); // Random value to randomise the encryption characteristics
    	
    	/** Constructor that initializes the encryption and decryption arrays */
    	public encrpytion() {
    		
    		for (int i=0; i<ALPHASIZE; i++)
    		{
    			encrypt[i] = alpha[(i + 3 + random) % ALPHASIZE]; // rotate by x places
    		}
    
    		for (int i=0; i<ALPHASIZE; i++)
    		{
    			decrypt[encrypt[i] - 'A'] = alpha[i]; // reverse of encrypt
    		}
    		
    	}
    
    	/** Encryption method */
    	public String encrypt(String secret) {
    		
    	char[] mess = secret.toCharArray(); // the message array
    	
    		for (int i=0; i<mess.length; i++) // encryption loop
    			if (Character.isDefined(mess[i])) // we have a letter to change
    			{
    				mess[i] = encrypt[mess[i] - 'A']; // encrypt letter
    			}
    		
    	return new String(mess);
    	
    	}
    
    	/** Decryption method */
    	public String decrypt(String secret) {
    		
    	char[] mess = secret.toCharArray(); // the message array
    	
    	for (int i=0; i<mess.length; i++) // decryption loop
    		
    		if (Character.isDefined(mess[i])) // we have a letter to change
    		{
    			mess[i] = decrypt[mess[i] - 'A']; // encrypt letter
    		}
    	
    	return new String(mess);
    
    	}
    	
    	
    }
    Code for output.java (used to take user input and display)

    Code:
    /**
     * 
     */
    package Encryption;
    
    import java.util.Scanner;
    
    /**
     * @author Home
     *
     */
    public class DisplayOutput {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		      
    		// Get user string 
    		System.out.println("Please enter a string:");
    		Scanner scanner = new Scanner(System.in);
    		String UserInput = scanner.nextLine();
    		System.out.println();
    		
    		// Display user string
    		System.out.println("Your string is:");
    		System.out.println(UserInput);
    		System.out.println();
    		
    		// Create new object
    		encrpytion cipher = new encrpytion(); 
    		
    		// Encrypt user string
    		String secretString = cipher.encrypt(UserInput);
    		System.out.println("Your string has been encrypted to:");
    		System.out.println(secretString); // the cipher text
    		System.out.println();
    		
    		String revealSecret = cipher.decrypt(secretString);
    		System.out.println("Your string has been decrypted to:");
    		System.out.println(revealSecret); // the cipher text
    		System.out.println();
    	}
    	
    	
    	
    
    }
    I have narrowed down the area to where the problem is occuring and that is here in encryption.java

    Code:
    		for (int i=0; i<ALPHASIZE; i++)
    		{
    			decrypt[encrypt[i] - 'A'] = alpha[i]; // reverse of encrypt
    		}
    The java complication error is :
    Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 52 	at Encryption.encrpytion.<init>(encrpytion.java:27) 	at Encryption.DisplayOutput.main(DisplayOutput.java:31)
    If you could please offer any guidance on how i may be able to fix the problem? or point me on track i would like to know.

    Thank you very much for taking time to read.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    One thing I notice here

    Code:
    decrypt[encrypt[i] - 'A']
    is that the encrypt array can possibly take on values that are lowercase characters where the difference between that character and 'A' is larger than the size of the array.

IMN logo majestic logo threadwatch logo seochat tools logo