Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Arrays.copyOf method

Posted on 2011-02-16
7
Medium Priority
?
894 Views
Last Modified: 2012-05-11
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
Comment
Question by:whichit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34906222
Just use System.arraycopy
0
 

Author Comment

by:whichit
ID: 34906289
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 34906326
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 34906335
>>The following has the same effect as your code:

(i.e. the first line of your code)
0
 

Author Comment

by:whichit
ID: 34906449
Now, the arraycopy() method throws an ArrayIndexOutOfBoundsException.

My original source bytes length property is 16, if that's relevant?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 34906672
Sorry, yes. Make that
System.arraycopy(myBytes, 0, keyBytes, 0, myBytes.length);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34914649
:)
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question