I'm trying to decrypt a 50mb file in AES-GCM Mode (cipherinputstream) with JDK 1.11.0.6 and it takes 9 min while the same in CTR Mode mode takes 10s, am i missing anything here? Encryption in both CTR and GCM Mode takes around 600ms only
I've seen earlier post, Java 9: AES-GCM performance
I've tried with JDK 1.8,1.9,1.11.0.6 and even 1.13.
Surprisingly, JDK 1.9 takes 3 mins and all others takes around 9-10mins which is definitely not acceptable on comparing with the 10s taken in CTR Mode.
Same Code with Bouncy Castle Provider decrypts in 700ms. Difference between BC implementation and Native Java Implementation is so huge?
sample code,
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();
}