当前位置: 首页 > news >正文

jmeter接口测试使用rsa加密解密算法

本篇介绍jmeter 使用rsa算法进行加密参数

如果测试过程中,部分接口采用了rsa加密算法,我们的jmeter 也是可以直接拿来调用的,不需要开发配合去掉加密代码!

直接上代码

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 result ="";
try {result = Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY));System.out.println(result);
} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();
}print(result);
vars.put("sign",result);
return result;

看运行效果

上述代码,直接把加密结果放入变量sign中,在其他地方,如果需要调用加密结果,只需要 使用代码:${sign}即可

import org.apache.commons.codec.binary.Base64;
引入了jmeter包中的类,如果本代码在jmeter环境运行,不需要加载第三方jar包
如果在eclipse 或者其他环境中运行,需要其他base64的类替换,请注意!

2023最新Jmeter接口测试从入门到精通(全套项目实战教程)

http://www.lryc.cn/news/196229.html

相关文章:

  • IDEA通过Docker插件部署SpringBoot项目
  • 微查系统,一站式查询,让您的查询更加便捷
  • C++stack和queue模拟实现以及deque的介绍
  • WPF ListView 鼠标点击,移动改变背景色不启作用
  • Maven Dependency 机制
  • CustomShapes/自定义形状, CustomCurves/自定义曲线, AnimateableData/数据变化动画 的使用
  • 软件测试用例设计方法-因果图法
  • 水库大坝安全监测是什么和主要作用?
  • 极品三国新手攻略之进阶篇
  • windows应用程序告警:帐户名与安全标识间无任何映射完成
  • 自定义jenkins镜像提示FontConfiguration.head错误
  • 《软件方法》2023版第1章(10)应用UML的建模工作流-大图
  • The given SOAPAction http__xxxxx_xx does not match an operation
  • 【java零基础入门到就业】第二天:jdk的下载安装和第一个HelloWorld程序
  • C++数据结构X篇_15_求二叉树叶子数与高度(递归方法)
  • MySQL锁学习笔记
  • 如何将前后端分离项目部署到本地的Docker Desktop容器运行并且访问
  • 前端开发中的try...catch
  • 数据加密中,采用密钥管理系统相比加密机的好处
  • Elasticsearch:什么是大语言模型 (LLMs)?
  • 神奇的python的生成器
  • 【来点小剧场--项目测试报告】个人博客项目自动化测试
  • 【安卓环境搭建报错的解决】
  • Pruning Pre-trained Language Models Without Fine-Tuning
  • Java内存模型-Java Memory Model(JMM)-可见性、原子性、有序性
  • 基于Springboot实现在线答疑平台系统项目【项目源码+论文说明】
  • 前端工程化知识系列(1)
  • xml文件报错 ORA-00907: 缺失右括号
  • VScode platformio的使用
  • transformer_01