公司最新項目要求對接口參數進行md5加密,大概思路:現將輸入參數先進行ASCLL再進行MD5加密
一、使用eclipse編寫MD5加密方法(附上本人代碼)
package com.md5;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class TestMd5 {
public static String md5(String[] value) {
StringBuilder result = new StringBuilder();
String re_md5 = new String();
String sign;
String arr;
String signKey = "68c163f20f994e82a09c41cbe71ea5ac";
long nonce_str=System.currentTimeMillis();
sign=nonce_str+signKey;
if (value.length==0) {
//String re_md5 = new String();
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sign.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// return re_md5.toUpperCase();
}else {
Arrays.sort(value);
for(int i=0;i<value.length;i++){
result.append(value[i]);
}
String str = result.toString();
arr=str+nonce_str+signKey;
//return result.toString();
//return arr;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(arr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
re_md5 = buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
// return re_md5.toUpperCase();
}
return re_md5.toUpperCase();
}
}
二、導出jar包
三、將jar包添加到jmeter的lib/ext目錄下面
四、新建jmeter的BeanShell Sampler進行調用
注意:本人使用的是將對應jar報添加到jmeter的lib/ext目錄下面,開始的時候使用source函數進行添加老是失敗,具體原因自己也沒搞清楚。