性能测试之jmeter使用beanshell实现RSA加密算法

import org.apache.commons.codec.binary.Base64;import java.io.ByteArrayOutputStream;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.Map;import javax.crypto.Cipher;String RSA_PUB_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNPFO1OaKJbLOH7hVzjj8s+k+spSgG7D2imIpR1ukC3xqgEUYP/vYIiZHXnK04Ddk0ELYee5xDbFfTHSWOK6d2lqK0ydWtLFHCdKpBehM/YKa72zf5KaSJGGgag8EQw4o5ZBS/Ia9w2OxYZ1S94OeRXaA+Z4cy8rBui0hTW9Z0pwIDAQAB";
String KEY_ALGORITHM = "RSA";String SIGNATURE_ALGORITHM = "MD5withRSA";int MAX_ENCRYPT_BLOCK = 117;int MAX_DECRYPT_BLOCK = 128;
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)            throws Exception {        byte[] keyBytes = Base64.decodeBase64(publicKey);        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);        Key publicK = keyFactory.generatePublic(x509KeySpec);        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());        cipher.init(Cipher.DECRYPT_MODE, publicK);        int inputLen = encryptedData.length;        ByteArrayOutputStream out = new ByteArrayOutputStream();        int offSet = 0;        byte[] cache;        int i = 0;        // 对数据分段解密        while (inputLen - offSet > 0) {            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);            } else {                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);            }            out.write(cache, 0, cache.length);            i++;            offSet = i * MAX_DECRYPT_BLOCK;        }        byte[] decryptedData = out.toByteArray();        out.close();        return decryptedData;    }
    public static byte[] encryptByPublicKey(byte[] data, String publicKey)            throws Exception {        byte[] keyBytes = Base64.decodeBase64(publicKey);        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);        KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);        Key publicK = keyFactory.generatePublic(x509KeySpec);        // 对数据加密        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());        cipher.init(Cipher.ENCRYPT_MODE, publicK);        int inputLen = data.length;        ByteArrayOutputStream out = new ByteArrayOutputStream();        int offSet = 0;        byte[] cache;        int i = 0;        // 对数据分段加密        while (inputLen - offSet > 0) {            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);            } else {                cache = cipher.doFinal(data, offSet, inputLen - offSet);            }            out.write(cache, 0, cache.length);            i++;            offSet = i * MAX_ENCRYPT_BLOCK;        }        byte[] encryptedData = out.toByteArray();        out.close();        return encryptedData;    }    String str = "idNum=633335199606143151&name=杨先生&phone=17610010005";    //String str = vars.get("RSA1");//获取需要进行rsa加密的参数String result ="";try {    result = Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY));    //System.out.println(result);} catch (Exception e) {    // TODO Auto-generated catch block    e.printStackTrace();}    print(result);vars.put("sign",result);//把加密结果放入变量sign//return result;

更多内容请关注“测试小号等闲之辈”公众号获取~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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”中,可以查看加密后的密文。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值