Hi all,
Im trying to create an AES Shared secret to encrypt and decrypt a message in J2ME. Ive already tried this code in J2SS and it works fine but when i try to use the exact same code in J2ME it doesnt work.

I get the following error : [B]java.security.InvalidKeyException[/B]

The following is the code that im using to create the shared secret key and encrypt a message:

ECDHBasicAgreement KeyAgreement = new ECDHBasicAgreement();
KeyAgreement.init(ecPrivateKey);
BigInteger IntKeyAgreement = KeyAgreement.calculateAgreement(ecPublicKey);

Byte[] ShareSecretkey = IntKeyAgreement.toByteArray();
SecretKeySpec = new SecretKeySpec(ShareSecretkey, 0, 16 ,"AES");

Cipher cipher = null;
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec);

countin = cipher.update(input, 0, input.length, ciphertext, 0);
countin += cipher.doFinal(input, 0, input.length, ciphertext, 0);

It works fine untill i get to - cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec) then it produce the error. The code creates a 128-bit AES key, so i dont understand why it doesnt accept the key.
Any ideas why this might be??
Is the something special you have to do with Cipher in J2ME that im dont know about?
I really appreciate any help ye can give me, cause ive been stuck on this for a few days. Thanks in advance

Recommended Answers

All 14 Replies

Can you please provide the stack trace of exception.

At first glance it seems your J2ME installation might need Highest Jurisdiction policy, which you might already be having in your J2SS installation.

In J2SDK you need to download latest US_export_policy.jar and local_policy.jar and place it in your \jre\lib\security, else it leads to similar error (ElGamal error). Would only be sure of this cause, if you debug the code and get the full stack trace.

Thanks for replying, I did forget to add Highest Jurisdiction policy within my J2ME. However even after adding it i still get following error:

java.security.InvalidKeyException
        at com.sun.satsa.crypto.BlockCipher.doInit(BlockCipher.java:117)
        at com.sun.satsa.crypto.AESCipher.init(+8)
        at javax.crypto.Cipher.init(+30)
        at javax.crypto.Cipher.init(+7)

Any other ideas why it might not be working??

I believe you must have kept new jars at correct place and have restarted your application.

Are you by any chance adding up the provider using anything like Security.addProvider API. If yes, try removing provider in finally block at the end of each transaction, and add up again in next transaction in some constructor probably.

Ya i have the .jars in the right place (even tho im only using a 128-bit key), restarted the application - still no joy.
No im not using any providers, cant use Security.addProvider in J2ME.

I didnt find out what the problem with my code was, but ive decide to try coding it a different way and it now works.
But thanks for your help.

can u tel me how to implement AES in j2me... m jus learnin j2me an hardly know anythin abt it... an is thr any facility of using DEFFIE HELLMAN key excahange or any certificate authority in j2me

Welcome to daniweb divs.cse!
There are many links you can find on google if you can make time to search for them. For example webdev.sis.pitt.edu, Java Cryptography Extension (JCE) or bouncy castle on mobilefish.com and many other rest of google search.
These should get you some start...

Also for the future plese use full-sentence English. Please do not use "leet" speak or "chatroom" speak.

I didnt find out what the problem with my code was, but ive decide to try coding it a different way and it now works.
But thanks for your help.

Hi Littlen,

Can you publish the J2ME code you used to encrypt when it worked for you ?

Cheers,

Arnnei

Hi Arnnei

Here is the code i used to encrypt with AES (using the Bouncy Castle library)

SecretKey SecretKey = new SecretKeySpec(ShareSecretkey, 0, 16, "AES");

AESLightEngine blockCipher = new AESLightEngine();
CFBBlockCipher cfbCipher = new CFBBlockCipher(blockCipher, 8);
streamCipher = new StreamBlockCipher(cfbCipher);
Secretkeyparams = new KeyParameter(SecretKey.getEncoded());

 byte[] ciphertext = new byte[MessageSize.length];
 streamCipher.init(true, Secretkeyparams);
 streamCipher.processBytes(MessageEncrypted, 0, MessageEncrypted.length, ciphertext, 0);

Hope that helps

Many thanks... I'm using the AES in J2ME.

I've now resolved the problem I had.

Cheers,

Arnnei

Many thanks... I'm using the AES in J2ME.

I've now resolved the problem I had.

Cheers,

Arnnei

hi..
i have already implemented AES in normal java program, but when i used the same code for j2me i have lot of errors.. all the packages i used in the code for cryptography was not supported..

package like below are not supported ,
"import javax.crypto.*;
import javax.crypto.spec.*;"

whether i have to download any other jar file for j2me. please help me..

can you pleas forword AES in normal java program using ecdh

commented: yes, let's all do your homework -3

itsmycake, this thread has been dead for several years. if you have a specific question: ask it. if you're working on J2ME: do yourself a favour, upgrade. J2ME has been out of date for eight years now.

Hi Everyone,
bouncy castle is very heavy weight to integrate in blackberry j2me and i tried twice to intergate bouncy castle but failed getting all errors secruity exception.is there any other way to solve this issue?.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.