【问题标题】:Jmeter Encrypt AES 128 with CBCJmeter 使用 CBC 加密 AES 128
【发布时间】:2020-12-11 00:38:32
【问题描述】:

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

【问题讨论】:

  • 您正在尝试在 CBC 模式下使用 AES 加密,该模式需要一个精确的 16 个字节(即 AES 的块长度)的初始化向量 (IV),但您使用的是从 2 个字符长的字符串中获取的 IV [将产生一个 2 字节长的数组]。尝试使用 16 个字符长的字符串。其次,加密密钥也是如此 - AES“128”由 128/8 = 16 字节长的密钥定义 - 也附加您的密钥字符串。 安全警告 - 上述代码使用固定密钥和 IV,使其不安全。
  • 谢谢迈克尔,在这种情况下我的 IV 错了

标签: java encryption jmeter beanshell


【解决方案1】:

为了获得人类可读的错误消息,您需要将代码放入 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 用于编写脚本。与 Beanshell 相比,Groovy 的性能要好得多,尤其是在资源密集型加密操作方面。

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

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

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

【讨论】:

  • 谢谢@Dmitri T,这些行帮助我准确识别错误。
猜你喜欢
  • 2013-08-11
  • 2019-08-17
  • 2020-04-15
  • 2021-06-25
  • 1970-01-01
  • 2017-03-27
  • 2020-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多