jmeter根据公钥base64对明文密码进行rsa加密

在登录时遇到加密是比较常见的场景,也为安全一般是从服务器动态获取公钥,再根据公钥对密码明文进行rsa加密,做为密码再进行登录使用。所以需要对输入的密码进行处理,即在登录接口中添加前置JSR223进行处理,如下图所示:
在这里插入图片描述
所使用的都是java标准库里的函数,所以可以直接使用。具体代码如下:

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;

String plaintext = "2";
//String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiSmbl2XOiGkbR_7oY2ja8Da9DPTZTeINKsIUHM3sGY_rbo3boN13Hq20APD1374_VWwgJQaSzbhzTV6Aoo6t_GG7-Fsa79CT5Kmtb5ycuvXbw0KCCIydmlu9OR9M697T4CX276HfadV40pY0qp5dsduFefqlOwSvv-xs2affS6jEHs5VKhGO6b3YTvscwRzCxQMK06YNL-RIFnr9TTz7bLeGGdos94tquB2Ci914jAJzt27t9W0haOVvX5MuNs6p5xZJI6ju5yabI4fFXvyo9gWPOqrW57dwyFCa0fJATGMlvqGGP5Qm_2y0Il_dCZLFqv-nKs3imOwMrkCGYHA8JQIDAQAB";

String publicKeyStr = vars.get("publicKey");

// Convert public key string to PublicKey object;java 8及更高版本使用Base64.getDecoder().decode(publicKeyStr) 进行解码,否则使用Base64.decodeBase64(publicKeyStr)进行解码
byte[] publicKeyBytes = Base64.decodeBase64(publicKeyStr);
//byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr) 

X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory factory = KeyFactory.getInstance("RSA");
PublicKey publicKey = factory.generatePublic(spec);

// Encrypt plaintext using RSA
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes("UTF-8"));

// Convert encrypted bytes to base64 string
String encryptedStr = Base64.encodeBase64String(encryptedBytes);

// Set the result variable for JMeter
vars.put("encryptedPassword", encryptedStr);
//${__setProperty(encryptedPassword,encryptedStr,)};
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter支持RSA公钥加密算法,可以使用Java Cryptography Extension (JCE)提供的RSA算法。以下是使用JMeter进行RSA公钥加密的步骤: 1. 首先,需要将公钥文件导入到JMeter中。在JMeter的“Test Plan”中,右键单击“Add”->“Config Element”->“Keystore Configuration”,然后在“Keystore Configuration”中选择“JKS”作为“Keystore Type”,并指定公钥文件的路径和密码。 2. 接下来,在JMeter的“Test Plan”中,右键单击“Add”->“Sampler”->“Debug Sampler”,然后在“Debug Sampler”中输入要加密明文。 3. 在“Debug Sampler”中,添加一个“JSR223 Sampler”,并在“Script Language”中选择“groovy”。然后在“Script”中输入以下代码: ```groovy import java.security.KeyFactory; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; import java.util.Base64; String publicKey = vars.get("publicKey"); // 从变量中获取公钥 String plainText = vars.get("plainText"); // 从变量中获取明文 byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey); // 将公钥字符串解码为字节数组 RSAPublicKeySpec keySpec = new RSAPublicKeySpec(publicKeyBytes, new byte[]{1,0,1}); // 构造公钥规范 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // 获取RSA密钥工厂 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // 获取RSA加密器 cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec)); // 初始化加密器 byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); // 加密明文 String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); // 将加密后的字节数组编码为字符串 vars.put("encryptedText", encryptedText); // 将加密后的密文保存到变量中 ``` 4. 在“JSR223 Sampler”中,添加一个“View Results Tree”,并运行测试计划。在“View Results Tree”中,可以查看加密后的密文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值