【群答疑】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);
结果:解密成功
__EOF__
本文作者:持之以恒(韧)
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!
关于博主:擅长性能、全链路、自动化、企业级自动化持续集成(DevTestOps)、测开等
面试必备:项目实战(性能、自动化)、简历笔试,https://www.cnblogs.com/uncleyong/p/15777706.html
测试提升:从测试小白到高级测试修炼之路,https://www.cnblogs.com/uncleyong/p/10530261.html
欢迎分享:如果您觉得文章对您有帮助,欢迎转载、分享,也可以点击文章右下角【推荐】一下!