SM4、AES、DES加解密算法性能比较

本文通过代码测试对比了SM4、AES和DES三种加解密算法在处理不同大小文件时的性能。结果显示,AES在加解密速度上具有显著优势,而SM4和DES的性能相对较慢,尤其在处理大型文件时,这种差距更为明显。
摘要由CSDN通过智能技术生成

SM4、AES、DES加解密算法性能比较

测试代码

  public void encryDecry() throws  Exception{
        SM4StringEncryptor sm4 = new SM4StringEncryptor();
        String path = "D:\\Users\\xlj\\Downloads\\sylog.txt";

        File file = new File(path);
        InputStreamReader reader = new InputStreamReader(new FileInputStream(path));

        BufferedReader br = new BufferedReader(reader);
        StringBuilder fileContent = new StringBuilder();

        String line = "";
        line = br.readLine();
        while (line != null) {
            line = br.readLine();
            fileContent.append(line);
        }
        System.out.println("文件原始大小:" + fileContent.length() /1024/1024 + "Mb");

        StringBuilder fileContent = new StringBuilder("xlj12442");
        long startTime = System.currentTimeMillis();

        String encryContent=  sm4.encrypt(fileContent.toString());
        System.out.println("sm4加密后:" + encryContent);
        System.out.println("sm4加密后文件大小:" + encryContent.length() /1024/1024 + "Mb");

        long endTime = System.currentTimeMillis();
        System.out.println("sm4加密耗时:" + (endTime - startTime) + "ms");

        String decryContent = sm4.decrypt(encryContent);
        System.out.println("sm4解密后:" + decryContent);
        System.out.println("sm4解密后文件大小:" + decryContent.length()  /1024/1024 + "Mb");
        long endTime1 = System.currentTimeMillis();
        System.out.println("sm4解密耗时:" + (endTime1 - endTime) + "ms");

        // aes
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);

        long aesStartTime = System.currentTimeMillis();
        encryContent = aes.encryptHex(decryContent);
        long esaEndTimeEncry = System.currentTimeMillis();

        System.out.println("aes加密后:" + encryContent);
        System.out.println("aes加密后文件大小:" + encryContent.length() /1024/1024 + "Mb");
        System.out.println("aes加密耗时:" + (esaEndTimeEncry - aesStartTime) + "ms");


        long aesEncryStartTime = System.currentTimeMillis();
        decryContent = aes.decryptStr(encryContent, CharsetUtil.CHARSET_UTF_8);
        long aesEncryEndTime = System.currentTimeMillis();
        System.out.println("aes解密后:" + decryContent);
        System.out.println("aes解密后文件大小:" + decryContent.length()  /1024/1024 + "Mb");
        System.out.println("aes解密耗时:" + (aesEncryEndTime - aesEncryStartTime) + "ms");


        // des
        key = SecureUtil.generateKey(SymmetricAlgorithm.DESede.getValue()).getEncoded();
        SymmetricCrypto des = new SymmetricCrypto(SymmetricAlgorithm.DESede, key);


        long desStartTime = System.currentTimeMillis();
        encryContent = des.encryptHex(decryContent);
        long desEndTimeEncry = System.currentTimeMillis();

        System.out.println("des ede加密后:" + encryContent);
        System.out.println("des ede加密后文件大小:" + encryContent.length() /1024/1024 + "Mb");
        System.out.println("des ede加密耗时:" + (desEndTimeEncry - desStartTime) + "ms");


        long desEncryStartTime = System.currentTimeMillis();
        decryContent = des.decryptStr(encryContent, CharsetUtil.CHARSET_UTF_8);
        long desEncryEndTime = System.currentTimeMillis();
        System.out.println("des ede解密后:" + decryContent);
        System.out.println("des ede解密后文件大小:" + decryContent.length()  /1024/1024 + "Mb");
        System.out.println("des ede解密耗时:" + (desEncryEndTime - desEncryStartTime) + "ms");*/

    }

性能比较结果

加密算法文件大小加密耗时(ms)解密耗时(ms)
SM45M1596841
AES5M201447
DES5M317669
SM4620KB689172
AES620KB109186
DES620KB53132
SM436KB48734
AES36KB2057
DES36KB914
SM46Bytes5050
AES6Bytes1324
DES6Bytes10

5M文件加密耗时

5M文件解密耗时

620KB加密耗时

620KB解密耗时

36KB加密耗时

36KB解密耗时

6Bytes加密耗时

6Bytes解密耗时

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值