TA的每日心情 无聊 昨天 09:47
签到天数: 528 天
连续签到: 1 天
[LV.9]测试副司令
1 测试积点
在 jmeter 中使用 beanshell 实现签名算法,如下图,在使用过程中发现,直接在 BeanShell PreProcessor 实现加密算法在执行过程中耗时较久,达到 200ms+,影响性能测试
之后将签名算法打包成 jar 包,发现性能提升许多
有哪位大佬可以告诉我这中间的区别吗?以及原因吗?
具体代码
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DecimalFormat;
public class App {
public static byte[] TripleDesEncrypt(byte[] content, byte[] key) throws Exception {
byte[] icv = new byte[8];
System.arraycopy(key, 0, icv, 0, 8);
return TripleDesEncrypt(content, key, icv);
}
protected static byte[] TripleDesEncrypt(byte[] content, byte[] key, byte[] icv) throws Exception {
final SecretKey secretKey = new SecretKeySpec(key, "DESede");
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
final IvParameterSpec iv = new IvParameterSpec(icv);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return cipher.doFinal(content);
}
public static byte[] TripleDesDecrypt(byte[] content, byte[] key) throws Exception {
byte[] icv = new byte[8];
System.arraycopy(key, 0, icv, 0, 8);
return TripleDesDecrypt(content, key, icv);
}
protected static byte[] TripleDesDecrypt(byte[] content, byte[] key, byte[] icv) throws Exception {
final SecretKey secretKey = new SecretKeySpec(key, "DESede");
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
final IvParameterSpec iv = new IvParameterSpec(icv);
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
return cipher.doFinal(content);
}
public static String sign(String content, String privateKeyPem,long time) {
try {
long startTime = System.currentTimeMillis();
String privateKeyPem_String = privateKeyPem.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "");
byte[] encodedKey = org.bouncycastle.util.encoders.Base64.decode(privateKeyPem_String.getBytes("UTF-8"));
long spendtime2 = System.currentTimeMillis() - startTime;
log.info("=====sign Base64 :"+ spendtime2);
long startTime3 = System.currentTimeMillis();
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
Signature signature = Signature.getInstance("SHA256WithRSA");
long spendtime4 = System.currentTimeMillis() - startTime3;
log.info("=====sign signature :"+ spendtime4);
long startTime1 = System.currentTimeMillis();
signature.initSign(privateKey);
long spendtime3 = System.currentTimeMillis() - startTime1;
log.info("=====sign initSign :"+ spendtime3);
signature.update(content.getBytes("utf-8"));
byte[] signed = signature.sign();
long spendtime5 = System.currentTimeMillis() - startTime1;
log.info("=====sign update :"+ spendtime5);
return new String(org.bouncycastle.util.encoders.Base64.encode(signed));
} catch (Exception var6) {
String errorMessage = "签名遭遇异常,content=" + content + " privateKeySize=" + privateKeyPem.length() + " reason=" + var6.getMessage();
// log.error("====error: "+errorMessage);
throw new RuntimeException(errorMessage, var6);
}
}
}
// ==============begin================
String timestamp = String.valueOf(System.currentTimeMillis());
vars.put("timestamp",timestamp);
long startTime = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Random rand = new Random();
Integer randomNum = rand.nextInt(5000000) + 1000000;
String randomNumStr = String.valueOf(randomNum);
String order_id = sdf.format(new Date()) + randomNumStr;
vars.put("order_id",order_id);
double a=Math.random()*100 + 1.00;
DecimalFormat df = new DecimalFormat( "0.00" );
String pay_str=df.format(a);
Random rand = new Random();
Integer randomNum = rand.nextInt(5000000) + 1000000;
String randomNumStr = String.valueOf(randomNum);
String id_card = "430" + randomNumStr + "10104219";
String private_key = vars.get("key");
String dealer_id = "00243810";
String data = "{\"order_id\":\""+order_id+"\",\"dealer_id\":\"\",\"broker_id\":\"\",\"real_name\":\"\",\"card_no\":\"\",\"phone_no\":\"\",\"id_card\":\"\", \"pay\":\""+pay_str+"\"}";
byte[] des3key = "0857b75Fmev7mbTFuhUNa6Rt".getBytes("utf-8");
byte[] enc = App.TripleDesEncrypt(data.getBytes("utf-8"), des3key);
byte[] enc64 = Base64.encodeBase64(enc);
String data64 = new String(enc64);
vars.put("data",data64);
// 进行sign
String sign_data = "data=" + data64 + "&mess=test×tamp="+ timestamp + "&key=0aYxoR0s474JZns9kS09oXnA2e7Rz0fj";
String sign = App.sign(sign_data, private_key, startTime);
vars.put("sign",sign);
复制代码
我来回答