Reputation: 21
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:
Upvotes: 1
Views: 872
Reputation: 168147
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:
More information: Apache Groovy - Why and How You Should Use It
Upvotes: 1