先搞清楚加密规则,编写加密代码,导出jar,beanshell中使用jar中的加密方法得到加密后的参数,模拟请求
编写代码
参数放入TreeMap方法,TreeMap会自动对参数做排序,json的String可以直接转TreeMap
public class TestAesUtil {
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONObject object = JSON.parseObject("{\"d\":\"x\",\"a\":\"f\"}");
// TreeMap<String ,Object> billMap = JSONObject.parseObject(object.toJSONString(), new TypeReference<TreeMap<String, Object>>(){});
TreeMap billMap = getMap("{\"version\":\"1.0\",\"appId\":\"\",\"reqDateTime\":\"2019-3-28 10:4:47\",\"bizContent\":{\"productId\":\"10000041871717\"}}");
System.out.println("billMap"+billMap);
}
//字符串转TreeMap TreeMap可以对参数进行排序
public static TreeMap getMap(String sSrc) {
JSONObject object = JSON.parseObject(sSrc);
TreeMap<String ,Object> billMap = JSONObject.parseObject(object.toJSONString(), new TypeReference<TreeMap<String, Object>>(){});
return billMap;
}
SHA1加密方法
public class TestSHA1 {
public static String getCode(TreeMap<String, Object> treeMap ) throws Exception{
StringBuffer keyValue = new StringBuffer();
for (String key : treeMap.keySet()) {
// System.out.println("key值:" + key + " value值:" + treeMap.get(key));
keyValue.append(key);
keyValue.append(treeMap.get(key));
}
// log.info("========keyValue={}", keyValue);
// String newSign = SecureUtil.getCode(keyValue.toString());
String newSign = SecureUtil.sha1(keyValue.toString());
System.out.println(keyValue.toString());
return newSign;
}
}
Aes加密方法
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import lombok.experimental.var;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AesUtil {
final static String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
/**
* AES加密为base 64 code
* @param content 待加密的内容
* @param encryptKey 加密密钥
* @return 加密后的base 64 code
* @throws Exception
*/
public static String aesEncrypt(String content, String encryptKey) throws Exception {
return new BASE64Encoder().encode(aesEncryptToBytes(content, encryptKey));
}
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
}
jmeter中使用上面jar中的方法
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Calendar;
import java.util.TreeMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tangxinxing.util.rsautil.TestAesUtil;
import com.tangxinxing.util.rsautil.TestSHA1;
import com.tangxinxing.util.rsautil.AesUtil;
//JSONObject object = JSON.parseObject("{\"d\":\"x\",\"a\":\"f\"}");
//jmeter不支持泛型语法,1.5之后的java语法都不支持,只能封装成jar后调用
//TreeMap<String ,Object> billMap = JSONObject.parseObject(object.toJSONString(), new TypeReference<TreeMap<String, Object>>(){});
//System.out.println("billMap"+billMap);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String data = df.format(new Date());
System.out.println(data);// new Date()为获取当前系统时间
//除开sign参数外 并且 bizcontent参数要用密文 进行验签
String jsons= "{\"version\":\"1.0\",\"appId\":\"MH501\",\"flowNo\":1556088922261,\"reqDateTime\":\"${data}\",\"bizContent\":{\"biype\":\"9\"}}";
System.out.println("jsons"+jsons);
//调用str转TreeMap方法,TreeMap会自动对参数做排序
TreeMap buff = TestAesUtil.getMap(jsons);
System.out.println("buff"+buff);
//对参数做SHA1 签名
String sign = TestSHA1.getCode(buff);
System.out.println("sign"+sign);
vars.put("sign",sign);
//对业务参数做aes加密,参数加密的规则在这里只是随便举例,规则需根据各自公司开发人员约定来做
String aesEn = AesUtil.aesEncrypt(jsons,"${aes_key}");
System.out.println("aesEn"+aesEn);
vars.put("aesEn",aesEn);
在请求参数中参数化上面的变量请求成功
"respCode":"100",
"respDateTime":"2019-04-25 11:17:43",
"respMsg":"请求成功"
}
大功告成。