Java 密钥生成器异常
我试图实现IDEA算法。以下是我在谷歌上找到的代码:Java 密钥生成器异常,java,key-generator,Java,Key Generator,我试图实现IDEA算法。以下是我在谷歌上找到的代码: package ideaalgoritam; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class IDEAalgoritam { public static void main(String []args
package ideaalgoritam;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class IDEAalgoritam {
public static void main(String []args) throws Exception {
String toEncrypt = "The shorter you live, the longer you're dead!";
System.out.println("Encrypting...");
byte[] encrypted = encrypt(toEncrypt, "password");
System.out.println("Decrypting...");
String decrypted = decrypt(encrypted, "password");
System.out.println("Decrypted text: " + decrypted);
}
public static byte[] encrypt(String toEncrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance("IDEA");
kg.init(sr);
SecretKey sk = kg.generateKey();
// create an instance of cipher
Cipher cipher = Cipher.getInstance("IDEA");
// initialize the cipher with the key
cipher.init(Cipher.ENCRYPT_MODE, sk);
// enctypt!
byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());
return encrypted;
}
public static String decrypt(byte[] toDecrypt, String key) throws Exception {
// create a binary key from the argument key (seed)
SecureRandom sr = new SecureRandom(key.getBytes());
KeyGenerator kg = KeyGenerator.getInstance("IDEA");
kg.init(sr);
SecretKey sk = kg.generateKey();
// do the decryption with that key
Cipher cipher = Cipher.getInstance("IDEA");
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] decrypted = cipher.doFinal(toDecrypt);
return new String(decrypted);
}
}
但当我运行代码时,我得到了以下异常:
Exception in thread "main" java.security.NoSuchAlgorithmException: IDEA KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:169)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
at ideaalgoritam.IDEAalgoritam.encrypt(IDEAalgoritam.java:27)
at ideaalgoritam.IDEAalgoritam.main(IDEAalgoritam.java:16)
线程“main”java.security.NoSuchAlgorithmException中的异常:IDEA密钥生成器不可用
位于javax.crypto.KeyGenerator(KeyGenerator.java:169)
位于javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
位于ideaalgoritam.ideaalgoritam.encrypt(ideaalgoritam.java:27)
位于ideaalgoritam.ideaalgoritam.main(ideaalgoritam.java:16)
在jdk和jre安全库文件夹中,我添加了安全提供者bouncy castle。但我仍然得到这个例外。
有人知道我如何解决这个问题,以便运行此代码吗 您是否在
java.security
文件中添加了一行以注册提供程序?另外,使用新生成的密钥进行解密将永远无法工作。顺便说一句,自2013年1.50版专利到期后,IDEA就不再需要bcprov ext了,该专利被转移到bcprov。而且ECB在大多数应用中都不是很安全。