Wrox Programmer Forums
|
BOOK: Beginning Cryptography with Java
This is the forum to discuss the Wrox book Beginning Cryptography with Java by David Hook; ISBN: 9780764596339
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Cryptography with Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 13th, 2007, 05:05 PM
Authorized User
 
Join Date: Jan 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default RSA and multiple block encryption

First of all, let me say that I know this isn't supposed to be done. But I'm being forced into this ;) Money is the root of all evil.

I'm encrypting with RSA. My data is more than one block long. I can post code if necessary but I'll try to show pseudo code first as I will have to obscure some data to post code, and I don't think its necessary anywho.

Can someone tell me what I'm doing wrong? Everything works fine when I do a smaller chunk ( one block or less ) and use doFinal(). But my multi-block implementation isn't working. Details below.

Code:
//key objects and variables 
cipher rsaCipher
int cipherBlockSize

byte[] plainText
int lengthOfPlainText
int remainingBytesToProcess = lengthOfPlainText;

while ( remaingBytesToProcess > cipherBlockSize )
{
    byte[] block = getTheNextSetOfBlockSizeBytesFrom plainText;

    //THIS DOESN'T RETURN ANYTHING?  API SAYS IT DOES BUT I NEVER GET 
    //ANYTIING RETURNED BY UPDATE WITH THIS RSA CIPHER
    rsaCipher.update( block);

}

//EXITING THE BLOCK I SHOULD HAVE JUST A REMAINING CHUNK, LESS THAN BLOCK SIZE

block = remainintBytes //NOTE ITS LESS THAN A FULL BLOCK SIZE;PROBLEM?

//THIS NEXT LINE BLOWS UP WITH THE STACK TRACE INCLUDED BELOW
byte[] cipherText = rsaCipher.doFinal( block );
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
        at org.bouncycastle.jce.provider.JCERSACipher.engineD oFinal(Unknown Source)
        at javax.crypto.Cipher.doFinal(DashoA12275)
        at com.blackdog.testing.encryption.JFSmartCardTest.ma in(JFSmartCardTest.java:105)


 
Old February 13th, 2007, 05:40 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

You can only process one block of data with RSA. The amount of data is limited by the key size and the padding mechanism chosen.

With the update, the method will only return something when enough input data has been feed into the cipher.

Regards,

David

 
Old February 13th, 2007, 05:53 PM
Authorized User
 
Join Date: Jan 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:You can only process one block of data with RSA. The amount of data is limited by the key size and the padding mechanism chosen.
So, to do multiple blocks of RSA you essentially have to make a new cipher and do another block. I guess this speaks to the irregularity of what I'm doing. So I'll just put my cipherTextBlocks together after I've created them all. This makes me wonder how my "other party" will decode this.

Quote:
quote:With the update, the method will only return something when enough input data has been feed into the cipher.
Does this imply that the update() method is just not needed by the RSA cipher then?

 
Old February 13th, 2007, 06:46 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Update allows you to accumulate data in the cipher, so it's not so much that it's not needed, it's probably not needed in your case.

For what you're trying to do successive calls to doFinal would probably be the way to go, there's no need to create a Cipher object for each step. I'm assuming you've told whoever it is that's making you do this that it reduces the security of the encryption (make sure you get it in writing, if your work gets audited later, special circumstances not withstanding, this will not look good).

Regards,

David

 
Old February 14th, 2007, 01:24 PM
Authorized User
 
Join Date: Jan 2007
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does the RSA with PKCS1Padding *output* a fixed block size? In other words, when decoding a multiple block chunk, is it possible to distinguish the blocks?

 
Old February 14th, 2007, 06:39 PM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Yes - the output size is constant.

Regards,

David

 
Old June 4th, 2007, 03:17 AM
Registered User
 
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have an encrypted byte array which is 66 bytes long.
My private key which I need to decrypt is stored in a file and is 344 bytes long. It supports 64 byte blocks.

All the methods update(), doFinal(), unwrap() do not function. I always get the following (in this case I used unwrap):

Code:
java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
        at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(Unknown Source)
        at org.bouncycastle.jce.provider.WrapCipherSpi.engineUnwrap(Unknown Source)
        at javax.crypto.Cipher.unwrap(DashoA6275)
        at key.calculateMasterSecret(key.java:494)
        at key.<init>(key.java:100)
        at key.main(key.java:117)
What should I do? I tried to split up the encrypted data into one 64 byte and one 2 byte block and used the 3 methods again, but same failure raised.

The private key is in PKCS#8 DER (binary) format.
The encrypted data are encrypted using "RSA/ECB/PKCS1Padding".
 
Old June 4th, 2007, 07:26 AM
dgh dgh is offline
Wrox Author
 
Join Date: Aug 2005
Posts: 206
Thanks: 0
Thanked 20 Times in 20 Posts
Default

Exactly how many bits is your private key? Assuming it's 512 bits the data size that you have been given does not make any sense. If the data does represent 2 blocks they would be 33 bytes each.

RSA is not a regular block cipher, it should not be used for encrypting multiple blocks one after the other, and even if it was used that way it certainly wouldn't work like this!

Regards,

David

 
Old June 5th, 2007, 03:51 AM
Registered User
 
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I only had to use the last 64 bytes of the encrypted data.
I don't know why, but it functioned...





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem With RSA Interface Floetic Java GUI 1 March 25th, 2008 06:14 AM
Intermittent RSA block problems psearls55 BOOK: Beginning Cryptography with Java 5 July 17th, 2007 07:54 PM
RSA Decryption - Chapter 4 amalvido BOOK: Beginning Cryptography with Java 2 January 22nd, 2007 08:59 PM
BLOCK MULTIPLE LOGIN dayarprasad Classic ASP Databases 3 July 25th, 2006 11:01 PM
RSA ajm235 C++ Programming 1 August 19th, 2004 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.