使用 Java 存储 SHA2 密码
我正在尝试进行 XML-RPC 调用,该调用需要对特定字符串进行 HmacSHA-256 哈希处理。我目前正在使用带有以下代码的 Jasypt 库:
StandardPBEStringEncryptor sha256 = new StandardPBEStringEncryptor();
sha256.setPassword(key);
sha256.setAlgorithm("PBEWithHmacSHA2");
在尝试使用 sha256.encrypt(string) 时,我收到此错误:
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 加密扩展并将 jar 放在我的构建路径中,但这似乎没有做了任何事。我已经尝试在上面的 setAlgorithm 中使用多种组合,包括“PBE”、“PBEWithSha”(1|2|128|256)?、“PBEWithHmacSha”等。
我也尝试使用 BouncyCastle,但我没有任何组合那里也有运气。任何帮助或指导表示赞赏!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 @Rook 所正确指出的,您需要指定包含加密算法的 PBE 算法。其中两个示例是
"PBEWithSHA1AndDESede"
,它由 SunJCE 提供程序 和 Bouncycastle JCE 提供程序支持的"PBEWITHSHA256AND128BITAES-CBC-BC"
。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.这些评论很有帮助,但我想我问了错误的问题。我想做的是模仿 PHP 函数 hash_hmac('sha256',string,key)...
我最终使用了以下代码:
不过,谢谢您的指导。将来肯定会对我有帮助。
The comments were helpful but I guess I was asking the wrong question. What I was looking to do was mimic the PHP function hash_hmac('sha256',string,key)...
I ended up using the following code:
Thanks for the guidance, though. Will surely help me in the future.