在我的应用程序中,我们使用CRYPTO提供程序创建随机数。但是它已在Android N中删除。如果app依赖于setSeed()从串行派生键,那么我们应该切换到使用SecretKeySpec直接加载原始键字节OR使用真正的密钥派生函数(KDF)。根据以下链接
现在我的问题是,所有现有用户都使用带有旧算法的应用程序(SHA1PRNG和CRYPTO提供程序)。所有应用数据均已使用以下算法加密,并保存在“共享首选项”和“SQLITE”中。
如果我使用新的加密/解密算法更新应用程序,那么在解密来自SQLITE和共享首选项的已保存数据时应用程序可能会崩溃
任何人都可以建议一种方法将旧的加密/解密算法迁移到新的算法而不影响用户。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;}