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!