NoSuchAlgorithmException:算法 HmacSHA1 不可用

发布于 2024-09-01 14:00:38 字数 1163 浏览 2 评论 0原文

看一下 java 的以下行:

Mac.getInstance("HmacSHA1");

如果我将其放入一个简单的测试程序中,它在我的服务器上运行没有问题。但是,如果我在容器中使用此行,则会

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

在两种情况下使用相同的 JDK 安装。

经过一番谷歌搜索后,我设法通过做两件事让它工作:

  1. sunjce_provider.jar$JAVA_HOME/jre/lib/ext 复制到 lib 目录容器。
  2. 将以下行添加到我的代码中:

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

具体来说,这发生在我身上 Apache James mailet,但我很确定这与 JVM 选项有关。 这是启动脚本< /a> 它使用的。

虽然我最终让它工作了,但这个解决方案感觉太糟糕了,不是正确的解决方案。我希望能够解释正在发生的事情,以及更“正确”的解决方案。

相关问题使用Java加密导致NoSuchAlgorithmException 。然而,在这种情况下,我非常确定 HmacSHA1 算法应该得到开箱即用的支持。作为证据,这在测试程序中没有问题。

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:

  1. Copying sunjce_provider.jar from $JAVA_HOME/jre/lib/ext to the lib directory of the container.
  2. 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: 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风吹雪碎 2024-09-08 14:00:38

启动脚本将 java.ext.dirs 设置为其自己的一组目录(特定于应用程序),但省略“正常”扩展目录($JAVA_HOME/jre/lib/ext/ ),这是 sunjce_provider.jar 所在的位置。这解释了您的第一点(将 Jar 文件复制到 lib 目录使其再次可见)。这很容易复制。

至于第二点,我认为这是由于启动脚本使用 -Djava.security.policy 选项设置的策略文件所致。某些提供程序是否可用取决于策略文件。默认策略文件使 SunJCE 提供程序可用,但由于启动脚本强制使用非默认的自定义策略文件,因此一切正常。我建议您看一下该策略文件。

例如,在我的系统(Ubuntu Linux,由 Ubuntu 打包的 Sun JVM 1.6.0_20)上,默认策略文件位于 /etc/java-6-sun/security/java.security 中包含(除其他外)以下几行:

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

定义默认情况下可用的提供程序。从您的症状来看,我认为自定义策略文件使 SunJCE 不可用,除非显式注册(这是可以理解的,因为启动脚本还删除了对包含 SunJCE 的 Jar 文件的访问...)。

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...).

攒眉千度 2024-09-08 14:00:38

正确的缩写形式如下

HmacMD5
HmacSHA1
HmacSHA256

添加到上面,keycloak 在其 UI 上显示为 SHA1、SHA256 等,但如果您从浏览器检查,它实际上是在更新这些值 HmacSHA1、HmacSHA256代码>

The correct shortened form is as below

HmacMD5
HmacSHA1
HmacSHA256

To add to above, keycloak displays as SHA1, SHA256 etc on it's UI but if you inspect from browser, it is actually updating these values HmacSHA1, HmacSHA256

爱要勇敢去追 2024-09-08 14:00:38

它被缩短为 SHA1、MD5 和 SHA256

It was shortened to SHA1, MD5, and SHA256

花开雨落又逢春i 2024-09-08 14:00:38

尝试更改 Java 版本,

我在以下 Java 版本上遇到异常 NoSuchAlgorithmException:“无法获取 JCA MAC 算法 'HmacSHA512'”

java版本“1.8.0_131”
Java(TM) SE 运行时环境(内部版本 1.8.0_131-b11)
Java HotSpot(TM) 64 位服务器 VM(内部版本 25.131-b11,混合模式)

将 JDK 版本更改为以下版本后,问题得到解决:

java版本“1.8.0_45”
Java(TM) SE 运行时环境(内部版本 1.8.0_45-b15)
Java HotSpot(TM) 64 位服务器 VM(内部版本 25.45-b02,混合模式)

此问题所需的 jar 是 sunjce_provider.jar,它可能已损坏。

Try to change the Java version

I was getting exception NoSuchAlgorithmException: "Unable to obtain JCA MAC algorithm 'HmacSHA512'" on following Java version:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

After changing the JDK version to following the issue was resolved:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

The required jar for this issue is sunjce_provider.jar it is possible that it might be corrupt.

暖阳 2024-09-08 14:00:38

我遇到了同样的错误,当尝试列出所有密钥时,我没有注意到错误:

“JKS 密钥库使用专有格式。建议使用“keytool -importkeystore”迁移到 PKCS12,这是一种行业标准格式-srckeystore /Users/matua/.keystore -destkeystore /Users/matua/.keystore -deststoretype pkcs12”。

所以我所做的是将梯形校正类型更改为 PKCS #12 而不是 JKS,一切都很顺利。
我使用 KeysStore Explorer 5.4.4 通过 GUI 生成密钥库。

I was getting the same error, and when was trying to list all my keys I was not noticing an error:

"The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore /Users/matua/.keystore -destkeystore /Users/matua/.keystore -deststoretype pkcs12".

So what I did was changing a keystone type to PKCS #12 instead of JKS and all went smooth.
I used KeysStore Explorer 5.4.4 to generate keystore via GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文