AES-GCM Скорость расшифровки JAVA - PullRequest
1 голос
/ 22 января 2020

Я пытаюсь расшифровать 50-мегабайтный файл в режиме AES-GCM (cipherinputstream) с JDK 1.11.0.6, и это занимает 9 минут, а то же самое в режиме CTR - 10 секунд. Я что-то здесь упустил? Шифрование как в режиме CTR, так и в режиме GCM занимает около 600 мс.

Я видел предыдущее сообщение, Java 9: производительность AES-GCM

Я пробовал с JDK 1.8,1.9,1.11.0.6 и даже 1.13.

Удивительно, но JDK 1.9 занимает 3 минуты, а все остальные - около 9-10 минут, что явно недопустимо по сравнению с 10 секундами в режиме CTR.

Тот же код с Bouncy Castle Provider расшифровывается за 700 мс. Разница между реализацией B C и 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();
}
...