1

JMeter using jsr223 preprocessor wanted to use groovy as language. How to convert present code to groovy so that below code don't cause any performance bottlenecks when executed as part of each thread

The script looks like this:

enter image description here

Masud Jahan
  • 3,213
  • 2
  • 20
  • 33

1 Answers1

1
  1. StackOverflow is not a code writing service, if you want someone to do your job for you consider posting it on one of freelance agencies
  2. Going forward avoid posting code as image, I doubt anyone will be willing to use OCR tools or re-typing your code to reproduce your issue
  3. I strongly doubt that your code works given it relies on this JSEncrypt library

With regards to your question itself, you can use JDK Securit API in order to encrypt whatever string you want using whatever algorithm you like.

Example Groovy code:

def cipher = javax.crypto.Cipher.getInstance('RSA')
def factory = java.security.KeyFactory.getInstance("RSA")
def publicKeyString='your_public_key_here'
def  encodedKeySpec = new java.security.spec.X509EncodedKeySpec(publicKeyString.decodeBase64())
def publicKey = factory.generatePublic(encodedKeySpec)
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, publicKey)
cipherText = cipher.doFinal('the string you want to encode'.getBytes())
log.info('Encrypted: ' + cipherText.encodeBase64())

Demo:

enter image description here

More information: Apache Groovy - Why and How You Should Use It

Dmitri T
  • 140,090
  • 4
  • 71
  • 123
  • I tried to run above lines of code and i ran into errors Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF javax.script.ScriptException: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: Detect premature EOF at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java) – JMeter User Oct 16 '19 at 16:35
  • Replace `your_public_key_here` with the actual content of the private key file – Dmitri T Oct 16 '19 at 16:55
  • I did and it looks as below def cipher = javax.crypto.Cipher.getInstance('RSA') def factory = java.security.KeyFactory.getInstance("RSA") def publicKeyString='30820222300d06092a864886f70d01010105000382020f003082020a02820201009d865a' def encodedKeySpec = new java.security.spec.X509EncodedKeySpec(publicKeyString.decodeBase64()) def publicKey = factory.generatePublic(encodedKeySpec) cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, publicKey) cipherText = cipher.doFinal('asdasdfasrasf'.getBytes()) log.info('Encrypted: ' + cipherText.encodeBase64()) – JMeter User Oct 16 '19 at 22:03
  • It doesn't look like a public key to me, make sure to copy it **in full**: https://stackoverflow.com/questions/12749858/rsa-public-key-format – Dmitri T Oct 17 '19 at 03:06
  • publicKey was too long to paste here..But i copied it on code..As it is long so i split them in multi lines in code for e.g def PKEY='30820222300d06092a864886f70d01010105000382020f003082020a02820201009d865a469cd1672fe3dfcd9f15a8d4bb22de008a9f60748919aa8b2996\n'+'c04004b91f74553616b64b7d6d2626bef38f7a0dbd788d7026ef2ab4993eb77e551fda1569b31e81656e20629ec070730462e8c12b8ba79447fd7afb00de656a199878\n'+ '2149154d94d3be6ef2453365442c3830b6b609b2218d7481306f98bbffc7c44683fb520d68415ca73108e10b5790f5a32b73a6ee739748144173f71eddfbd7963f7746\n' – JMeter User Oct 17 '19 at 21:19
  • Still getting java.security.InvalidKeyException: invalid key format error. Do i need to parsekey ? – JMeter User Oct 21 '19 at 19:17