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.
whichitAsked:
Who is Participating?
 
CEHJCommented:
Sorry, yes. Make that
System.arraycopy(myBytes, 0, keyBytes, 0, myBytes.length);

Open in new window

0
 
CEHJCommented:
Just use System.arraycopy
0
 
whichitAuthor Commented:
It takes a lot more parameters:

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

Could you advise on how I apply it to my example?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
CEHJCommented:
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

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

(i.e. the first line of your code)
0
 
whichitAuthor Commented:
Now, the arraycopy() method throws an ArrayIndexOutOfBoundsException.

My original source bytes length property is 16, if that's relevant?
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.