`

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


分享到:
评论

相关推荐

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

    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自带方法实现RSA加密解密

    import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; public class RSAKeys { public static void main(String[] args) throws ...

    java密码加密

    import java.security.NoSuchAlgorithmException; public String encryptToMD5(String info) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(info.getBytes()); byte[] digestBytes ...

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

    Struts2是一个流行的Java Web应用程序框架,它继承和发展了Struts1.x的一些特性,同时又采用了WebWork框架的核心技术,使得Struts2在设计理念和技术实现上都有了显著的进步。 #### Struts2与WebWork的关系 Struts2...

    2022如何在Java处理PFX格式证书.docx

    import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.cert.Certificate; import java.security.cert.CertificateFactory; import java.security.cert.X509...

    MD5_java.rar_MD5 JAVA_java md5_md5_md5算法

    import java.security.NoSuchAlgorithmException; public class MD5Example { public static String getMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] ...

    各种java加密算法工具类

    import java.security.NoSuchAlgorithmException; public class MD5Util { public static String getMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] bytes = md....

    SHA-1-Java.rar_SHA1_java 算法_java文件指纹

    import java.security.NoSuchAlgorithmException; public class SHA1Example { public static void main(String[] args) { try { File file = new File("待计算文件的路径"); MessageDigest digest = ...

    数据进行sha1签名 数据防篡改

    2. 创建MessageDigest实例:使用`java.security.MessageDigest.getInstance("SHA1")`获取SHA1实例。 3. 更新数据:调用`digest.update()`方法添加要签名的数据。 4. 生成哈希值:通过`digest.digest()`方法获取哈希...

    Java加密技术详解(参考文档)

    import java.security.NoSuchAlgorithmException;public class HashUtil { public static String sha256(String data) throws NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance...

    MD5和RSA加密算法Java完成实现

    import java.security.NoSuchAlgorithmException; public class MD5Example { public static String getMD5(String input) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD...

    Java 生成RSA密钥进行数据加密解密 支持超长字符分区

    import java.security.NoSuchAlgorithmException; import javax.crypto.Cipher; // 保存密钥到文件 public void saveKeys(PublicKey publicKey, PrivateKey privateKey, String publicKeyPath, String ...

    单向加密算法的Java实现

    import java.security.NoSuchAlgorithmException; public class MD5Example { public static byte[] encryptMD5(byte[] data) throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance...

    创建pkcs12格式数字签名证书的Java代码

    在Java环境中,我们通常会借助第三方库如BouncyCastle来创建和处理这种格式的证书。本文将深入探讨如何使用BouncyCastle库在Java中创建PKCS12格式的数字签名证书。 首先,理解BouncyCastle库。BouncyCastle是一个...

    MD5加密java和android通用

    import java.security.NoSuchAlgorithmException; public class MD5Util { public static String encrypt(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] bytes = md....

    java随机生成数字 验证码

    import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.util.ArrayList; import java.util.HashMap; import java.util....

    Java加密组件实现DES、RSA和SHA加密算法

    import java.security.NoSuchAlgorithmException; public class SHAUtil { public static byte[] sha256(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance(...

    java进行加密

    import java.security.NoSuchAlgorithmException; public String calculateMD5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes...

    java完整性校验解决方案

    import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.security.Security; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util....

Global site tag (gtag.js) - Google Analytics