3

Hi I am using Robolectric for test cases. I am facing some issue while simulating for encryption relateed test cases. I tried to use cipher with AES for encryption. And it is giving me some error. I tried it in following manner :

    @Test
public void testGet() {
    Cipher cipher = null;
    try {
        SecretKey sks= getKeySpec(pass, salt);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sks);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }
}

public SecretKey getKeySpec(char[] pass, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {

    //generate key spec...
    return secretKeyFactory.generateSecret(keySpec);
}

It gives me following error :

Illegal key size or default parameters

I already added JCE for illegal size exception. It is working if I run it on device and properly working in my application. Only thing when I try with robolectric it is giving me this error.Need some help. Thank you.

nilkash
  • 7,408
  • 32
  • 99
  • 176

1 Answers1

2

The reason behind this is Robolectric runs on JVM. JVM supports up to 128 bit key encryption only. Therefore if you are using 256 bit key encryption you need to use Java Cryptography Extension (JCE). follow this answer to how to do that

Community
  • 1
  • 1
ChaturaM
  • 1,507
  • 18
  • 32