SHA-256、AES-256计算时间开销

  • Post author:
  • Post category:其他


由于研究需要,我们计算了SHA-256和AES-256的计算时间开销,下面将代码贴在下方。

需要注意的是,我们使用

System.nanoTime()

方法,获取的时间戳的单位是纳秒。此外,我们循环计算了1000次求其平均值,由于第一次的时间开销明显过大,我们将其抛弃。

SHA-256

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;

public class SHA256 {
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    public static long sha256(byte[] bytes) throws Exception {
        long t1 = System.nanoTime();

        MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
        messageDigest.update(bytes);
        String encodestr = byte2Hex(messageDigest.digest());//结果以16进制的字符串输出,256/8=32位

        long t2 = System.nanoTime();

        System.out.println("It changes into byte is : " + Arrays.toString(bytes));
        System.out.println("The length of bytes is " + 8 * bytes.length + " bits");
        System.out.println("The SHA-256 result is " + encodestr);
        System.out.println(t1);
        System.out.println(t2);
        System.out.println("The time it cost is " + (t2 - t1) + " ns");
        System.out.println("-------------------------------------------------------");
        return t2 - t1;
    }

    public static void main(String[] args) throws Exception {
        //使用String类型,便于打印
        String string = "7214083869398510205830034914356820453685029199381676443177786398";

        //转换为byte类型,byte长度表示其具有多少字节长度
        byte[] bytes = string.getBytes(StandardCharsets.UTF_8);

        System.out.println("The string is : " + string);
        System.out.println("The length of string is " + 8 * string.length() + " bits");

        System.out.println("-------------------------------------------------------");

        int num = 1000;
        long time = 0;
        for (int i = 0; i < num + 1; i++) {
            long t = sha256(bytes);
            if (i == 0) continue;
            time = time + t;
        }
        System.out.println("time is " + time / num +" ns");

    }
}

AES-256

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AES256Enc {
    public static long aes256(byte[] bytesKey,byte[] bytesMessage) throws Exception
    {
        long t1 = System.nanoTime();

        Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(bytesKey,"AES"));
        byte[] encrypted = cipher.doFinal(bytesMessage);

        long t2 = System.nanoTime();

        System.out.println("The encrypt result is " + new String(Base64.getEncoder().encode(encrypted)));
        System.out.println("The encrypt time it cost is " + (t2 - t1) + " ns");
        System.out.println("-----------------------------------------------");

        return t2-t1;
    }
    public static void main(String[] args) throws Exception {
        //密钥
        String key = "39759093433089516788876228354479";
        byte[] bytesKey = key.getBytes(StandardCharsets.UTF_8);
        System.out.println("The key is : " + key);
        System.out.println("The length of key is " + 8 * bytesKey.length + " bits");

        //明文
        String message = "3mx9c08c9nov2x8che13qg0vfgmbcj2bscwoenst69tka0ezg1dpwzmwnibjti7xbc5obj56egk2xcnnmf2kkwnk46wlo0qb98pdgpk41yu6qpf7dlwxr8ojrodqlfx1s79fnzmdkf5dsvmftbg1zahwy0tq7lxfi8dd6wgp4da2ci39447ub5l0djcx1p60xdm62becln6p5ssng5qo4xyhyacxfewwraskdt9ckby1jnfkcc8f0spo3tjnu5w2rytcfww5gmarzmpk8sdaqzbm9hlg2lgs5c2ylbz1ssbeke1w1bs4b1iwcynyh7fkwo9wg6yndn7au2pjz0o2yy93s95irr3nrzn3ie87o4l5ivwflyzuk94s9jf0klwbjew5vka2q8j3g8j9teucjoild2npqq09lynx0a6xkzxrhk96539iup81mg64gys2p4yq73ns7zgiuy81hjxdnqr8di2mfp73by9v3gtqqvygfet3vsyzy3658d8j7k7udk77sk8lp4rsaxpmz9qdlyrvb7w6wisvl3eqyai2upcg0ii13lutsl8cy9qu7y3r29p1xtsk7oxcm366uhr47vk4g3qeilxyydcje3z1l7gis33nz6tp9j8bvdtx2qls7olat7h2ffdl2a0zv4flj8ozvw3o4kabynas5xu6cytdb38jw3racweqaz6ig3923b1ky4pd5y7gex4fy2h0c90mnza2am4lcw3aw5ogpepk0lbkrb1lb9wxtetwofgznsawoktt79pen5jjk31k13s9ue5czz9dof747q9y7c82b22ti9ylb92jz8ysov0dicuypvatmsljy51t34b39rfihqj5nyg1nktnexwqufy9uweg79awkqx0pjr42t9pjq11b7skvqglomqy7eqousq71j3y3e62mpsmf1kbdmq1liiwc0hald06cesqoskf4x7gdape1q1ryn11ivn15y8tvr2mxw5imfqnlxprmmywx55cjfetdwxjtvzd79cf05lkf9ysagkbrq33r34e924yr8r2ul2eagoqn7judw2bvkrz6rwrhmznkc1x2v201uf67aek33ppy0dz82x6f7k8vfpvregf2gue3jckea7u9gef78351eqc66v7mwetnrda4o4w136w6sduba9iihug65ez14nuushwivph77x95cxdb92acno9ow23gr3qly8c2tlr1ny71qbf8p567towybqvng9xfqb5ijt26maztyhjwp2q4imuxnaysxlc16oe1iv29syg";
        System.out.println("The message is : " + message + "\n");
        byte[] bytesMessage = message.getBytes(StandardCharsets.UTF_8);

        int num = 1000;
        long time = 0;
        for (int i = 0; i < num + 1; i++) {
            long t = aes256(bytesKey,bytesMessage);
            if (i == 0) continue;
            time = time + t;
        }
        System.out.println("time is " + time / num +" ns");
    }
}

我们只关注其结果

SHA-256的计算开销是

在这里插入图片描述

AES-256的计算开销是

在这里插入图片描述



版权声明:本文为qq_42450533原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。