質問

特定の文字列をHMACSHA-256ハッシュする必要があるXML-RPCコールを作成しようとしています。現在、次のコードを使用して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 Cryptography Extensionをダウンロードして、JarsをBuildPathに配置しましたが、それは何もしていないようです。上記のSetalgorithmで、「PBE」、「PBEWITHSHA」(1 | 2 | 128 | 256)?、「PBEWITHHMACSHA」など、多くの組み合わせを上記で使用しようとしました。

また、Bouncycastleを使用してみましたが、そこにも運がありませんでした。どんな助けやガイダンスが高く評価されています!

役に立ちましたか?

解決

@Rookで正しく指摘されているように、あなたはを含むPBEアルゴリズムを指定する必要があります 暗号化 アルゴリズム。多くの例は2つです "PBEWithSHA1AndDESede" これはサポートされています Sunjceプロバイダー"PBEWITHSHA256AND128BITAES-CBC-BC" これは、Bouncycastle JCEプロバイダーによってサポートされています。

他のヒント

コメントは役に立ちましたが、私は間違った質問をしていたと思います。私がやりたいと思っていたのは、PHP関数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);
}

ただし、ガイダンスをありがとう。確かに将来私を助けてくれるでしょう。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top