Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 155
Registered: ‎01-22-2010
My Device: PlayBook -> 9900, 9810, 9800, 9700,9000, 8800, 8700...
My Carrier: AT&T

CBCEncryptor throwing IllegalArgumentException

I'm in the midst of converting a BB SSH app to use the native BB crypto API instead of the custom code it currently uses.  The work is nearly done, but I have run into a snag.

 

In the code below, new CBCEncryptor is throwing an IllegalArgumentException. Does anyone have any thoughts?  The basic background:

  • IVc2s is 20 bytes long and populated with an SHA1 hash of other values in the app.  (Calculation of hash is according to the SSH RFC)
  • Ec2s is 40 bytes long is is populated with a concatentation of two SHA1 hashes.    (Calculation of the key is according to the SSH RFC)
  • Creation of the key, the 3des engine, and the IV all succeed; the parameters to the cBCDecryptorENgine are valid.
  • I was initially acquiring the 3des engine via the DecyptorFactory, but receieved the same exception in the CBC constructor -this leads me to think it's related to the IV, perhaps..
function setupCrypto(byte[] IVc2s, byte[] Ec2s){
    try {
		TripleDESKey keyDe = new TripleDESKey(Es2c);
		InitializationVector iv= new InitializationVector(IVs2c);
		TripleDESDecryptorEngine tripleDESDecryptor = new TripleDESDecryptorEngine(keyDe);
                 // Exception thrown on this line. 
		BlockDecryptorEngine encryptor = new CBCDecryptorEngine(tripleDESDecryptor, ivdecrypt);

			
    } catch (CryptoException ex) { 
 // doing something here 
   } 
}


Try out BBSSH, a free Blackberry SSH client.
  • If you like my post, please let me know by Liking it!
  • If my post solved your problem please click on the Accept as Solution button.

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: CBCEncryptor throwing IllegalArgumentException

Something about this suggests that there is a mix-up somewhere between encryption and decryption code. The variables passed to new TripleDESKey() and new InitializationVector() aren't defined in your comments or the code. Are you sure that what you posted is what's executing?

 

Is there any message with the IllegalArgumentException?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: CBCEncryptor throwing IllegalArgumentException

My 3DES is a bit rusty, but shouldn't the key length be 192 bit and the IV length be 64 bit?

Developer
Posts: 155
Registered: ‎01-22-2010
My Device: PlayBook -> 9900, 9810, 9800, 9700,9000, 8800, 8700...
My Carrier: AT&T

Re: CBCEncryptor throwing IllegalArgumentException

Thanks for the replies so far. 

Ted_Hopp: I did confirm that everything is passed in correctly - the IV and key data are populated and are not in any way reversed.  Unfortunately, there's no accompanying message with the IllegalArgumentException.

 

I believe it will ignore anything extra in this case. 

 

Now - I was able to get this working, by shuffling things around a bit:

 

            BlockEncryptorEngine ee = EncryptorFactory.getBlockEncryptorEngine(key,
                    "TripleDES", iv);

            BlockEncryptorEngine encrypt = new CBCEncryptorEngine(ee);

 

And this does work without any issues.

 

When I previously tried to use EncrpytorFactory and failed, usage was as follows:

 

         BlockEncryptorEngine ee = EncryptorFactory.getBlockEncryptorEngine(key,
                    "TripleDES");

            BlockEncryptorEngine encrypt = new CBCEncryptorEngine(ee, iv);

 

 

So - while the change above does resolve the issue, I'm still going to leave the question open in case someone has any insights as to why the CBC engine can't accept the IV, but the DES engine can.



Try out BBSSH, a free Blackberry SSH client.
  • If you like my post, please let me know by Liking it!
  • If my post solved your problem please click on the Accept as Solution button.