我可以使用标准Java Cipher API使用BouncyCastle的Tweakable Block Ciphers吗?

BouncyCastle提供了Threefish的实现,可以将调整作为参数:

ThreeFishEngine engine = new ThreeFishEngine(256); engine.init(true, new TweakableBlockCipherParams(...)); 

但是, TweakableBlockCipherParams与Java默认Cipher实例使用的AlgorithmParameter类型不兼容。

有没有办法通过调整来初始化这个密码?

 Cipher cipher = Cipher.getInstance("Threefish-256/CBC/NoPadding"); cipher.init(???); 

如果您不想在加密期间使用tweak参数,则只能通过Java的加密API使用Bouncy Castle的Threefish算法。 通过Java的API,你只能引入一个和一个初始化向量参数,但这不会被用作一个tweak参数(我在代码示例之后解释了为什么,见下文)。

此外,为了使下面的示例工作,您必须使用Java Cryptography Extension(JCE)Unlimited Strength Jurisdiction Policy Files更新您的JRE / JDK,您可以从此处下载。 Java 7和8有不同的版本。

如果您不想使用tweak参数,可以通过标准加密API使用Threefish算法。

 static final BouncyCastleProvider PROVIDER = new BouncyCastleProvider(); public static void main(String[] args) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("Threefish-1024", PROVIDER); kg.init(1024); SecretKey key = kg.generateKey(); byte[] plaintext = "Hi! I'm cat!".getBytes(); byte[] ciphertext = encrypt(key, plaintext); System.out.println(new String(decrypt(key, ciphertext))); // prints "Hi! I'm cat!" } static byte[] encrypt(SecretKey key, byte[] plaintext) throws Exception { return encryptOrDecrypt(true, key, plaintext); } static byte[] decrypt(SecretKey key, byte[] ciphertext) throws Exception { return encryptOrDecrypt(false, key, ciphertext); } static byte[] encryptOrDecrypt(boolean encrypt, SecretKey key, byte[] bytes) throws Exception { Cipher cipher = Cipher.getInstance("Threefish-1024/CBC/PKCS5Padding", PROVIDER); // note that we are creating a dummy iv parameter, in this case it // should be 128 bytes long, because if it's not an exception is raised cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key, new IvParameterSpec(new byte[128])); return cipher.doFinal(bytes); } 

我从这里下载了带有调试符号的Bouncy Castle JAR并调试了上面的代码。 对Threefish.init的调用登陆Threefish.init ,变量params将是KeyParameter一个实例,而不是我们案例中的TweakableBlockCipherParameters 。 因此, tweakBytes将为null,并且在加密期间不会使用。

知道这一点, 现在不可能使用Java API将tweak参数提供给底层的Threefish密码引擎。

链接到另一个非常相似的问题