Link to home
Create AccountLog in
Avatar of whichit
whichit

asked on

Arrays.copyOf method

Hi all.

I'm trying to use the javax.crypto package to decrypt a byte[] array. The routine is proven to work on newer versions of Java, but I'm trying to port it to a 1.4 system. The following code works on 1.6:

final byte[] keyBytes = Arrays.copyOf(myBytes, 24); 
	    for (int j = 0, k = 16; j < 8;) { 
	            keyBytes[k++] = keyBytes[j++]; 
	    } 

Open in new window


However on 1.4, the copyOf method isn't available. I've tried the following replacement for the first line:

final byte[] keyBytes = new byte[24];

Open in new window


But clearly this isn't doing whatever copyOf() does. When I continue with the decryption, I get an error:

 javax.crypto.BadPaddingException: Given final block not properly padded

Are you able to suggest a 1.4 compatible routine that does what copyOf does?

Thanks.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Just use System.arraycopy
Avatar of whichit
whichit

ASKER

It takes a lot more parameters:

System.arraycopy(src, srcPos, dest, destPos, length)

Could you advise on how I apply it to my example?
The following has the same effect as your code:


final byte[] keyBytes = new byte[24];
	System.arraycopy(myBytes, 0, keyBytes, 0, 24);

Open in new window

>>The following has the same effect as your code:

(i.e. the first line of your code)
Avatar of whichit

ASKER

Now, the arraycopy() method throws an ArrayIndexOutOfBoundsException.

My original source bytes length property is 16, if that's relevant?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
:)