【群答疑】jmeter实现aes解密

需求

调用aesDecrypt方法解密

String keyword = "eb9dc4aa69d6582693ba2e3ebe2fe260";
String data = "C4564EEBA5C956464B1A050F4961A7C5A41D33414BA8175B668ACF8BFAB228D484BD2FB175C854F5DDBF13470D61AD073E6432C45B81303578BF004917B8DEE4";

期望结果:

{"transType": "V2101","transDate": "2010-12-24 12:25:15"}

方案:beanshell引入java文件

SHATest.java

package com.test;

import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class SHATest {
    private static int digit = 128;
    private static String  keyword = "eb9dc4aa69d6582693ba2e3ebe2fe260"; 

    // aes解密
    public static String aesDecrypt(String content, String password) throws Exception {
        byte[] contentByte = parseHexStr2Byte(content);
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(password.getBytes("UTF-8"));
        kgen.init(digit, random);

        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();

        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key); 
        byte[] result = cipher.doFinal(contentByte);
        String returStr = new String(result,"UTF-8");
        return returStr;

    }

    /**
     * 将16进制转换为二进制
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }
}

beanshell脚本

source("E:\\myjava\\demo\\src\\main\\java\\com\\test\\SHATest.java");

String keyword = "eb9dc4aa69d6582693ba2e3ebe2fe260";
String data = "C4564EEBA5C956464B1A050F4961A7C5A41D33414BA8175B668ACF8BFAB228D484BD2FB175C854F5DDBF13470D61AD073E6432C45B81303578BF004917B8DEE4";

String res = SHA256.aesDecrypt(data,keyword);
log.info("========="+res);

结果:解密成功

【bak】

你可能感兴趣的:(jmeter,python,开发语言)