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
Developer
swap_chau
Posts: 179
Registered: ‎06-15-2011
My Device: Storm 9550

Common encryption algorithm in Android and Blackberry

Hi all,

I am trying to find a common encryption mechanism which can work on both Android and blackberry. We have a server,which uses AES algorithm as shown below:

 

private static final byte[] SALT = {(byte) 0x09, (byte) 0xcb, (byte) 0x8d, (byte) 0x42, (byte) 0x61, (byte) 0x15,
            (byte) 0x0E, (byte) 0xD1};

    private static final int ITERATION_COUNT = 1024;

    private static final int KEY_LENGTH = 256;

    private static final byte[] IV_BYTE = {09, 46, -23, 16, 10, -35, -22, -100, 28, -45, -34, -22, -40, -09, -07, -25};

    private Cipher ecipher;

    private Cipher dcipher;

    private final String PASSPHRASE = "abcdef";

    public static AESEncrypter encrypter;

    private AESEncrypter() throws Exception {
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        KeySpec spec = new PBEKeySpec(PASSPHRASE.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        String cipherType = "AES/CFB8/NoPadding"; // AES/CBC/PKCS5Padding

        ecipher = Cipher.getInstance(cipherType);
        IvParameterSpec ivSpec = new IvParameterSpec(IV_BYTE);
        ecipher.init(Cipher.ENCRYPT_MODE, secret, ivSpec);

        dcipher = Cipher.getInstance(cipherType);
        dcipher.init(Cipher.DECRYPT_MODE, secret, ivSpec);
    }

    public String encrypt(String encrypt) throws Exception {
        byte[] bytes = encrypt.getBytes("UTF8");
        byte[] encrypted = encrypt(bytes);
        return new BASE64Encoder().encode(encrypted);
    }

    public byte[] encrypt(byte[] plain) throws Exception {
        return ecipher.doFinal(plain);
    }

    public String decrypt(String encrypt) throws Exception {
        byte[] bytes = new BASE64Decoder().decodeBuffer(encrypt);
        byte[] decrypted = decrypt(bytes);
        return new String(decrypted, "UTF8");
    }

    public byte[] decrypt(byte[] encrypt) throws Exception {
        return dcipher.doFinal(encrypt);
    }

 

In android, I have written the equivalent code, but in Blackberry I am finding it difficult to write eqivalent code.

 

How can I write an equivalent code in BB where I can use Salt, IV, and iteration count?