本次修改:
1、各算法的调用接口修改后,测试代码(runnable任务)都需要修改
2、algorithm入参和出参
Common.java调用,入参明文是String,以及算法AlgorithmEnum.valueOf(algorithm);
3、SM2 web接口获取KeyIdSm2
- 原有:
- private byte[] plain;
- public EncTask(CyclicBarrier barrier, int loopNum, int index, String algorithm, byte[] plain, ThreadData threadData){}
- // 2.3.1版本的加密方法调用//byte[] cipherData = CipherSuiteUtils.encrypt(plain, algorithm, keyId, metadata, iv);
-
- 修改成:
- private String plain;
- public EncTask(CyclicBarrier barrier, int loopNum, int index, String algorithm, String plain, ThreadData threadData){}
- // 2.3.1版本的加密方法调用
- //byte[] cipherData = CipherSuiteUtils.encrypt(plain, algorithm, keyId, metadata, iv);
- // 2.3.4.8版本的加密方法调用
- String cipherData = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- 原有:
- private byte[] cipher;
- public DecTask(CyclicBarrier barrier, int loopNum, int index, String algorithm, byte[] cipher, ThreadData threadData){}
- // 2.3.1版本的加密方法调用
- CipherSuiteUtils.decrypt(cipher, algorithm, keyId, metadata, iv);
-
- 修改成:
- private String cipher;
- public DecTask(CyclicBarrier barrier, int loopNum, int index, String algorithm, String cipher, ThreadData threadData){}
- // 2.3.1版本的加密方法调用
- //CipherSuiteUtils.decrypt(cipher, algorithm, keyId, metadata, iv);
- //2.3.4.8版本的加密方法调用
- String decryptData = CryptoUtil.decrypt(cipher, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- public class ThreadData {
- private int id; //编号
- private String algorithm; //算法名称
- }
编号 算法 算法类型 数据大小 线程数量 循环次数 运行时间
2.3.1、变量
- //public static byte[] iv = null; //2.3.1版本
- public static String iv=null;
- public static String keyId_SM2 = null;
2.3.2、execute方法
- 修改:
- public static ThreadData execute(ThreadData data){
- //明文数据
- int length=data.getDataSize();
- log.info("数据(大小)长度:"+length);
- byte[] plainByte= new byte[length];
- for (int i=0; i
- plainByte[i] = (byte) 0x61; //length长度的a字节数组
- }
- String plainData=new String(plainByte, StandardCharsets.UTF_8);
- //CipherSuiteUtils.encrypt(plainData, "SM4_GCM", keyId, metadata, iv); //2.3.1版本
- CryptoUtil.encrypt(plainData, AlgorithmEnum.valueOf("SM4_GCM"), keyId, metadata, iv); //2.3.4.8版本
-
- switch部分全部修改,例如:
- //开始执行
- switch (algorithm) {
- case "SM4_ECB":
- case "SM4_CBC":
- case "SM4_CTR":
- case "SM4_GCM":
- case "AES_ECB":
- case "AES_CBC":
- case "AES_CTR":
- case "AES_GCM":
- if (cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask(barrier, loopNum, i, algorithm, plainData, data));
- }
- } else {
- //cipherData = CipherSuiteUtils.encrypt(plainData, "SM4_ECB", keyId, metadata, iv); //2.3.1版本
- cipherData = CryptoUtil.encrypt(plainData, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);//2.3.4.8版本
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask(barrier, loopNum, i, algorithm, cipherData, data));
- }
- }
- break;
- case "FPE_EMAIL":
- plain = "sdktest@163.com";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_EMAIL"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_ID_CARD":
- plain = "123456789123456789";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_ID_CARD"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_PHONE_NUM":
- plain = "15387936109";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_PHONE_NUM"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_OFFICER_CARD":
- plain = "军字第00111206号";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_OFFICER_CARD"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_PASSPORT":
- plain = "G12345678";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_PASSPORT"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_CREDIT_CARD":
- plain = "6212262201023557228";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_CREDIT_CARD"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_IDENTIFICATION":
- plain = "0299233902";
- data.setDataSize(plain.length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i, algorithm, plain, data));
- }
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_IDENTIFICATION"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_CHINESE":
- plain = "中文算法炼石测试";
- data.setDataSize(plain.length() * 2); //1个中午字符unicode编码等于2个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new EncTask_FPE(barrier, loopNum, i,algorithm , plain, data));
- }
-
- }else {
- //2.3.1版本
- //cipher = CipherSuiteUtils.encrypt(plain, AlgorithmEnum.valueOf("FPE_CHINESE"), keyId, metadata, iv);
- //2.3.4.8版本
- cipher = CryptoUtil.encrypt(plain, AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new DecTask_FPE(barrier, loopNum, i, algorithm, cipher, data));
- }
- }
- break;
- case "FPE_NUMBER":
- //byte[] plainData_fpe = "243.57".getBytes();
- String plainData_fpe = "243.57";
- data.setDataSize("243.57".length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new NumberEncTask(barrier, loopNum, i,algorithm , plainData_fpe, 5, 2, data));
- }
- }else {
- //2.3.1版本先加密取得密文再执行解密任务
- //byte[] cipherData_fpe = CipherSuiteUtils.fpeNumberEncrypt(plainData_fpe, "FPE_NUMBER", keyId, metadata, iv, 5, 2);
- BigDecimal cipherData_fpe = CryptoUtil.encryptNumber(new BigDecimal(plainData_fpe), AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv, 5 + 1, 2);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new NumberDecTask(barrier, loopNum, i, algorithm, cipherData_fpe, 5, 2, data));
- }
- }
- break;
- case "FPE_NUMBER_TWEAK":
- //byte[] plainData_fpe = "243.57".getBytes();
- plainData_fpe="243.57";
- data.setDataSize("243.57".length()); //1个字符等于1个字节B
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new NumberEncTask(barrier, loopNum, i, algorithm, plainData_fpe, 5, 2, data));
- }
- }else {
- //2.3.1版本先加密取得密文再执行解密任务
- //cipherData_fpe = CipherSuiteUtils.fpeNumberEncrypt(plainData_fpe, "FPE_NUMBER_TWEAK", keyId, metadata, iv, 5, 2);
- BigDecimal cipherData_fpe = CryptoUtil.encryptNumber(new BigDecimal(plainData_fpe), AlgorithmEnum.valueOf(algorithm), keyId, metadata, iv, 5 + 1, 2);
- for (int i = 0; i < threadCount; i++) {
- exs.execute(new NumberDecTask(barrier, loopNum, i, algorithm, cipherData_fpe, 5, 2, data));
- }
- }
- break;
- case "SM3":
- //SM3摘要
- for (int i = 0; i < threadCount; i++) {
- //exs.execute(new DigestTask(barrier, loopNum, i, "SM3", plainData, data)); //2.3.1版本调用
- exs.execute(new DigestTask(barrier, loopNum, i, algorithm, plainByte, data)); //2.3.4.8版本调用
- }
- break;
- case "Sm2EncDec":
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new Sm2EncSign(barrier, loopNum, i, "Sm2Enc", plainByte, data));
- }
- }else {
- byte[] cipher_sm2 = CryptoUtil.sm2Encrypt(keyId_SM2, plainByte);
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new Sm2DecVerify(barrier, loopNum, i, "Sm2Dec", plainByte, cipher_sm2, data));
- }
- }
- break;
- case "Sm2SignVerify":
- if(cryptoId == 1) {
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new Sm2EncSign(barrier, loopNum, i, "Sm2Sign", plainByte, data));
- }
- }else {
- byte[] sign = CryptoUtil.sm2Signature(keyId_SM2, plainByte);
- for (int i = 0; i < threadCount; i++) {
- //通过 ExecutorService 对象的 execute 方法将任务添加到线程去执行(获取到运行时间)
- exs.execute(new Sm2DecVerify(barrier, loopNum, i, "Sm2Verify", plainByte, sign, data));
- }
- }
- break;
- default:
- System.err.println("input crypto type error");
- break;
- }
2.3.3、readData添加算法入参
- case 1:
- threadData.setAlgorithm(buf);
- break;
2.3.4、writeCSVFileData添加算法入参
+ "," + "算法"
+ "," + data.getAlgorithm()
2.3.5、SDKTest.java
- public class SDKTest extends Common{
- //执行性能测试
- for (ThreadData data:dataList) {
- log.info("id = "+ data.getId() +", algorithm = "+data.getAlgorithm()+", cryptoId = "+ data.getCryptoId() +", dataSize = "
- + data.getDataSize() +", threadCount = "+ data.getThreadCount() + ", loopNum = " + data.getLoopNum()
- +", setTime = " + data.getSetTime());
- ThreadData outData=Common.execute(data);
- //写入result.csv
- Common.writeCSVFileData("./result.csv",outData);
- }
- }
3、web接口修改
3.1、SDKWeb增加selectSm2Key方法和addSm2Key方法
- private String sm2KeyAlias="sm2KeyTest"; //sm2KeyAlias
-
- /**
- *查询SM2公私钥对
- */
- public String selectSm2Key(){
- //创建对象
- HttpsClientUtils httpUtils = new HttpsClientUtils();
- String url = host+"/kms/v1/jmj/sm2/list";
- String jsonStr = "{\"page\":1,\"limit\":10}";
- //请求头
- Map
headers=new HashMap<>(); - headers.put("Content-Type","application/json;charset=UTF-8");
- headers.put("Authorization","Bearer "+token);
-
- HttpClientResult httpResult=httpUtils.doPost(url,headers,jsonStr);
- String resultContent=httpResult.getContent();
- String sm2KeyId=JsonUtil.getTargetValue(resultContent,sm2KeyAlias,"description","keyPairId");
- if(sm2KeyId!=null){
- log.info("selectSm2Key方法,sm2KeyId:"+sm2KeyId);
- }
-
- return sm2KeyId;
- }
-
- /**
- *新增Sm2公私钥对
- */
- public String addSm2Key(){
- //创建对象
- HttpsClientUtils httpUtils = new HttpsClientUtils();
- String url = host+"/kms/v1/jmj/sm2/gen-key-pair";
- String jsonStr = "{\"description\":\""+sm2KeyAlias+"\"}";
-
- //请求头
- Map
headers=new HashMap<>(); - headers.put("Content-Type","application/json;charset=UTF-8");
- headers.put("Authorization","Bearer "+token);
-
- HttpClientResult httpResult=httpUtils.doPost(url,headers,jsonStr);
- String resultContent=httpResult.getContent();
- String sm2KeyId=JsonPath.read(resultContent, "$.data");
- if(sm2KeyId!=null){
- log.info("addSm2Key方法,sm2KeyId:"+sm2KeyId);
- }
- return sm2KeyId;
- }
3.2、SDKTest.java增加获取keyId_SM2
- //从管理平台获取数据密钥
- List
casbKeyId = getCasbKeyId(); - log.info("casbKeyId:" + casbKeyId);
- keyId = casbKeyId.get(0).toString();
- // SM2 密钥ID:请从密管平台获取
- keyId_SM2 = casbKeyId.get(1).toString();
-
- /**
- *CASB管理平台,查询keyId
- * @return List
- */
- public static List
getCasbKeyId() { - Login lo = new Login();
- String token = lo.getToken();
- String host = Common.readProperty("./config.properties", "HOST");
- SDKWeb sdkWeb = new SDKWeb(host, token);
-
- List
casbKeyId=new ArrayList<>(); - //查询数据密钥(如没有,则新增)
- String moduleKeyId = sdkWeb.selectModuleKey();
- if (moduleKeyId == "") {
- moduleKeyId = sdkWeb.addModuleKey();
- }
- casbKeyId.add(moduleKeyId);
- //查询SM2密钥(如没有,则新增)
- String sm2KeyId=sdkWeb.selectSm2Key();
- if(sm2KeyId==null){
- log.info("查询接口,查询不到sm2KeyId,进行新增操作");
- sdkWeb.addSm2Key();
- }
- casbKeyId.add(sm2KeyId);
- return casbKeyId;
- }
4、执行
4.1、sdkPerfTest-1.1.0_2.3.4.8_algorithm.jar执行
$source /etc/profile
$java -Djava.ext.dirs=/opt/casb/lib/:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/ext -jar sdkPerfTest-1.1.0_2.3.4.8_algorithm.jar
4.2、sdkPerfTest-1.0.8_2.3.4.8_algorithm.jar执行
$source /etc/profile
#Linux如果用脚本安装,那么默认安装目录为:/opt/casb/lib/
$java -Djava.ext.dirs=/opt/casb/lib/:$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/ext -jar sdkPerfTest-1.0.8_2.3.4.8_algorithm.jar