Android java.security.NoSuchProviderException: no such provider: Crypto

原文地址:http://blog.163.com/jr_yijie/blog/static/239835922201721365436916/

由于项目的优化改进,用到AES+RSA加密传输数据。于是,在网上摘录了网友们的AES算法,如下:
public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}

public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}

public static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
sr.setSeed(seed);
kgen.init(128, sr);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
一切正常的在Android 4.3-6.1的手机上加解密,但是我用 LGE Nexus 5X (7.1.1 API 25)上发现在Android N上 google去掉了Crypto provider,意味着我们将不能继续像上面那样对数据加密填充。当然,在studio里的Logcat里会提示前往关于Android N对Crypto的解决方案:
http://android-developers.blogspot.com/2016/06/security-crypto-provider-deprecated-in.html
----------------------------------
另外,我在一篇文章里最终找到了解决方案:
http://stackoverflow.com/questions/39097099/security-crypto-provider-deprecated-in-android-n/42337802#42337802 
经过测试,改方案不仅解决了7.0的问题,也向下兼容了4.2-6.1的手机系统。(具体为什么能解决,原谅博主对Android 源码不是很深究,特别是security这块儿。 Android java.security.NoSuchProviderException: no such provider: Crypto - jr_yijie - jr_yijie的博客 )

好了,这次困扰了近2小时的问题总算解决了 Android java.security.NoSuchProviderException: no such provider: Crypto - jr_yijie - jr_yijie的博客 。希望也能帮助到各位小伙伴。

解决方法:

You can use this provider replacing "Crypto" for SecureRandom, its working for me fine:

Use,

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", new CryptoProvider());

instead of,

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");

and your CryptoProvider class like as below,

import java.security.Provider;
/**
 * Implementation of Provider for SecureRandom. The implementation     supports the
 * "SHA1PRNG" algorithm described in JavaTM Cryptography Architecture, API
 * Specification & Reference
*/
public 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");
}
}


你可能感兴趣的:(Android错误总结,加解密)