Using Java crypto leads to NoSuchAlgorithmException

*爱你&永不变心* 提交于 2019-12-21 06:24:37

问题


Here's the encryption portion of my code. It compiles fine but fails with that exception at runtime:

import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

...

byte[] salt = new byte[8];
Random rand = new Random();
rand.nextBytes(salt);

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
SecretKey key = keyFactory.generateSecret(keySpec);
PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] ciphertext = cipher.doFinal(plaintext);

Does this algorithm not come with Java 1.5? I don't mind using another algorithm, I just don't know what is available. I hope I don't have to use an external library like bouncycastle, because I've struggled for days trying to get it working to no avail (by including it in my .jar application it triggers an "Invalid signature file digest" error).


回答1:


Looks like you may have to use some other cipher if you do not wish to rely on some external library like BouncyCastle.

See the documentation about Sun crypto providers for more details about what's supported straight out of the box.



来源:https://stackoverflow.com/questions/1009996/using-java-crypto-leads-to-nosuchalgorithmexception

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