Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 10th, 2012, 10:56 PM
lastcentury lastcentury is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 2 lastcentury User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 5 sec
Reputation 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.

Reply With Quote
  #2  
Old October 11th, 2012, 12:03 PM
bullet's Avatar
bullet bullet is offline
Java Junkie
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2004
Location: Mobile, Alabama
Posts: 3,826 bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level)bullet User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 6 Days 8 h 47 m 58 sec
Reputation Power: 1248
Send a message via ICQ to bullet Send a message via AIM to bullet Send a message via MSN to bullet
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Help cant seem to fix

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap