jmeter用cbc加密aes128

rmbxnbpk  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(526)

我想加密一个文本,我用aes加密密钥和向量变量,我有以下代码:

import java.util.zip.GZIPOutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; 

log.info("Beanshell Execution Commenced"); 

String plainText = vars.get("xmlDeclaracion").toString();

//log.info(plainText); 

static final String _Key = vars.get("keybytes");
static final String _Iv = vars.get("iv");

Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec _KeySpec = new SecretKeySpec(_Key.getBytes(), "AES");
IvParameterSpec _IvSpec = new IvParameterSpec(_Iv.getBytes());

_Cipher.init(Cipher.ENCRYPT_MODE, _KeySpec, _IvSpec); 

byte[] cipherText = _Cipher.doFinal(plainText.getBytes("UTF-8")); 

String encryptedResponse = Base64.encodeBase64String(cipherText); 

vars.put("encryptedResponse",encryptedResponse);

但在控制台中显示错误 Error invoking bsh method 当我运行测试时

2020-12-10 18:29:25,154 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2020-12-10 18:29:25,154 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2020-12-10 18:29:25,155 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2020-12-10 18:29:25,155 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Thread Group
2020-12-10 18:29:25,155 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Thread Group.
2020-12-10 18:29:25,155 INFO o.a.j.e.StandardJMeterEngine: Thread will continue on error
2020-12-10 18:29:25,155 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2020-12-10 18:29:25,160 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2020-12-10 18:29:25,160 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2020-12-10 18:29:25,160 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-12-10 18:29:25,160 INFO o.a.j.s.FileServer: Stored: C:/Jmeter/Morales2020/MoralesAnual.csv
2020-12-10 18:29:25,165 INFO o.a.j.u.BeanShellTestElement: Beanshell Execution Commenced
2020-12-10 18:29:25,166 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.zip.GZIPOutputStream; import java.security.InvalidAlgorithmPara . . . '' : Method Invocation _Cipher.init
2020-12-10 18:29:25,166 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.zip.GZIPOutputStream; import java.security.InvalidAlgorithmPara . . . '' : Method Invocation _Cipher.init**
2020-12-10 18:29:25,166 INFO o.a.j.t.JMeterThread: Thread is done: Thread Group 1-1
2020-12-10 18:29:25,166 INFO o.a.j.t.JMeterThread: Thread finished: Thread Group 1-1
2020-12-10 18:29:25,167 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2020-12-10 18:29:25,167 INFO o.a.j.s.FileServer: Close: C:/Jmeter/Morales2020/MoralesAnual.csv
2020-12-10 18:29:25,167 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)
w1e3prcc

w1e3prcc1#

为了获得可读的错误消息,您需要将代码放入try块中,例如:

try {
//your code here
}
catch (Exception ex) {
    log.error("Failure", ex);
}

还要注意,由于jmeter3.1,建议使用jsr223测试元素和groovy语言编写脚本。groovy的性能比beanshell好得多,尤其是在资源密集型加密操作方面。
对代码唯一需要的更改是从变量中删除静态修饰符
所以考虑到你的 xmlDeclaracion , keybytes 以及 iv 变量具有有效值,您的代码应该可以正常工作。
关于jmeter中groovy脚本的更多信息:apachegroovy-为什么以及如何使用它

相关问题