SecretKeyFactory.getInstance() throws exception for all algorithms in unit tests

主宰稳场 提交于 2020-01-14 09:17:14

问题


By some reason I always get exception in unit test when calling SecretKeyFactory.getInstance() no matter what algorithm is specified. For example:

SecretKeyFactory.getInstance("PBEWITHMD5ANDDES")

com.mhe.connect.util.EncryptionException: java.security.NoSuchAlgorithmException: PBEWITHMD5ANDDES SecretKeyFactory not available

At the same time, I see that Security.getProviders() returns me needed algorithms:

SECRETKEYFACTORY.DESEDE SunJCE
SECRETKEYFACTORY.PBEWITHMD5ANDDES SunJCE
SECRETKEYFACTORY.DES SunJCE
SECRETKEYFACTORY.PBEWITHMD5ANDTRIPLEDES SunJCE
SECRETKEYFACTORY.PBKDF2WITHHMACSHA1 SunJCE
SECRETKEYFACTORY.PBEWITHSHA1ANDDESEDE SunJCE
SECRETKEYFACTORY.PBEWITHSHA1ANDRC2_40 SunJCE

This happens only in unit tests (from Maven, or IDE) and works fine if code is running under Jetty (that is run from same Maven/JDK).

What could be the reason for such behavior?


回答1:


Ran into the same issue today. Solved it by adding @PowerMockIgnore ("javax.crypto.*") to the class.

Does anyone know if I can globally ignore this package? I don't want to add it to every class that executes code from that package.




回答2:


Thanks, @T3rm1 that solved my problem!

turns out that running

SecretKeyFactory.getInstance(<algorithm>)

in a test environment with powermockito the javax.crypto.* must be mocked, and thus throwing java.security.NoSuchAlgorithmException in my case

javax.crypto.SecretKeyFactory no such algorithm PBKDF2WithHmacSHA1


来源:https://stackoverflow.com/questions/20805236/secretkeyfactory-getinstance-throws-exception-for-all-algorithms-in-unit-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!