Search code examples
javaandroidjava-meaes

AES encryption j2me


I am trying to do AES encryption in j2me.I used almost same code for android and it's working fine there.Following is the block of code. I'm getting null as output

package cartoon;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class MCrypt {

private String iv = "0123456789abcdef";// iv 
private IvParameterSpec ivspec;
private SecretKeySpec keyspec;
private Cipher cipher;
private String SecretKey = "fedcba9876543210";// secretKey 

public MCrypt() {
    ivspec = new IvParameterSpec(iv.getBytes(), 0, iv.getBytes().length);

    keyspec = new SecretKeySpec(SecretKey.getBytes(), 0, iv.getBytes().length, "AES");

}

String Decrypt(String text) throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    byte[] results = null;
    int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0);
    System.out.println("String resultssssssssssssss " + results1);
    return new String(results, "UTF-8");
}

String Encrypt(String text)
        throws Exception {
    cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    System.out.println("String input : " + text);

    cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
    byte[] results = null;
    int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0);
    return Base64.encode(results);
}
}

Printing result

MCrypt mcrypt = new MCrypt();
    try {
        encrypted = mcrypt.Encrypt("id=450");

        decrypted = new String(mcrypt.Decrypt(encrypted));
        System.out.println("Encrypted Text : " + encrypted);
        System.out.println("Decrypted Text : " + decrypted);

    } catch (Exception e) {
        e.printStackTrace();
    }

Where am I going wrong?


Solution

  • Try the following

    You should initialize the byte array first appropriately

    String Decrypt(String text) throws Exception {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
    
        // here
        byte[] results = cipher.doFinal(Base64.decode(text));
    
        int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0);
        System.out.println("String resultssssssssssssss " + results1);
        return new String(results, "UTF-8");
    }
    
    String Encrypt(String text)
        throws Exception {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
        System.out.println("String input : " + text);
    
        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
    
        // and here
        byte[] results = cipher.doFinal(text.getBytes());
    
        int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0);
        return Base64.encode(results);
    }