Jmeter使用beanshell对接口加密,调用AES代码的jar包,CBC模式,有偏移量。
  5Yhi9zYOlhpx 2023年11月01日 58 0

工作中需要对接口进行AES加密,找开发要来了加密的代码(如下),记录下具体的使用方法:

  1. 在IDEA中新建一个AES1模块,在里面新建一个类(类的名字为AESu1)。

  2. 把下面的代码复制进去,

  3. 这样,AES加密的方法就构建好了。接下来就是导出为jar包了。文件-项目结构-工件-点击+号,

    image
    image
    3.1 点击构建-构建工件
    image
    3.2 再选中AES1.jar,点击构建,jar就创建好了
    image

  4. 把生成的jar包,放到jmeter的lib路径下(有的文章说需要放到lib的ext路径下),不过我是放到lib就已经可以使用了。

  5. 在jmeter中,需要把jar包的路径,放到测试计划中。

  6. 在beanshell中import AES1.*;填入代码即可

这是AES加密代码

package AES1;  
  
import sun.misc.BASE64Encoder;  
import javax.crypto.Cipher;  
import javax.crypto.spec.IvParameterSpec;  
import javax.crypto.spec.SecretKeySpec;  
import java.io.UnsupportedEncodingException;  
import java.nio.charset.StandardCharsets;  
import java.security.AlgorithmParameters;  
import java.security.Key;  
public class AESu1{  
  
    private static final String ALGORITHM = "AES/CBC/PKCS5PADDING";//加密模式,设置为CBC  
    private static final String IV_SPEC = "ZE*84J]YE@11a=?9";//偏移量,可自行更改。  
    public static String encrypt(String content, String token) throws UnsupportedEncodingException {  
        //对content加密,token为加密的秘钥,长度要为16位  
        byte[] result = encryptByte(content.getBytes(StandardCharsets.UTF_8), token.substring(0, 16).getBytes(StandardCharsets.UTF_8));  
        //return Base64.encodeBase64URLSafeString(result);  
        return new BASE64Encoder().encode(result);  
  
    }  
  
    static byte[] encryptByte(byte[] content, byte[] token) {  
        if (content == null || token == null) {  
            return null;  
        }  
        try {  
            Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, token);  
            return cipher.doFinal(content);  
        } catch (Exception e) {  
            // log.error("AES encrypt failed", e);  
        }  
        return null;  
    }  
  
    private static Cipher initCipher(int mode, byte[] token) throws Exception {  
        if (token == null) {  
            return null;  
        }  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        Key key = new SecretKeySpec(token, "AES");  
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");  
        params.init(new IvParameterSpec(IV_SPEC.getBytes(StandardCharsets.UTF_8)));  
        cipher.init(mode, key, params);  
        return cipher;  
    }  
  
    public static void main(String[] args) throws UnsupportedEncodingException {  
        String a2 = "1234";  
        String pswEncrypt = AESu1.encrypt(a2,"10b457959d4a7a30");  
        System.out.println(pswEncrypt);  
  
    }  
}


beanshell中的脚本

import AES1.*;//导入AES1包
 
String a1 = AESu1.encrypt("1234","10b457959d4a7a31");	//1234是需要加密的字符,10b457959d4a7a31是加密的秘钥,长度需要为16位	
vars.put("a1",a1);//把a1的内容,赋值给jmeter的变量a1

log.info("a1是"+a1);//打印,显示为加密后的内容

ps:当需要加密的字符有双引号时,如果直接定义String r ="{"state":"FAILURE","code":"A000204","result":"验证码不正确"}",
,可能jmeter会报错

可以通过prev函数,把返回体中需要的部分,直接传给String,举例如下:

String r = prev.getResponseDataAsString();
String params= AESu1.encrypt(r,"10b457959d4a7a30")

文章部分内容所借鉴的部分:https://blog.csdn.net/qq_25375659/article/details/80944646?spm=1001.2014.3001.5502

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  K9VoqAoS5QtN   2024年05月08日   83   0   0 软件测试
5Yhi9zYOlhpx
最新推荐 更多