我正在尝试使用JDK 1.11.0.6在AES-GCM模式(cipherinputstream)中解密50mb文件,它需要9分钟,而在CTR模式下则需要10s,我在这里错过了什么吗? CTR和GCM模式下的加密仅需600毫秒左右
我看过较早的帖子, Java 9: AES-GCM performance
我尝试使用JDK 1.8、1.9、1.11.0.6甚至是1.13。
令人惊讶的是,JDK 1.9耗时3分钟,而其他所有耗时约9-10分钟,与CTR模式下的10s相比绝对不可接受。
与Bouncy Castle Provider相同的代码将在700毫秒内解密。 BC实现与Native Java实现之间的差异如此之大?
示例代码
public static void encryptfile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException {
InputStream fin = new FileInputStream("test.txt");
OutputStream fout = new FileOutputStream("enc_test.txt");
SecretKeySpec serverKey = new SecretKeySpec(HexConverterUtil.BASE16_DECODE("a4e97a4713841586ca639a416c636e3ef2a404efaf58b0a7768cd5758b1297a0"), "AES");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(16 * 8, HexConverterUtil.BASE16_DECODE("258f7d4c1c72bee0109bcbe5"));
Cipher encryptCipher = Cipher.getInstance("AES/GCM/NoPadding");
encryptCipher.init(Cipher.ENCRYPT_MODE, serverKey, gcmParameterSpec);
fout = new CipherOutputStream(fout, encryptCipher);
byte[] bytesBuffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fin.read(bytesBuffer)) != -1) {
fout.write(bytesBuffer, 0, bytesRead);
}
fin.close();
fout.close();
}
public static void decryptfile() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IOException {
InputStream fin = new FileInputStream("enc_test.txt");
SecretKeySpec serverKey = new SecretKeySpec(HexConverterUtil.BASE16_DECODE("a4e97a4713841586ca639a416c636e3ef2a404efaf58b0a7768cd5758b1297a0"), "AES");
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(16 * 8, HexConverterUtil.BASE16_DECODE("258f7d4c1c72bee0109bcbe5"));
Cipher decryptCipher = Cipher.getInstance("AES/GCM/NoPadding");
decryptCipher.init(Cipher.DECRYPT_MODE, serverKey, gcmParameterSpec);
fin = new CipherInputStream(fin, decryptCipher);
OutputStream fout = new BufferedOutputStream(new FileOutputStream("dec_test.mp3"));
byte[] bytesBuffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fin.read(bytesBuffer)) != -1) {
fout.write(bytesBuffer, 0, bytesRead);
}
fin.close();
fout.close();
}