我的用户定义的类上写有一些加密。采取相同的.jar文件。
课程:Test2
public static PublicKey getKey(String final_key) {
try{
byte[] byteKey = Base64.decode(key.getBytes(), Base64.DEFAULT);
X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key k=kf.generatePublic(X509publicKey);
return (PublicKey) k;
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
主类:
String public_key1="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzIhoSZkc7jyTXifjc2xNeZwZdlDWjX/t3zrLT6+mUlWOxVmS4CHNiGSQ5crJBG1TkWAwTPgjT5mbCvhkzSm8eTHsRciDLfgDx5GJL7dPTtIudwyv+CoyPUq02myuCAwnSwJLHDXO5bqKDZLv5yCA0IkpkMY/NDO71gt2RUNlhKy1Qd/BG+2nVRPl/hvTLnwkAX+0jGgE3NYqjdrI94K4sZBpZA+zjNdKt7r6wEg6cdxye4JDhgrJVtXcIvReNDZscK3OXSpK1w05cJ/iX+9gDY/l+jS4K6LZ3j9rND+Z6uyBd14qtgg34C3ftckNpPQgosp8f+VRb8oXVg8woWjwKwIDAQAB";
String newKey=public_key1.replaceAll("\\\n", "");
System.out.println(newKey);
Key k=getKey(public_key1);
System.out.println(k);
System.out.println("After Encryption:" +encrypt(card1,k));
Jmeter代码:
import com.pack.testing.Test2;
String Original_String= ctx.getPreviousResult().getResponseDataAsString();
String Trim1=Original_String.substring(0, Original_String.lastIndexOf(","));
String Trim2=Trim1.replaceFirst("-----BEGIN PUBLIC KEY-----", "");
String Trim3=Trim2.replaceFirst("-----END PUBLIC KEY-----", "");
String[] parts = Trim3.split(":");
String myString = parts[1].substring(1, parts[1].length()-1);
String final_string=myString.replaceAll("\\\\n", "");
vars.putObject("publicKey", Test2.getKey(final_string));
log.info(publicKey);
在Main类中,我有硬编码的字符串。但实际上,该字符串可以通过jmeter进行更改,我需要将此字符串(从Jmeter接收)传递到该加密类中,以便在Jmeter中获取Key作为输出。