Questions Thread - June 14, 2016

Problems using cryptography on Android N

As you probably know, Google just deprecated the crypto provider on Android N (see http://android-developers.blogspot.co.uk/2016/06/security-crypto-provider-deprecated-in.html). As result, my current implementation is broken, even if I checked the code they suggest to use. I did something like: public class MainActivity extends AppCompatActivity { private int ITERATIONS = 1; // trust me, I'll do more! private int KEYSIZE = 256;

public static String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC"; //public static String algorithm = "PBKDF2WithHmacSHA1";

protected Key key;

private String secretMessage = "that's my secret message";

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

try {
    generateSK("passw0rd".toCharArray(), generateSalt());
    byte[] encrypted = getCipher(Cipher.ENCRYPT_MODE).doFinal(secretMessage.getBytes());
    Log.d("DEBUG","secretMessage is "+secretMessage);
    Log.d("DEBUG","encrypted is "+toHexString(encrypted));
    byte[] decrypted = getCipher(Cipher.DECRYPT_MODE).doFinal(encrypted);
    Log.d("DEBUG","decrypted is "+new String(decrypted));
}
catch (Exception e) {
    e.printStackTrace();
}

}

private byte[] generateSalt() { SecureRandom sr = new SecureRandom(); byte[] output = new byte[KEYSIZE/8]; sr.nextBytes(output); return output; }

private void generateSK(char[] passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);

KeySpec spec = new PBEKeySpec(passPhrase,salt,ITERATIONS, KEYSIZE);
SecretKey secretKey = secretKeyFactory.generateSecret(spec);

key = new SecretKeySpec(secretKey.getEncoded(), algorithm);

}

private Cipher getCipher(int mode) { try { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(mode, key);

    return cipher;
}catch (Exception e) {
    e.printStackTrace();
    return null;
}

}

public static String toHexString(byte[] bytes) { char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char[] hexChars = new char[bytes.length * 2]; int v; for ( int j = 0; j < bytes.length; j++ ) { v = bytes[j] & 0xFF; hexChars[j2] = hexArray[v/16]; hexChars[j2 + 1] = hexArray[v%16]; } return new String(hexChars); } } This works perfectly till Marshmallow, on Android N (on emulator) I got this error: 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: java.security.InvalidKeyException: Algorithm requires a PBE key 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:564) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:1006) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:2524) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.tryCombinations(Cipher.java:2431) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher$SpiAndProviderUpdater.updateAndGetSpiAndProvider(Cipher.java:2336) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.chooseProvider(Cipher.java:486) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.init(Cipher.java:732) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.init(Cipher.java:673) 06-14 15:40:37.144 23568-23568/net.rehacktive.testcrypto W/System.err: at net.rehacktive.testcrypto.MainActivity.getCipher(MainActivity.java:73) If I try to change the algorithm to PBKDF2WithHmacSHA1 (as suggested), the error is: 06-14 15:44:27.534 24905-24905/net.rehacktive.testcrypto W/System.err: java.security.NoSuchAlgorithmException: No provider found for PBKDF2WithHmacSHA1 06-14 15:44:27.534 24905-24905/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.createCipher(Cipher.java:449) 06-14 15:44:27.534 24905-24905/net.rehacktive.testcrypto W/System.err: at javax.crypto.Cipher.getInstance(Cipher.java:333) 06-14 15:44:27.534 24905-24905/net.rehacktive.testcrypto W/System.err: at net.rehacktive.testcrypto.MainActivity.getCipher(MainActivity.java:72) What should I change? Any help will be appreciated.

/r/androiddev Thread