【发布时间】: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