简体   繁体   English

如何确定Java中使用CipherOutputStream进行加密和解密所花费的时间?

[英]How determine elapsed time for encrypt and decrypt with CipherOutputStream in java?

I try determined elapsed time for encrypt and decrypt separately in AES algorithm Is my code below true method if no please tell me where is find that? 我尝试确定在AES算法中分别进行加密和解密所需的时间,如果没有,我的代码是否在true方法下面?请告诉我在哪里找到?

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import de.flexiprovider.core.FlexiCoreProvider;

public class ExampleCrypt123 {

    public static void main(String[] args) throws Exception {
        Security.addProvider(new FlexiCoreProvider());
        Cipher cipher = Cipher.getInstance("AES128_CBC", "FlexiCore");

        KeyGenerator keyGen = KeyGenerator.getInstance("AES", "FlexiCore");
        SecretKey secKey = keyGen.generateKey();

        String cleartextFile = "F:/java_projects/cleartext.txt";
        String ciphertextFile = "F:/java_projects/ciphertextSymm.txt";
        String cleartextAgainFile = "F:/java_projects/cleartextAgainSymm.txt";

        byte[] block = new byte[16];

        FileInputStream fis = new FileInputStream(cleartextFile);
        FileOutputStream fos = new FileOutputStream(ciphertextFile);

        cipher.init(Cipher.ENCRYPT_MODE, secKey);
        // Encrypt
        long startTime = System.nanoTime();

        for(int j=0; j< 1000000; j++){
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            int i;
            while ((i = fis.read(block)) != -1) {
                cos.write(block, 0, i);
            }
            cos.close();
        }
        long elapsedTime = System.nanoTime() - startTime;

        System.out.println("Total execution time to create 1000 objects in Java in millis: "
                + elapsedTime/1000000);

        // Decrypt



        fis = new FileInputStream(ciphertextFile);

        fos = new FileOutputStream(cleartextAgainFile);

        cipher.init(Cipher.DECRYPT_MODE, secKey);

        long startTime2 = System.nanoTime();
        for(int j=0; j< 1000000; j++){
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int i;

            while (( i = cis.read(block)) != -1) {
                fos.write(block, 0, i);
            }
            fos.close();
        }
        long elapsedTime2 = System.nanoTime() - startTime2;

        System.out.println("Total execution time to create 1000  objects in Java in millis: "
                + elapsedTime2/1000000);
    }

When run this program on my laptop give me the following results Total execution time to create 1000 objects in Java in millis: 1416 Total execution time to create 1000 objects in Java in millis: 1600 在笔记本电脑上运行此程序时,请给我以下结果:以毫秒为单位在Java中创建1000个对象的总执行时间:1416以毫秒为单位在Java中创建1000个对象的总执行时间:1600

No, that's not right. 不,那是不对的。 For example you open the fis stream only once, so you actually process it only once in your encryption loop. 例如,您只打开一次fis流,因此实际上在加密循环中只处理一次。

And you make the same mistake in your decryption loop. 而且您在解密循环中也会犯同样的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM