Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: ArrayIndexOutOfBoundsException problem in decryption algorithm

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Unhappy ArrayIndexOutOfBoundsException problem in decryption algorithm

    I don't need a lesson in what the exception is, I know what it is. What I don't know is why I'm getting it. The encryption algorithm uses a similar line, but the call from the array is exactly the same. The encryption uses a simple-substitution with a pattern buster variable. The decryption algorithm worked until I after I implemented that variable. The array being returned by the toArray() method is correct and full and so is the array in the call, it should have a length of 11 and does until that point for some reason.

    Code for testing class:
    public class tester {	
    	public static void main(String args[]){
     
     
     
    			long start = System.currentTimeMillis();
     
    			String enc = Encryptor.hardEncrypt("Mississippi",7,1);
    			System.out.println(enc);
    			System.out.println("Decrypted: " + Encryptor.keyDecrypt(enc, 7, 5));
     
    			long end = System.currentTimeMillis();
    			System.out.println("   Elapsed time: " + (end-start) + "ms.");	
    	}
    }

    Code for Encryption class:
    package algorithms;
     
    import javax.swing.JOptionPane;
     
    public class Encryptor {
     
    	private static String[] vals = {" ","!", "\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9"
    		,":",";","<","=",">","?","@","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","{","}","|"
    		,"}","~"};
    	private static int max = vals.length;
     
    	public Encryptor(){}
    	//takes a String a makes it into an array containing every character
    	private static String[] toArray(String enc)
    	{
    		String[] arr = new String[enc.length()];
    		for(int i = 0; i < enc.length(); i++)
    		{
    			arr[i]=enc.substring(i,i+1);
    		}
    		return arr;
    	}
    	//assigns number values to each character
    	private static int[] numValues(String[] enc)
    	{
    		int[] numvals = new int[enc.length];
     
    		for(int i = 0; i < enc.length;i++)
    		{
    			for(int j = 0; j < max; j++)
    			{
    				if(enc[i].equals(vals[j]))
    				{
    					numvals[i] = j;
    					System.out.println("numValues(): " + i+": " + j);
    					break;
    				}
    			}
    		}
    		System.out.println("numValues(): " + numvals.length);
    		return numvals;
    	}
     
    	//shifts each character by the specified increment
    	public static String hardEncrypt(String enc, int inc, int times)
    	{
    		String fin = "";//variable for final encrypted String
    		int chng = enc.length()*enc.length();//pattern breaker and makes the substitution much less obvious
    		int[] nums = numValues(toArray(enc));//integer array for the characters in the String to be encrypted
    		for(int amount = 0; amount < times; amount++)//encryption and re-encryption loop
    		{	
    			fin = "";//resets fin
    			chng = enc.length()*enc.length();//resets the breaker
    			for(int i = 0; i < enc.length(); i++)//encryption algorithm loop
    			{
    				int ind = (nums[i] + inc)*chng;//the index in vals[] containing the correct character
    				if(ind < 0)//makes sure the number is positive
    				{
    					ind *= -1;
    				}
    				if(ind >= max)//makes sure the index is in bounds
    				{
    					while(ind >= max)
    						ind -= max;
    				}
    				fin += vals[ind];//adds the character at vals[ind] to fin
    				chng = Math.round(chng*1/2);//algorithmically changes the breaker integer
    				if(chng == 1)//resets the breaker if it equals one to enc.length^2*i
    				{
    					chng = enc.length()*enc.length() * i;
    				}
    			}
    			nums = numValues(toArray(fin));//resets the nums array
    		}
     
    		return fin;//returns the encrypted String
    	}
    	//decryption method based on hardEncrypt()
    	/*
    	 * Problem Code!! vvvvvvvvv
    	 */
    	public static String keyDecrypt(String enc, int key, int times)
    	{
    		String fin = "";//String for final decrypted String
    		int chng = enc.length()*enc.length();//copies the algorithm and attempts to reverse it
    		System.out.println("keyDecrypt(): " + enc);
    		System.out.println("keyDecrypt(): " + chng);
    		int[] nums = numValues(toArray(enc));//array getting the exception
    		for(int amount = 0; amount < times; amount++)
    		{	
    			fin = "";
    			chng = enc.length()*enc.length();
    			for(int i = 0; i < enc.length(); i++)
    			{
    				int ind = (nums[i] - key)*chng;//getting an ArrayIndexOutOfBoundsException and I can't figure out the cause
    				if(ind < 0)
    				{
    					ind += max;
    				}
    				if(ind >= max)
    				{
    					while(ind >= max)
    						ind -= max;
    				}
    				fin += vals[ind];
    				nums = numValues(toArray(fin));
    				chng = Math.round(chng*1/2);
    				if(chng == 1)
    				{
    					chng = enc.length()*enc.length() * i;
    				}
    			}
    		}
     
    		return fin;
    	}
    	/*
    	 * Problem Code!! ^^^^^^^^^
    	 */
     
    	public static String easyEncrypt(String enc)
    	{
    		return hardEncrypt(enc, 3,1);
    	}
     
    	public static String decryptEasyEncrypt(String dec)
    	{
    		return hardEncrypt(dec, -3,1);
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    I found errors in you algorithm. After you encrypt the word mississippi, the numeric cypher text would be 82, 30, 0, 0 each at nums[0], nums[1] and so on. When the turns comes of the third value that is 0. you see that the ind value remains negative. that is (0 - 7) * 121 = -847 which is negative.
    And even after adding the max value to it, it remains negative still. The ind value does not change at all. And when the program reach at fin += vals[ind] then the ind value is still negative. So this might have cause the problem. Whats your encryption algorithm. I wanted to check if the algorithm itself is correct or not. Hope this will help you

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    i found the error now but your algorithm is wrong.
    In you keydecrypt method nums = numValues(toArray(fin)); should be out of the loop statement. This was causing the problem.
    overall it should be
     
    				if(ind < 0)
    				{
    					ind *= -1;
    				}
     
     
    				if(ind >= max)
    				{
    					while(ind >= max)
    						ind -= max;
    				}
     
    				fin += vals[ind];
     
    				chng = Math.round(chng*1/2);
    				if(chng == 1)
    				{
    					chng = enc.length()*enc.length() * i;
    				}
    			}
    			nums = numValues(toArray(fin));
    		}
     
    		return fin;
    	}

    It works but the decryption fails. I will work on your algorithm and find out the problem. Hope it helps buddy

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    Thanks, what about this as a solution:

    if(ind < 0)
    {
            while(ind < 0)
                     ind+=max;
    }

    That is what I did, however, I still am getting the exception at that line. If you look at the same line in the encryption algorithm, where it calls the value at

    int ind = (nums[i] - key)*chng;

    It's the same in both spots, however, I only get the exception in the decryption algorithm. Also, I was working on the decryption algorithm, it won't be right yet, but I can't make it right with the exception popping up everytime I run it.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    the ind could be negative even you did ind *= -1;. Try with this:
    				int ind = (nums[i] + inc)*chng;//the index in vals[] containing the correct character
    				if(ind < 0) ind = math.abs(ind);
    				if(ind >= max)//makes sure the index is in bounds
    				{
    					while(ind >= max)
    						ind -= max;
    				}
    				fin += vals[ind];//adds the character at vals[ind] to fin

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    In that case, it couldn't be negative.

    if(ind < 0) //ind has to be negative to return true
          ind *= -1; //will only use this line if the if statement returns true meaning ind has to be negative.

    int ind = (nums[i] - key)*chng;//getting an ArrayIndexOutOfBoundsException and I can't figure out the cause

    That is the line I'm getting the exception from. the only variable working with nums[] is "i". nums[] length is correct before the loop and should be correct the whole time, however, it only goes through one time meaning there's only one value in nums[]. It should s

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    Quote Originally Posted by aesguitar View Post
    In that case, it couldn't be negative.

    if(ind < 0) //ind has to be negative to return true
          ind *= -1; //will only use this line if the if statement returns true meaning ind has to be negative.

    int ind = (nums[i] - key)*chng;//getting an ArrayIndexOutOfBoundsException and I can't figure out the cause

    That is the line I'm getting the exception from. the only variable working with nums[] is "i". nums[] length is correct before the loop and should be correct the whole time, however, it only goes through one time meaning there's only one value in nums[]. It should s
    Uh..oh I've copied your Encryptor and surveyed it...have a question
    int[] nums = numValues(toArray(enc));//array getting the exception
    ...
    for(int i = 0; i < enc.length(); i++)
    What happens if you change to:

    int[] nums = numValues(toArray(enc));//array getting the exception
    ...
    for(int i = 0; i < nums.length; i++)

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    ...I've copied and tested...it worked fine.

  9. #9
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    Alright, but it's supposed to decrypt the encrypted phrase. Basically, in this case, change "x> jnb<wMF" back into Mississippi. It printed "Decrypted: 1". It should say "Decrypted: Mississippi".

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    29
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    can you tell the name of encryption algorithm you are using. I actually wanted to check your algorithm correctness.

  11. #11
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: ArrayIndexOutOfBoundsException problem in decryption algorithm

    I actually made it up myself. It's a simple-substitution. I added an algorithmic "pattern-breaker" do break up same letters and such. The decryption worked perfectly until I added the breaker, which was what I was trying to do but kept getting the exception.

    //shifts each character by the specified increment a number of times.
    //a pattern breaker renders the equation Number of Shifts = Increment * Number of Times useless and breaks up any same letters
    	public static String hardEncrypt(String enc, int inc, int times)
    	{
    		String fin = "";//variable for final encrypted String
    		int chng = enc.length()*enc.length();//pattern breaker and makes the substitution much less obvious
    		int[] nums = numValues(toArray(enc));//integer array for the characters in the String to be encrypted
    		for(int amount = 0; amount < times; amount++)//encryption and re-encryption loop
    		{	
    			fin = "";//resets fin
    			chng = enc.length()*enc.length();//resets the breaker
    			for(int i = 0; i < enc.length(); i++)//encryption algorithm loop
    			{
    				int ind = (nums[i] + inc)*chng;//the index in vals[] containing the correct character
    				if(ind < 0)//makes sure the number is positive
    				{
    					ind *= -1;
    				}
    				if(ind >= max)//makes sure the index is in bounds
    				{
    					while(ind >= max)
    						ind -= max;
    				}
    				fin += vals[ind];//adds the character at vals[ind] to fin
    				chng = Math.round(chng*1/2);//algorithmically changes the breaker integer
    				if(chng == 1)//resets the breaker if it equals one to enc.length^2*i
    				{
    					chng = enc.length()*enc.length() * i;
    				}
    			}
    			nums = numValues(toArray(fin));//resets the nums array
    		}
     
    		return fin;//returns the encrypted String
    	}

    The comments outline exactly what is going on in the algorithm.
    Last edited by aesguitar; July 13th, 2012 at 03:52 PM.

Similar Threads

  1. Simple encryption/ decryption problem
    By searchformeaning in forum Loops & Control Statements
    Replies: 2
    Last Post: May 7th, 2012, 01:34 AM
  2. Euler Paths with dfs algorithm problem
    By claudio.r in forum Algorithms & Recursion
    Replies: 18
    Last Post: March 22nd, 2012, 04:39 PM
  3. Code for Decryption
    By me_newbie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2011, 07:40 AM
  4. Decryption problem
    By hjen in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 20th, 2010, 02:58 AM
  5. I have algorithm problem
    By Newoor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2009, 08:11 PM

Tags for this Thread