jMeter Groovy 使用来自字符串的 rsa 私钥解密消息

jMeter Groovy decrypt message with rsa private key from string

我正在尝试编写 Groovy 脚本以便在 JMeter 的 JSR223 采样器中使用它。 此脚本应获取字符串 RSA 私钥并解密消息。

这是我的代码:

// This is what I want to decrypt
def sessionToken = vars.get('SESSION_TOKEN') 

def cipher = javax.crypto.Cipher.getInstance('RSA')
def factory = java.security.KeyFactory.getInstance("RSA")

// My string private key
def privateKeyString = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAM+lBL+vbq6FyyaGoZqIyHGAWAEhYAKi0wUvve1bvyHZhxTJLSQUYoqDdxZK1MyLnsy75FWQi+oNMdmRtrq7f4FUM9b11pHq83eT57yTWkAUvOX6r0gF7mdcqEoLSG/TxYU5s0qhwSa07JtOzX/EKWKF4Pfmj8+Flagu+hr90MYBAgMBAAECgYEAuITN6YD9/DyMwJmW9fpjFOmWSrrb1LvYhZ1dS5XiDTR+o2v6nzs2LhyRMNAitfnEje7SA29FxiEfkVW5acrAjBTc9wYXAQKXttkL3Ik4NdhJMoM2dBDs2f28z3dWYpRvvTGalHjL4dN9nOfaq/yzFGzC5XbAb7Jo/PoTfD9hMlkCQQDsrqpivsOKL+QZvZQPvEw1yaRUgnZQq36zCH5JiOojIi7sv8aEENDFzD3nKgIEpvMk9MP/AlOCBbRw2jTayyQbAkEA4JegxHCK92mAL28IZNxroG3gfgmfApijvkxeNIWfJ8d4yuY/1cXPg7/XU+4Pvh/i1pUasSa37uG1ArSEnhjIEwJAK8gHlqqJC1fejvBloh+HzW9WaZeUgUmn70BD9CBSh1s8aOj0tNtTczNbxBYeN3QWiCCK3PI2NlgNz85ddpebPwJAKy+88Ekbz7tvtK9LE+n2oCDAvDupYdxsEBmrO7o+Am4u4gUoXTjuUE1BYJg0WsDS46REP7BMShkIr356ydPGFQJAFzC0H3saS2yq7Hs3vXMIbTESI0ro5OAJcgCqBE6sbGqb7MqoTvewtww311Dn0ndXficV1Ihv4I18wdh1kg9Bfg=="

def keyBytes = Base64.getDecoder().decode(privateKeyString.getBytes());
def encodedKeySpec = new java.security.spec.PKCS8EncodedKeySpec(keyBytes)
def privateKey = factory.generatePrivate(encodedKeySpec)

cipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey)
cipherText = cipher.doFinal(sessionToken.getBytes())
log.info('SESSION TOKEN Decrypted: ' + cipherText)

我运行我的代码后,出现错误:

javax.crypto.IllegalBlockSizeException: Data must not be longer than 128 bytes

我不确定自己做错了什么:(.

额外信息:我没有 Groovy / Java 编程经验。

谢谢!

在@Topaco 的帮助下(谢谢!)我能够使脚本正常工作。

首先我必须解码我的信息:

def sessionBytes = Base64.getDecoder().decode(sessionToken.getBytes());

然后我不得不将填充添加到 chiper:

def cipher = javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding")

最后我不得不从密码中解码我的结果:

def string = new String(Base64.getDecoder().decode("YOUR STRING HERE".getBytes()));

最终脚本:

def sessionToken = "N9Tuh2bvXAQulujXQpSq5xJne+hNaUrsue0X1eOccPxZNhCwWt9/7yXgNv6eULRjCv1fiuuBQ3AchJU2qQp7/PuszHqYYzr6G8k0sYsB2oYR226+so5Ntsnaq61JUr461/jtoxnE3P9hqKwz4NIXjhlB78oVhWsOLeVx69wRuGY="

def sessionBytes = Base64.getDecoder().decode(sessionToken.getBytes());

def cipher = javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1Padding")
def factory = java.security.KeyFactory.getInstance("RSA")

def privateKeyString = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKHn8nN2NDU0oP+hEfa1GOie2hVo57MHC76q6/e/yrfLDgH71zIjThZF9CZLHutw0kD2v2bcvsd5AkGqsAj4AvTRqcuNrNQ+AMtYsphhO8DkZoedOHkOnZ6rkAl7NOF7kUBfamR8ztJdtVnA+LTqzzMMaXgylDopHGZg9JN5ScSPAgMBAAECgYBT6VDBAqxAPwyKMEKNKILGXT4OBpa/NJGjuhYeCyrXYsfZw1pew+l+pbbJ+fkGcSynOrssZpAB9fdzbTFbFJ8CyzK1jQPptxMjo37aKxZI9lKVVOgr/pLX13H/61EjTnFOJwcOV6r6I8LQn5ag+qZjb4KE/N+1zT1prvII3iDTgQJBAOMp/NuiG7QQDV9SGgCjNYc37la9BnMIVsVPIfJc5nbI8dKoxrYO1nIN0IQgDbc5H4smybtONeplXia+KVbSzW8CQQC2dU9xlwXvk8am44x08TVsUdQ7sWunLwgPLiwgPOkhDnTODB3DbHnjNLNCuxLo8e+Vp+JHi1YaqgQzMLP3ISrhAkEAn3LMYpnR2jzeNgcZ61Kj8uqmZ8m8aifzSAF4cXcV6VC4tMX03LtjeKELuIILPo1g/7pVJR0LqSBHyuf1elTzDwJBALRjwwmQwKOevLZfHoy3tZPES0pBHSgLTbKEecfdsLen7T+RpxOA+fjyL5D4F7gLCk4xz3vgfF6cXM6nhiX8usECQQC37mWgTd80UFuw0SYb+gGUzu4+zhvtRzQ3EOLYzAcSVm05lWIpIbHQ7YX4Y+xV4c3l60a5Kds4PxuQlLULe2OM"

def keyBytes = Base64.getDecoder().decode(privateKeyString.getBytes());
def encodedKeySpec = new java.security.spec.PKCS8EncodedKeySpec(keyBytes)
def privateKey = factory.generatePrivate(encodedKeySpec)

cipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey)
cipherText = cipher.doFinal(sessionBytes)

def encodedSessionToken = cipherText.encodeBase64().toString()

def decodedSessionToken = new String(Base64.getDecoder().decode(encodedSessionToken.getBytes()));

println decodedSessionToken