Solved

Arrays.copyOf method

Posted on 2011-02-16
7
857 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 79
thymeleaf natural templating vs JSP 2 109
eclipse shortcuts 9 58
servlet web applications   metadata-complete="true" or false 3 47
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

825 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