在Java中进行第一次迭代后,循环中的加密和解密可提供更快的结果

时间:2019-03-21 13:06:49

标签: java encryption rsa

我正在测试一种情况,其中使用RSA使用不同的公共密钥对32字节的数据进行多次加密。这是我的代码:

    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

我假设是因为它运行相同,所以所有迭代都应给出相同的持续时间,但事实证明我错了。代码有什么问题吗?

0 个答案:

没有答案