1

我尝试在AES算法中分别确定加密和解密的经过时间我的代码是否低于真实方法如果没有请告诉我在哪里找到它?

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);
    }

当在我的笔记本电脑上运行这个程序给我以下结果 在 Java 中创建 1000 个对象的总执行时间(以毫秒为单位):1416 在 Java 中创建 1000 个对象的总执行时间(以毫秒为单位):1600

4

1 回答 1

1

不,这是不对的。例如,您fis只打开一次流,因此您实际上只在加密循环中处理一次。

你在解密循环中犯了同样的错误。

于 2013-10-17T05:34:26.647 回答