954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with AES encryption in J2ME

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 : java.security.InvalidKeyException

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

Littlen
Newbie Poster
10 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

ksaxena
Light Poster
31 posts since Jul 2007
Reputation Points: 10
Solved Threads: 4
 

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

Littlen
Newbie Poster
10 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

ksaxena
Light Poster
31 posts since Jul 2007
Reputation Points: 10
Solved Threads: 4
 

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.

Littlen
Newbie Poster
10 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Littlen
Newbie Poster
10 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

divs.cse
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 
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

arnneisp
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

Littlen
Newbie Poster
10 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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

I've now resolved the problem I had.

Cheers,

Arnnei

arnneisp
Newbie Poster
2 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

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

funnykd
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You