我会尝试使用RC6算法,但出现错误:

RC6 KeyGenerator不可用

如何获得rc6的密钥生成器?

线程“主”中的异常java.security.NoSuchAlgorithmException:RC6 KeyGenerator不可用
    在javax.crypto.KeyGenerator。(KeyGenerator.java:169)
    在javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223)
    在RC6.encrypt(RC6.java:27)
    在RC6.main(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);
   }
}

最佳答案

RC6不是Oracle安全提供程序之一提供的算法。提供程序提供Cipher甚至KeyGenerator后面的算法实现。

在类路径中添加Bouncy Castle提供程序.jar后,这应该可以工作:

static {
    Security.addProvider(new BouncyCastleProvider());
}




您可能还需要在JRE文件夹中安装无限的加密文件。

10-07 13:49