-1

I have got error like "org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import android.util.Base64; import java.security.spec.X509EncodedKeySpec; import . . . '' : Typed variable declaration : Class: def not found in namespace " while running the below Piece ofcode in 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();
    }

I have placed the android.util.Base64 jar file on my lib folder of Jmeter. Help on this is helpful!

Siddish
  • 97
  • 3
  • 12

2 Answers2

1

You are getting this error as you're trying to use Groovy def keyword in the Beanshell sampler which is not supported there, the options are in:

  1. Remove it completely, in Beanshell you can initialize your cipher variable without any helper keywords like:

    cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");
    
  2. (better) Switch to JSR223 Sampler and make sure groovy is selected in the "Language" dropdown. Since JMeter 3.1 it is recommended to use Groovy for scripting as it has much better performance comparing to Beanshell.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Tried with the JSR223 Sampler too, but no response data from the Sampler . No idea what went wrong :( – Siddish Mar 08 '18 at 12:36
1

Since you’re using Beanshell, def in your script should be var.

Otherwise switch to groovy by using JSR223 Test element

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116