Jmeter 使用 CBC 加密 AES 128

Jmeter Encrypt AES 128 with CBC

我想加密文本,我正在使用带有密钥和向量变量的 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*)

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

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

另请注意,since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language 用于编写脚本。 Groovy 与 Beanshell 相比具有更好的性能,尤其是在涉及资源密集型加密操作时。

唯一需要对代码进行的更改是从变量中删除 static 修饰符

因此,鉴于您的 xmlDeclaracionkeybytesiv 变量具有有效值,您的代码应该可以正常工作。

有关 JMeter 中 Groovy 脚本的更多信息:Apache Groovy - Why and How You Should Use It