Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

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.
0
whichit
Asked:
whichit
  • 5
  • 2
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Open in new window

0
 
CEHJCommented:
:)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now