java sha2加密算法,使用Java进行SHA2密码存储

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");

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

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.

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

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SHA-1是一种单向哈希函数,可以将任意长度的数据转换成160位的哈希值。在密码加密中,我们通常将用户输入的密码进行SHA-1哈希值计算,然后将计算出的哈希值存储到数据库中,以保证用户密码的安全性。 以下是一个使用Java语言实现SHA-1密码加密的示例代码: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Sha1PasswordEncryption { public static void main(String[] args) { String password = "123456"; String hashedPassword = sha1(password); System.out.println("原始密码:" + password); System.out.println("加密后密码:" + hashedPassword); } public static String sha1(String input) { try { MessageDigest messageDigest = MessageDigest.getInstance("SHA-1"); byte[] inputBytes = input.getBytes(); byte[] hashedBytes = messageDigest.digest(inputBytes); StringBuilder hashedStringBuilder = new StringBuilder(); for (byte hashedByte : hashedBytes) { hashedStringBuilder.append(Integer.toString((hashedByte & 0xff) + 0x100, 16).substring(1)); } return hashedStringBuilder.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } } ``` 在上述代码中,我们首先定义了一个字符串类型的原始密码password,然后调用sha1方法将其进行SHA-1哈希值计算。在sha1方法中,我们首先获取SHA-1的MessageDigest实例,然后将输入字符串input转换成字节数组inputBytes,通过调用digest方法对inputBytes进行哈希值计算得到字节数组hashedBytes,最后将hashedBytes转换成十六进制字符串hashedStringBuilder并返回。最终输出加密后的密码hashedPassword。 需要注意的是,在真实的应用场景中,我们通常会对哈希值进行加盐处理以增加密码的安全性。同时,SHA-1已经被证明存在一定的安全漏洞,因此在实际应用中建议使用更加安全的哈希算法,例如SHA-256、SHA-512等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值