使用Java进行SHA2密码存储 [英] SHA2 password storage with Java

查看:407
本文介绍了使用Java进行SHA2密码存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行需要HmacSHA-256散列特定字符串的XML-RPC调用。我目前正在使用带有以下代码的Jasypt库:

I'm attempting to make a XML-RPC call that requires HmacSHA-256 hashing of a particular string. I'm currently using the Jasypt library with the following code:

StandardPBEStringEncryptor sha256 = new StandardPBEStringEncryptor();
          sha256.setPassword(key);
          sha256.setAlgorithm("PBEWithHmacSHA2");

在尝试使用sha256.encrypt(字符串)时出现此错误:

On trying to use sha256.encrypt(string) I get this error:

Exception in thread "main" org.jasypt.exceptions.EncryptionInitializationException: java.security.NoSuchAlgorithmException: PBEWithHmacAndSHA256 SecretKeyFactory not available
     at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:597)
     at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.initialize(StandardPBEStringEncryptor.java:488)
     at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:541)
     at nysenateapi.XmlRpc.main(XmlRpc.java:52)
    Caused by: java.security.NoSuchAlgorithmException: PBEWithHmacAndSHA256 SecretKeyFactory not available
     at javax.crypto.SecretKeyFactory.(DashoA13*..)
     at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
     at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.initialize(StandardPBEByteEncryptor.java:584)
     ... 3 more

我下载了JCE Cryptography扩展并将jar放在我的buildpath中,但这似乎没有做任何事情。我尝试在上面的setAlgorithm中使用了许多组合,包括PBE,PBEWithSha(1 | 2 | 128 | 256)?,PBEWithHmacSha等。

I downloaded the JCE Cryptography extension and placed the jars in my buildpath, but that doesn't seem to have done anything. I've tried using a number of combinations in setAlgorithm above, including "PBE", "PBEWithSha"(1|2|128|256)?, "PBEWithHmacSha", etc.

我也尝试过使用BouncyCastle,但我也没有运气。任何帮助或指导表示赞赏!

I also tried using BouncyCastle but I didn't have any luck there either. Any help or guidance appreciated!

推荐答案

正如@Rook正确指出的那样,您需要指定包含的PBE算法加密算法。其中两个例子是<$ h $ => http://download.oracle.com/javase/6/docs/technotes/支持的PBEWithSHA1AndDESede指南/安全/ SunProviders.html#SunJCEProviderrel =nofollow> SunJCE提供商和PBEWITHSHA256AND128BITAES-CBC-BC这是由Bouncycastle JCE支持的提供者。

As correctly noted by @Rook you need to specify a PBE algorithm that includes an encryption algorithm. Two examples out of many are "PBEWithSHA1AndDESede" which is supported by the SunJCE provider and "PBEWITHSHA256AND128BITAES-CBC-BC" which is supported by the Bouncycastle JCE provider.

这篇关于使用Java进行SHA2密码存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆