之前用的一个aes的加密解密在4.x-6.x的android版本手机上没有问题,然后有天用一个android7.0的手机测试的时候,突然就出现加密完后,取值的时候获取的是null,跟踪代码,发现加密存储的时候抛出异常:
no such provider: Crypto
根本就没存进去,具体的代码出错在下面这个方法中:
@SuppressLint("TrulyRandom")
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
原来android7.0中已经没有了“Crypto”,为了加密成功,只需要将以上方法中的
sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
替换为
sr = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());
那么getRawKey方法就变成以下的了:
@SuppressLint("TrulyRandom")
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = null;
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());
} else {
sr = SecureRandom.getInstance("SHA1PRNG");
}
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
这样还没完,我们需要自己写一个CryptoProvider类,CryptoProvider.java代码如下:
static final class CryptoProvider extends Provider {
/**
* Creates a Provider and puts parameters
*/
public CryptoProvider() {
super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)");
put("SecureRandom.SHA1PRNG",
"org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl");
put("SecureRandom.SHA1PRNG ImplementedIn", "Software");
}
}
ok,关于加密的就到这里了,谢谢诶。