Jmeter 二次开发 函数助手 AES加解密

Jmeter 二次开发 函数助手 AES加解密

  • [1. 环境准备](#1. 环境准备)
  • [2. 关键技术说明](#2. 关键技术说明)
    • [2.1 离线导包](#2.1 离线导包)
    • [2.2 示例代码](#2.2 示例代码)
  • [3. 代码包](#3. 代码包)
  • [4. 结果演示](#4. 结果演示)

1. 环境准备

  • IDE :IntelliJ IDEA 2021.1.1 x64
  • JAVA环境 :jdk1.8.0_251
  • 离线导包:导入Jmeter安装目录下lib/ext下的ApacheJmeter_function.jar 和 ApacheJmeter_cotre

2. 关键技术说明

2.1 离线导包

重点

  • 程序依赖的jar包需要放在ext路径下
  • 程序依赖的class需要放在 ApacheJmeter_function 中,和新开发的class放在同级


2.2 示例代码

以AES CBC加密为例

py 复制代码
package org.apache.jmeter.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;

public class AES_CBC_Encrypt extends AbstractFunction {

    //自定义function的描述
    private static final List<String> desc = new LinkedList<>();
    //function名称
    private static final String KEY = "__AES_CBC_Encrypt";

    static {
        desc.add("json_input");
        desc.add("secret_input");
        desc.add("iv_input");
    }

    private CompoundVariable json_input;
    private CompoundVariable secret_input;
    private CompoundVariable iv_input;

    // 函数的逻辑执行主体
    /** {@inheritDoc} */
    @Override
    public String execute(SampleResult previousResult, Sampler currentSampler)
            throws InvalidVariableException {

        String bodyData = String.valueOf(json_input.execute());
        String secret_key = String.valueOf(secret_input.execute());
        String iv_key = String.valueOf(iv_input.execute());
        return AES_CBC.encryptCBC(bodyData, secret_key, iv_key);

    }

    // 用来接收和处理GUI界面的参数的传值
    /** {@inheritDoc} */
    @Override
    public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
        checkParameterCount(collection, 3);
        Object[] values = collection.toArray();

        json_input = (CompoundVariable) values[0];
        secret_input = (CompoundVariable) values[1];
        iv_input = (CompoundVariable) values[2];

    }

    // 用来定义函数的名称,把自定义的内容显示在函数对话框中
    /** {@inheritDoc} */
    @Override
    public String getReferenceKey() {
        return KEY;
    }

    // 用来设置GUI界面的函数对话框,把自己定义的参数给显示在jmeter的GUI界面上
    /** {@inheritDoc} */
    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }

    public static void main(String[] args) {        // AES支持三种长度的密钥:128位、192位、256位。
        // 代码中这种就是128位的加密密钥,16字节 * 8位/字节 = 128位。
        String srt = "123456778";
        String iv_seed = "1234567887654321";
        System.out.println("密钥key:" + srt);
        System.out.println("iv:" + iv_seed);

        System.out.println("--------AES_CBC加密解密---------");
        String cbcResult = AES_CBC.encryptCBC(aes, srt, iv_seed);
        System.out.println("aes_cbc加密结果:" + cbcResult);
        System.out.println();

        System.out.println("---------解密CBC---------");

        String cbcDecrypt = AES_CBC.decryptCBC(cbcResult, srt, iv_seed);
        System.out.println("aes解密结果:" + cbcDecrypt);
        System.out.println();
    }
}

3. 代码包

https://download.csdn.net/download/weixin_39451323/88218178

4. 结果演示

加密:

复制代码
    ${__AES_CBC_Encrypt({"token":"0e84b297-d8f2-4779-b7f1-60966ed19ce1"},qwertyuiiuytrewq,1234567887654321)}

解密:

复制代码
${__AES_CBC_Decrypt(RwHL3jF01WDd9T3tKRUi2XIWuPJ/a5H78mExd8I3/fArE6778r7EsbSUwjx7cncAkkfrfc6QV53NrStWBnissA==,qwertyuiiuytrewq,1234567887654321)}

参考资料:jmeter官方函数https://github.com/ufctester/apache-jmeter/tree/master/src/functions/org/apache/jmeter/functions

相关推荐
一木 之林14 分钟前
四、STL容器与数据结构
开发语言·数据结构·c++
QX_hao17 分钟前
【Go】--Cobra-cli的用法
开发语言·后端·golang
艾醒(AiXing-w)23 分钟前
LangChain 1.0 入门(三):稳定性双核心——重试机制+速率限速器参数详解与实战
开发语言·langchain·php
总有刁民想爱朕ha1 小时前
Python PyQt5图片批量转MP4视频工具:本地离线免费无水印,完整代码
开发语言·python·qt
宸津-代码粉碎机1 小时前
FastUtil+AI多Agent实战:Java AI项目性能终极加速方案
java·服务器·开发语言·python·安全·php
Python私教2 小时前
Python 3.15 来了:free-threading 稳定 ABI 能给高并发服务带来什么
开发语言·python
wuhuhuan2 小时前
【JMeter 学习打卡 Day 7】从会用到会用对,完成一次完整压测
学习·jmeter
Logintern093 小时前
[Matlab] 遗传算法求解TSP入门
开发语言·matlab
练习时长两年半的RL练习生4 小时前
与AI对话后对 Actor-Critic 中 TD Target 的 a‘ 来源总结
开发语言·人工智能·php
十五年专注C++开发4 小时前
100w条数据丝滑滚动!Qt Model/View 架构实战(二)
开发语言·c++·qt