0

I am trying to store the results of a xor operation into a string array. When I run my code as shown, it is able to print out the values.

int[] randnumbs = new int[0];
randnumbs = streamGen(plaintext.length());
String plaintextnumb;

String encryption;

int[] answer = new int[0];
int count = 0;

while(true) {
    plaintextnumb = plaintext.substring(count, count + 2);
    int numbfromfile = Integer.valueOf(plaintextnumb, 16);
    int xored = numbfromfile ^ randnumbs[count];

    encryption = Integer.toHexString(xored);

    System.out.print(encryption + " "); 
    if(count == (plaintext.length() / 2) - 1) {
        break;
    }else {
        count++;
    }
}

results:

af a0 52 b1 fb 0 a6 75 

When I change my variable "encryption" to a String array my code is able to run but seams to stop running when it gets to where "encryption[count] = Integer.toHexString(xored);" I have never encountered this problem before. No errors are showing up when I run my program, it is just showing an empty console. I also inserted printout statements before and after this line of code and was only able to see the printout before the line of code, not after. Any explanation of why this is happening would be greatly appreciated!

int[] randnumbs = new int[0];
randnumbs = streamGen(plaintext.length());
String plaintextnumb;

String[] encryption = new String[0];

int[] answer = new int[0];
int count = 0;

while(true) {
    plaintextnumb = plaintext.substring(count, count + 2);
    int numbfromfile = Integer.valueOf(plaintextnumb, 16);
    int xored = numbfromfile ^ randnumbs[count];

    encryption[count] = Integer.toHexString(xored);

    System.out.print(encryption[count] + " "); 
    if(count == (plaintext.length() / 2) - 1) {
        break;
    }else {
        count++;
    }
}
1
  • You mean "seems" to stop? Can you post the clear value of the input to hex-string ? Commented Nov 10, 2019 at 18:57

2 Answers 2

1

The problem is that the size of the array, encryption is 0 because of String[] encryption = new String[0]; declaration. Therefore, assigning a value using the statement, encryption[count] = Integer.toHexString(xored); may cause ArrayIndexOutOfBoundsException because the value of count may have increased (may have become more than 0).

A solution would be to declare your array with the required size e.g. String[] encryption = new String[10];

Also, please check if you have put a debug point at the line, encryption[count] = Integer.toHexString(xored); and whether you are running the program in the Debug mode. If yes, this may be the reason why your program is not proceeding beyond this line.

Sign up to request clarification or add additional context in comments.

1 Comment

Put simpler: String[] is an array type and, in Java, this cannot grow dynamically. Use ArrayList<String>. (+1 for debug mode which is probably auto-catching AIOOBE)
0

I think because you are creating the encryption array with size 0, so it cannot have values. So when you are trying to use encryption[count] the value doesn't exist. Use ArrayList and use the add() method to add the string to your list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.