`

java.security.NoSuchAlgorithmException

    博客分类:
  • Java
阅读更多
list 00001  Mac.getInstance

Mac mac = Mac.getInstance("HmacSHA1");

报错

java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available

原因,Eclipse 的项目的 jre 配置用的是jre, 改成 jdk 就好了

参照链接,另一个相似有启发的问题

http://stackoverflow.com/questions/2856248/nosuchalgorithmexception-algorithm-hmacsha1-not-available
====================================================================

below is the main content of the link above.

Question :

Look at the following line of java:

Mac.getInstance("HmacSHA1");

If I put this in a simple test program, it runs without problems on my server. However, if I use this line in a container, I get

java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available
  at javax.crypto.Mac.getInstance(DashoA13*..)


The same JDK installation is used in both cases.

After googling around a bit, I managed to get it to work by doing two things:

引用
Copying sunjce_provider.jar
from $JAVA_HOME/jre/lib/ext
to the lib directory of the container.

Adding the following line to my code:


java.security.Security.addProvider(new com.sun.crypto.provider.SunJCE());


Specifically, this happens to me in an Apache James
mailet, but I'm pretty sure this is has to do with JVM options. Here is the startup script
that it uses.

Although I got it to work in the end, the solution feels too hacked
to be the right one. I would appreciate an explanation of what is going
on, as well as a more "proper" solution.

Related question
: http://stackoverflow.com/questions/1009996/using-java-crypto-leads-to-nosuchalgorithmexception
.
However, in this case I'm pretty sure the HmacSHA1 algorithm should be
supported out of the box. As evidence, this works without problems in a
test program.

clew:

The startup script sets the java.ext.dirs
to its own set of directories (specific to the application) but omitting the "normal" extension directory ($JAVA_HOME/jre/lib/ext/
) which is where sunjce_provider.jar
resides. This explains your first point (copying the Jar file to the
lib directory makes it visible again). This is easily reproduced.

As for the second point, I think this is due the policy file that the startup script sets with the -Djava.security.policy
option. Whether some providers are available or not depends on policy
files. The default policy file makes the SunJCE provider available, but
since the startup scripts mandates a non-default, custom policy file,
then anything goes. I suggest you take a look at that policy file.

For instance, on my system (Ubuntu Linux, with Sun JVM 1.6.0_20 as packaged by Ubuntu), the default policy file is in /etc/java-6-sun/security/java.security
and contains (among others) the following lines:

security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
security.provider.3=com.sun.net.ssl.internal.ssl.Provider
security.provider.4=com.sun.crypto.provider.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=com.sun.security.sasl.Provider
security.provider.7=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.8=sun.security.smartcardio.SunPCSC

which define what providers should be available by default. From your
symptoms, I think that the custom policy file made SunJCE unavailable
unless explicitly registered (which is understandable since the startup
script also removed the access to the Jar file containing SunJCE...).


分享到:
评论

相关推荐

    jce_policy-8.AES加密版本.zip

    Exception in thread "main" java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/ECB/PKCS7Padding https://www.codeprj.com/blog/92cdc41.html

    智能识别收货地址Java.pdf

    import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util....

    jsse-2.0.5.jar.zip

    java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available at sun.security.jca.GetInstance.getInstance(GetInstance.java:142) at java.security.SecureRandom.getInstance(SecureRandom....

    struts2源码解析,个人感觉很不错

    下载的Struts2源代码文件是一个名叫struts-2.1.0-src.zip的压缩包,里面的目录和文件非常多,读者可以定位到struts-2.1.0-src"struts-2.0.10"src"core"src"main"java目录下查看Struts2的源文件

    ApigeeEdge-Java-SecureRandom:使用java.security.SecureRandom在Apigee Edge策略内生成伪随机数的Java标注

    标注使用java.security.SecureRandom在策略Apigee Edge代理内生成随机数(int,UUID或高斯值),并使用该信息设置上下文变量。免责声明此示例不是正式的Google产品,也不是正式的Google产品的一部分。使用此标注您...

    aesjava源码-THIS-.-IS-GREAT-:////IntelliJIDEA从.class文件重新创建的源代码//(由Fernflo

    aes java源码这很棒- // // IntelliJ IDEA从.class文件重新创建的源代码//(由...导入java.security.NoSuchAlgorithmException; 导入java.security.PrivilegedAction; 导入java.security.PrivilegedActionException

    文件加密解密算法(Java源码)

    import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.NoSuchPaddingException; import javax.crypto.BadPaddingException; import javax....

    RSA+bcpkix-jdk15.zip

    import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import...

    org.jasig.cas.client.util.CommonUtils

    public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String ...

    微信小程序配置服务器提示验证token失败的解决方法

    最近在学习微信小程序,遇到的第一个问题就是需要配置服务器 关于这个服务器的配置我也是绕了好多弯路,说白了腾讯就是想通过你填的这个URL和Token去...import java.security.NoSuchAlgorithmException; import java

    sunjce_provider.jar

    程序报错:Caused by: java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA256 SecretKeyFactory not available 就是缺少这个包

    Android md5加密与php md5加密一致详解

    在Android开发过程中加密密码常常采用md5加密方式,然而如果服务器端采用PHP开发(php采用md5加密很简单... import java.security.NoSuchAlgorithmException; public class MD5 { //密码加密 与php加密一致 public s

    android中AES加解密的使用方法

    今天在android项目中使用AES对数据进行加解密,遇到了很多问题,网上也找了很多资料,也不行。...import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Ciph

    local_policy.jar和US_export_policy.jar

    微信退款的解密时报java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/ECB/PKCS7Padding错误,囊括jdk1.6、1.7、1.8的local_policy.jar和US_export_policy.jar,用于替换jdk里的两...

    分享JavaScript与Java中MD5使用两个例子

    在网上查了一下,在网上收集了Java与JavaScript中使用的两个例子,试验过...import java.security.NoSuchAlgorithmException; public class MD5Utils { public static void main(String[] args){ System.out.println

    android中Sha256加密算法

    import java.security.NoSuchAlgorithmException; public class Sha256 { public static String getSHA256(String str){ MessageDigest messageDigest; String encodestr = ; try { messageD

    java生成MD5串

     import java.security.NoSuchAlgorithmException;  /**  * 采用MD5加密解密  *  * @authortfq  * @datetime2011-10-13  */  public class TestMD5 {  /***  * MD5加码生成32位md5码  */  ...

    android md5加密与rsa加解密实现代码

    import java.security.NoSuchAlgorithmException; public class MD5 { /* * MD5加密 */ public static String getDigest(String str) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest....

    Android 中 MD5 的几种生成方式(小结)

    Java 提供了 MessageDigest 类...import java.security.NoSuchAlgorithmException; public class Hash { private static final char[] hexCode = 0123456789ABCDEF.toCharArray(); public static String md5(Strin

Global site tag (gtag.js) - Google Analytics