I want to encrypt a text, I'm using the AES encryption with key and Vector variables and I have the following code:
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);
But in Console shows the ERROR Error invoking bsh method
when I run the test
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*)
In order to get the human-readable error messages you need to put your code inside the try block like:
try {
//your code here
}
catch (Exception ex) {
log.error("Failure", ex);
}
Also be aware that since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting. Groovy has much better performance comparing to Beanshell especially when it comes to resource-intensive cryptographic operations.
The only required change to your code would be removing static modifiers from the variables
So given your xmlDeclaracion
, keybytes
and iv
variables have valid values your code should work fine.
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It