最近项目中在进行加密是遇到个问题
SecretKeyFactory skf = SecretKeyFactory.getInstance
("PBEWithMD5AndTripleDES");
执行以上代码时,报错:
org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: SecretKeyFactory PBEWithMD5AndTripleDES implementation not found
执行查看遍历加密算法:
for(Object obj:java.security.Security.getAlgorithms("Cipher")){
Log.e("加密",obj.toString());
}
//打印结果
PBEWITHSHAAND192BITAES-CBC-BC
AES/CBC/PKCS5Padding
PBEWITHSHAAND40BITRC4
AES/CFB/NoPadding
DESEDE/CBC/NoPadding
PBEWITHSHAAND128BITRC2-CBC
PBEWITHSHA256AND256BITAES-CBC-BC
RSA/ECB/NoPadding
PBEWITHSHAAND128BITAES-CBC-BC
DESEDE/ECB/NoPadding
PBEWITHSHAAND40BITRC2-CBC
AES/ECB/PKCS5Padding
AES/OFB/PKCS5Padding
PBEWITHSHA1ANDDES
AESWRAP
AES/CBC/NoPadding
DES
AES
DESEDEWRAP
DESEDE/CFB/NoPadding
PBEWITHSHAAND256BITAES-CBC-BC
PBEWITHSHA256AND192BITAES-CBC-BC
PBEWITHMD5AND128BITAES-CBC-OPENSSL
AES/ECB/NoPadding
PBEWITHMD5AND256BITAES-CBC-OPENSSL
BLOWFISH
DESEDE/OFB/PKCS5Padding
DESEDE/OFB/NoPadding
PBEWITHSHA1ANDRC2
PBEWITHSHAAND2-KEYTRIPLEDES-CBC
PBEWITHMD5AND192BITAES-CBC-OPENSSL
ARC4
DESEDE/ECB/PKCS5Padding
DESEDE/CBC/PKCS5Padding
AES/CTR/NoPadding
PBEWITHMD5ANDDES
DESEDE/CFB/PKCS5Padding
RSA
PBEWITHSHA256AND128BITAES-CBC-BC
PBEWITHSHAAND3-KEYTRIPLEDES-CBC
DESEDE
PBEWITHSHAANDTWOFISH-CBC
PBEWITHSHAAND128BITRC4
RSA/ECB/PKCS1Padding
AES/OFB/NoPadding
AES/CFB/PKCS5Padding
PBEWITHMD5ANDRC2
AES/CTR/PKCS5Padding
打印发现没有PBEWithMD5AndTripleDES这个算法
经过一阵百度,最终终于解决了问题
在自己jdk目录下 (jdk1.8.0_73\jre\lib\ext\sunjce_provider.jar) 找到sunjce_provider.jar,将这个jar包放入自己项目libs,并添加依赖,并在代码中修改成以下代码
Class cryptoClass = Class.forName("com.sun.crypto.provider.SunJCE");
Provider SunCrypto = (Provider) cryptoClass.newInstance();
Security.insertProviderAt(SunCrypto, 1);
SecretKeyFactory skf = SecretKeyFactory.getInstance
("PBEWithMD5AndTripleDES", "SunJCE");
遍历加密算法结果会发现多了 PBEWithMD5AndTripleDES这个算法
......
PBEWITHMD5ANDDES
AES_192/OFB/NoPadding
PBEWithHmacSHA256AndAES_256
PBEWithMD5AndTripleDES
PBEWithHmacSHA512AndAES_256
AES_256/ECB/NoPadding
PBEWITHSHAAND128BITRC4
PBEWithMD5AndDES
AES_192/CBC/NoPadding
AES/CFB/PKCS5Padding
PBEWITHMD5ANDRC2
PBEWithSHA1AndRC4_40
再编译就大功告成了
网友评论