In my app, we have used CRYPTO provider to create random number.But it was removed in Android N. If app was relying on setSeed() to derive keys from strings, then we should switch to using SecretKeySpec to load raw key bytes directly OR use a real key derivation function (KDF). According to below link
Caused by: java.security.NoSuchProviderException: no such provider: Crypto - Android N
Now my problem is , all existing users using app with old algorithm(SHA1PRNG & CRYPTO provider). All app data has been encrypted using below algorithm and saved in "Shared Preference" and "SQLITE".
If I give an app update with new Encrpt/decrypt algorithm then app might crash when decrypting saved data from SQLITE and shared preference
Can anyone suggest a way to migrate older Encrpt/decrypt algorithm to new one without affecting user.
public static String encrypt(String seed, String clearText) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes(STR_ENCODE_UTF8));
byte[] result = encryptDecrypt(rawKey, clearText.getBytes(STR_ENCODE_UTF8), Cipher.ENCRYPT_MODE);
return new String(Base64.encode(toHex(result).getBytes(STR_ENCODE_UTF8), Base64.DEFAULT)).trim();
}
public static String decrypt(String seed, String encrypted) throws Exception {
String decodedStr = new String(Base64.decode(encrypted.trim(), Base64.DEFAULT));
byte[] rawKey = getRawKey(seed.getBytes(STR_ENCODE_UTF8));
byte[] enc = toByte(decodedStr);
byte[] result = encryptDecrypt(rawKey, enc, Cipher.DECRYPT_MODE);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws NoSuchAlgorithmException, NoSuchProviderException {
KeyGenerator kGen = KeyGenerator.getInstance(KEY_GENERATOR_ALGORITHM);
SecureRandom sr = SecureRandom.getInstance(STR_SHA1PRNG, CRYPTO);
sr.setSeed(seed);
kGen.init(128, sr);
SecretKey sKey = kGen.generateKey();
byte[] raw = sKey.getEncoded();
return raw;
}