mercredi 2 décembre 2015

How to encrypt without a random number in Android

I currently have an accessory that uses AES/CBC without a random number on the key. Instead, the message itself includes a random number and the key is hardcoded. I'm trying to do the same thing on my Android to exchange with the accessory through BLE. Somehow I can't figure out how generate a Key-class object without using a random number.

Here's an example of what I'd like to be able to do :

 public byte[] encrypt(byte[] key, byte[] input) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding ");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(input);
    }

Here's what I've tried :

public byte[] encrypt(byte[] key, byte[] input) throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    SecureRandom secureRandom = new SecureRandom(key);
    secureRandom.setSeed(key);
    keyGenerator.init(128, secureRandom);
    SecretKey secretkey = keyGenerator.generateKey();
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding ");
    cipher.init(Cipher.ENCRYPT_MODE, secretkey);
    return cipher.doFinal(input);
}
public byte[] encrypt(byte[] key, byte[] input) throws Exception {
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES/CBC/NoPadding ");
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding ");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    return cipher.doFinal(input);
}

Unfortunately both of those alter the key before the encryption.

How can I use my key "as is" ?

Thank you




Aucun commentaire:

Enregistrer un commentaire