7 Replies - 1144 Views - Last Post: 06 October 2012 - 03:01 PM Rate Topic: -----

#1 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:06 PM

Hi all!

I'm a beginner to Java and I've been doing an assignment for one of my classes. I'm looking for some tips and pointers
in order to discover why I keep receiving the array out of bounds error.

My goal for this assignment is to write a program which will take in both upper and lower case letters then encrypt them, omitting any spaces or punctuation.

In my program, I've used a random number generator so that I always get a new cipher every execution of my program. So, whenever I receive a number from the random generator, I move the original letter that many places along the alphabet then
assign that as the new character and store the value of the random number in an array.

This is the error I am receiving through the Eclipse console window:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 71
at Encryptor.<init>(Encryptor.java:53)
at Test.main(Test.java:14)

and here is my code:

 import java.util.Random;

public class Encryptor {
	
	// The size of the alphabet and what is contained within the alphabet in an array
	public static final int AlphaSize = 52;
	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' };
	
	/* Arrays which will hold the value of the encrypted and original values of the string input and also an ArrayList to hold the 
	 number of rotations for each character. */
	protected char[] encrypt = new char[AlphaSize];
	protected char[] decrypt = new char[AlphaSize];
	protected int[] numstore = new int[100];
	
	// A random number generator which will be used to generate the number of rotation for each letter.
	public Random rand = new Random();
	
	/** A constructor which initialises the encryption and decryption arrays */
	public Encryptor()
	{
		// The encryption.
		for (int i=0; i < AlphaSize; i++)
		{
			int rotate = rand.nextInt(AlphaSize); // Generates a random number for encryption.
			encrypt[i] = alpha[ (i + rotate) % AlphaSize ]; // Moves whatever char i will be along 'rotate' places along the alpha.
			numstore[i] = rotate; // Adds the value of rotate to the Array for storage
		}
		
		// The decryption.
		for (int i=0; i < AlphaSize; i++)
		{
			for (int j=0; j < numstore.length; j++) 
			{
				int rotate = numstore[j];
				decrypt[encrypt[i] - rotate ] = alpha[i]; // Reverse of encrypt.
			}
		}
		
	}
	
	/** The encryption method */
	public String encrypt(String s)
	{
		char[]mess = s.toCharArray(); 
		for (int i=0; i < mess.length; i++)
		{
			if (Character.isUpperCase(mess[i]) && (Character.isLowerCase(mess[i]))) // Excludes any punctuation.
			{
				for (int j=0; j < numstore.length; j++) 
				{
					int rotate = numstore[j]; // Finds the random number value used.
					mess[i] = encrypt[mess[i] - rotate]; // Encrypts the letter
				}
			}
		}
	String secret = new String(mess);
	secret.trim();
	return secret;
	}
	
	/** The decryption method */
	public String decrypt(String s)
	{
		char[]mess = s.toCharArray();
		for (int i=0; i < mess.length; i++)
		{
			if (Character.isUpperCase(mess[i]) && (Character.isLowerCase(mess[i]))) // Excludes any punctuation.
			{
				for (int j=0; j < numstore.length; j++) 
				{
					int rotate = numstore[j]; // Finds the random number value used.
					mess[i] = decrypt[mess[i] - rotate]; // Decrypts the letter
				}
			}
		}
	return new String(mess);
	}
	
} 


I've tried comparing the error to the values of each ASCII character and changing to an ArrayList all to no avail.

I'd very much appreciate help and tips from anyone.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Encryption program in Java - Array out of bounds.

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:14 PM

Your problem here is that arrays do not start at 1, they start at 0. This being the case, there may be 52 values in your alpha array, but those values occupy index's 0 to 51.
Was This Post Helpful? 0
  • +
  • -

#3 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:19 PM

Hello.

When you encode, you're using modular arithmetic, so you have to do the same thing when you are decoding. For instance, if you have an alphabet of 26 characters and you're encoding character 23 with a rotation of, say, 12, you get (23+12)%26 = 9. When you go to decode, you get 9-12 = -3, which is probably what is producing an error on line 53.

Doing (c-rotate)%26 won't work either, because -3 will remain -3 mod 26. You could do something like (c + 26 - rotate)%26 instead. I didn't see any other problems.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#4 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:23 PM

I totally did not see that :P but I assume the array thing will cause problems down the road, if you are trying to reference an invalid index of an array you are bound to get errors, but he is right about your current error.
Was This Post Helpful? 0
  • +
  • -

#5 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:36 PM

Thanks for both of your replies, I'm surprised at the speed! :L

That got the program running. However, the text is not encrypting whenever I run it? It just sys.outs the input String I used?

I can't for the life of me understand why it isn't rotating the letters then storing them?

Thanks again for the assistance and the explanation of modular arithmetic NathanMullenax, was extremely helpful. Thank you :)

I'd really be grateful for further assistance
Was This Post Helpful? 0
  • +
  • -

#6 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:41 PM

Would you be able to post the main method?
Was This Post Helpful? 0
  • +
  • -

#7 NiftyClown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-October 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 02:46 PM

Here is the main method here:

 public class Test {

	public static void main(String[]args)
	{
		Encryptor e = new Encryptor();
		String s = "Hello world! My name is Batman!";
		s = e.encrypt(s);
		System.out.println(s);
		s = e.decrypt(s);
		System.out.println(s);
	}
	


Was This Post Helpful? 0
  • +
  • -

#8 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Encryption program in Java - Array out of bounds.

Posted 06 October 2012 - 03:01 PM

OK. I don't think the problem is with main. I think it might be that you are mixing up numbers with characters--both of which are numbers, ultimately.

Like, you have an alphabet {A,B,C,D,E...} and those characters are represented by their indices into the alpha array in some places and by the character codes themselves in others.

I'm not sure exactly where this is happening in your code. I guess to start with, since this is a symmetric key cipher (same key for encrypting and decrypting), your constructor would only need to store a key of random numbers between 0 and AlphaSize-1.

When you are reading a string to encrypt or decrypt, you would need to convert the character codes into array indexes. The sloppy, non-portable way to do this would be to subtract the first letter of the alphabet (assumes alphabet is made up of sequential character codes).

// given a character c
int index = c - 'A';



And then to go back to character codes:

char c = (char)(index + 'A');



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1