错误 - org.apache.jorphan.util.JMeterException:调用bsh方法时出错:eval源文件:

时间:2018-03-08 12:15:35

标签: encryption jmeter jmeter-3.2

我遇到错误,如“ org.apache.jorphan.util.JMeterException:错误调用bsh方法:eval源文件:内联评估:``import android.util.Base64; import java.security.spec .X509EncodedKeySpec; import ...':'类型变量声明:类:在命名空间中找不到def “在Jmeter中运行下面的代码片段:

import android.util.Base64;
import java.security.spec.X509EncodedKeySpec;
import java.security.Key;
import java.security.KeyFactory;
import java.security.PublicKey;
import javax.crypto.Cipher;

String Original_String= ctx.getPreviousResult().getResponseDataAsString();
String Trim1=Original_String.substring(0, Original_String.lastIndexOf(","));
String Trim2=Trim1.replaceFirst("-----BEGIN PUBLIC KEY-----", "");
String Trim3=Trim2.replaceFirst("-----END PUBLIC KEY-----", "");
String[] parts = Trim3.split(":");
String myString = parts[1].substring(1, parts[1].length()-1);
String final_string=myString.replaceAll("\\\\n", "");
log.info(final_string);
String input="4000221111111111";

try{
        byte[] byteKey = Base64.decode(final_string.getBytes(), Base64.DEFAULT);
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        Key k=kf.generatePublic(X509publicKey);
        //return (PublicKey) k;
        def cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, k);
        byte[] encryptedBytes = cipher.doFinal(input.getBytes());
        SampleResult.setResponseData( Base64.encodeToString(encryptedBytes, Base64.NO_WRAP));
}
    catch(Exception e){
        e.printStackTrace();
    }

我已将android.util.Base64 jar文件放在Jmeter的lib文件夹中。对此有所帮助很有帮助!

2 个答案:

答案 0 :(得分:1)

当您尝试在Beanshell采样器中使用Groovy def keyword时,您会收到此错误,但该选项位于:

  1. 完全删除它,在Beanshell中,您可以初始化cipher变量,而不需要任何帮助关键字:

    cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
    
  2. (更好)切换到JSR223 Sampler并确保在“语言”下拉列表中选择groovy。从JMeter 3.1开始,recommended使用Groovy进行脚本编写,much better performance comparing to Beanshell

答案 1 :(得分:1)

由于您使用的是Beanshell,因此脚本中的def应为var。

否则使用JSR223 Test元素

切换到groovy