• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split up a byte array into blocks, turn them into numbers and go back in reverse  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I am writing here, as I am desperate.


My problem is the following - I need to split up a byte array into smaller chunks, turn them into numbers and do it again in reverse. The thing is, the bytes are getting mixed up somewhere along the way and I can not figure out who the culprit is.

Here is the function I use to split up the original byte array:


And this is how I enumerate the chunks:


And this is how I convert it back into a byte array:


And finally, how I combine the small arrays into big ones:



I did a check and got different array lengths for the different stages:
63354 for the original byte
63360 for the array of blocks
64466 for the new array, after converting the enumerated one



Any help is greatly appreciated, you guys are my hail marry.


Thank you for reading.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which part is broken? Does your divideArray method work properly? If not, there is no reason to look at anything else. If you don't know if it works, then you need to test it and work on it until you KNOW it is rock solid.

Use a lot of System.out.println statements to make sure ret array is the right size. Make sure each array in it is the right size.

My ratio of real code to 'code that proves my real code works' is about 1-3 or 1-4. All that eventually gets thrown away, but it lets me write more code with more confidence.

Once you are sure your divideArray() method is correct, start testing the code you have in the "how I enumerate the chunks".
 
chavdar yotov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for the reply!

That's an excellent idea for testing, thank you! I will get on it first thing in the morning.

Could you give me any suggestions on how should I test the enumeration snippet? I'm not sure what to consider solid information with the conversion between a byte array and it's numeric value.
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should really create an SSCCE because without one we are guessing how you are putting all you code together.

There may be more wrong but there is one very obvious problem. When using your approach you convert a byte array to a BigInteger and then back again leading bytes of zero will be lost. For example (0, 0, 1, 1, 2, 2} will end up as {1,1,2,2} .
 
chavdar yotov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you have any tips on how to avoid said byte loss, Richard?

The rest of my code works as intended, as it's a simple RSA encryption, which has been tested thoroughly. The issue here is the conversion and reversal, as it's the newest element, which I'm having issues with. If you'd insist, I'll get on to posting more of it.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chavdar yotov wrote:Would you have any tips on how to avoid said byte loss, Richard?

The rest of my code works as intended, as it's a simple RSA encryption, which has been tested thoroughly. The issue here is the conversion and reversal, as it's the newest element, which I'm having issues with. If you'd insist, I'll get on to posting more of it.


Since you are not using the JCE I have to assume that this is a homework assignment.

Using your splitting approach one could end up with the last block having just one byte and if one uses RSA on a small value (such as 1 byte) then the private exponent can be easily deduced. This security problem is removed in real world RSA by using PKCS1 padding (Google is your friend) but it doesn't totally remove the leading zero problem because your the ciphertext generated for a block may itself have a leading zero so you will lose synchronization. This loss of synchronisation can be avoided by padding with zeros each block of ciphertext to a fixed length (normally the length of the RSA modulus).

Just for information, since this is probably homework I assume you can't use this approach but the normal approach to using RSA on large data is to use a hybrid approach. One generates a random key for use in a block cipher such as AES or Blowfish and one uses that cipher and random key to encrypt the data. One then only encrypts the random key with RSA. The encrypted output is then -

<RSA encrypted key length><RSA encrypted key><Block cipher encrypted PKCS5 padded data>

The RSA algorithm is very slow when compared to block cipher algorithms so this hybrid approach has the added advantage of being much faster for intermediate/long files than pure RSA.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The deafening silence from the OP makes me think that my assumption that this is a homework assignment is totally wrong and the OP is instead trying to avoid using the JCE !
 
chavdar yotov
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologise for the prolonged response!

It is indeed a homework assignment. I ended up solving my problem by incorporating a padding scheme, which goes around the signed bit which toByteArray() brings.

It is working magically well right now.

To anyone who would happen to have the same issue as me, since there aren't that many sources on the internet for this - I do recommend looking over complete implementations of the algorithm to get an idea of the approach, if it HAS to be fully hand made.


THank you Richard and Fred for the help, you guys have been absolutely marvellous! <3
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!