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