-2

我正在尝试使用 TWOFISH 进行加密和解密。我收到错误:线程“main”中的异常 java.security.NoSuchAlgorithmException:twofish KeyGenerator 不可用

我的代码:

public class TWOFISH {

    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("twofish");
      kg.init(sr);
      SecretKey sk = kg.generateKey();

      // create an instance of cipher
      Cipher cipher = Cipher.getInstance("twofish");

      // 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("twofish");
      kg.init(sr);
      SecretKey sk = kg.generateKey();

      // do the decryption with that key
      Cipher cipher = Cipher.getInstance("twofish");
      cipher.init(Cipher.DECRYPT_MODE, sk);
      byte[] decrypted = cipher.doFinal(toDecrypt);

      return new String(decrypted);
   }
}
4

1 回答 1

2
KeyGenerator kg = KeyGenerator.getInstance(String algorithm);

根据

https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html#KeyGenerator

参数的接受Stringalgorithm不包括"twofish",因此例外。

于 2017-11-24T15:17:08.307 回答