问题
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