在登录时遇到加密是比较常见的场景,也为安全一般是从服务器动态获取公钥,再根据公钥对密码明文进行rsa加密,做为密码再进行登录使用。所以需要对输入的密码进行处理,即在登录接口中添加前置JSR223进行处理,如下图所示:
所使用的都是java标准库里的函数,所以可以直接使用。具体代码如下:文章来源地址https://www.toymoban.com/news/detail-811001.html
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,)};
文章来源:https://www.toymoban.com/news/detail-811001.html
到了这里,关于jmeter根据公钥base64对明文密码进行rsa加密的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!