Шифрование и дешифрование в цикле дает более быстрый результат после первой итерации в Java - PullRequest
0 голосов
/ 21 марта 2019

Я тестирую случай, когда 32-байтовые данные шифруются несколько раз с различными открытыми ключами с использованием RSA.Вот мой код:

    List<Long> colDurSetup = new ArrayList();
    List<Long> colDurEnc = new ArrayList();
    List<Long> colDurDec = new ArrayList();
    List<PublicKey> pubKeys = new ArrayList();
    List<PrivateKey> privKeys = new ArrayList();

    for(int i=0; i<10;i++) {
        long timeStart = System.currentTimeMillis();
        KeyPair kp = buildKeyPair();
        long timeEnd = System.currentTimeMillis();
        long dur = timeEnd-timeStart;
        colDurSetup.add(dur);
        pubKeys.add(kp.getPublic());
        System.out.println(kp.getPublic().getEncoded().length);
        privKeys.add(kp.getPrivate());
        System.out.println(kp.getPrivate().getEncoded().length);
    }

    SecureRandom r = new SecureRandom();
    byte[] data = new byte[32];
    r.nextBytes(data);
    System.err.println(Arrays.toString(data));

    System.out.println("data length "+data.length);

    for(int i=0;i<10;i++) {
        long timeStart = System.nanoTime();
        byte[] ciphertext = encrypt(pubKeys.get(i), data);
        long timeEnd = System.nanoTime();
        long dur = timeEnd-timeStart;
        System.out.println("enc duration "+dur);
        colDurEnc.add(dur);
        System.out.println(timeStart);
        System.out.println(timeEnd);

        System.out.println("ciphertext length "+ciphertext.length);
        System.err.println("cip "+Arrays.toString(ciphertext));

        long timeStart2 = System.nanoTime();
        byte[] decrypted = decrypt(privKeys.get(i), ciphertext);
        long timeEnd2 = System.nanoTime();

        long dur2 = timeEnd2-timeStart2;
        colDurDec.add(dur2);
        System.err.println("dec "+Arrays.toString(decrypted));
        System.out.println("dec duration "+dur2);
    }

public static KeyPair buildKeyPair() throws NoSuchAlgorithmException {
    final int keySize = 2048;
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    keyPairGenerator.initialize(keySize, random);      
    return keyPairGenerator.genKeyPair();
}

public static byte[] encrypt(PublicKey publicKey, byte[] message) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");  
    cipher.init(Cipher.ENCRYPT_MODE, publicKey, random);  
    return cipher.doFinal(message);  
}

public static byte[] decrypt(PrivateKey privateKey, byte [] encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");  
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    return cipher.doFinal(encrypted);
}

Когда я запустил его, я заметил, что первая итерация требует больше времени для шифрования и дешифрования, чем остальные итерации, и я не уверен, почему.Вот результат:

enc duration 231
dec duration 4
enc duration 0
dec duration 4
enc duration 0
dec duration 4
enc duration 0
dec duration 4
enc duration 0
dec duration 4
enc duration 1
dec duration 3
enc duration 0
dec duration 4
enc duration 0
dec duration 4
enc duration 0
dec duration 4
enc duration 0
dec duration 4

Я полагаю, потому что он выполняется одинаково, тогда все итерации должны давать одинаковую продолжительность, но оказывается, что я ошибся.Что-то не так в коде?

...