我会尝试使用RC6算法,但我有一个错误:
RC6 KeyGenerator不可用
如何获得rc6的密钥生成器?
线程"main“中的异常: javax.crypto.KeyGenerator.(KeyGenerator.java:169) at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223) at RC6.encrypt(RC6.java:27)在RC6.main上不可用的RC6 KeyGenerator (RC6.java:16)
import javax.crypto.spec.*;
import java.security.*;
import javax.crypto.*;
public class Main
{
private static String algorithm = "RC6";
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(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// create an instance of cipher
Cipher cipher = Cipher.getInstance(algorithm);
// 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(algorithm);
kg.init(sr);
SecretKey sk = kg.generateKey();
// do the decryption with that key
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, sk);
byte[] decrypted = cipher.doFinal(toDecrypt);
return new String(decrypted);
}
}发布于 2017-10-21 20:54:16
RC6不是由Oracle安全提供者之一提供的算法。提供程序提供了Cipher和KeyGenerator后面的算法实现。
在类路径中添加了.jar之后,应该可以这样做:
static {
Security.addProvider(new BouncyCastleProvider());
}您还可能需要在JRE文件夹中安装无限加密文件。
https://stackoverflow.com/questions/46867344
复制相似问题