SHA2密码存储Java
-
09-10-2019 - |
题
我在尝试做一个XML-RPC呼吁,要求HmacSHA-256散列的特定string.目前,我正在使用的Jasypt图书馆与下列代码:
StandardPBEStringEncryptor sha256 = new StandardPBEStringEncryptor();
sha256.setPassword(key);
sha256.setAlgorithm("PBEWithHmacSHA2");
在试图使用sha256。加密(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密码扩展和安置的罐子在我的buildpath,但是,似乎没有做任何事情。我已经尝试采用一个数量组合在setAlgorithm述,包括"PBE","PBEWithSha"(1/2/128/256)?, "PBEWithHmacSha",等等。
我还试图使用BouncyCastle但我没有任何运。任何帮助或指导的感谢!
解决方案
如@Rook正确指出的,您需要指定包含一个的PBE算法 加密 算法。许多例子中有两个例子 "PBEWithSHA1AndDESede"
这是由 Sunjce提供商 和 "PBEWITHSHA256AND128BITAES-CBC-BC"
由Bouncycastle JCE提供商支持。
其他提示
该评论是有帮助的,但我想我是问错了问题。什么我想要做的不过是模仿PHP function hash_hmac('sha256'string key)...
我最终使用下列代码:
Mac mac = Mac.getInstance("HmacSha256");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), "HmacSha256");
mac.init(secret);
byte[] shaDigest = mac.doFinal(phrase.getBytes());
String hash = "";
for(byte b:shaDigest) {
hash += String.format("%02x",b);
}
谢谢你的指导,虽然。一定会帮助我的未来。
不隶属于 StackOverflow