jmeter使用rsa进行sign加密

1.在需要加密的请求下添加BeanShell预处理程序

2.编写脚本,不需要引入任何的jar包和js包(因为个人引入时一直报错,一气之下直接放弃了)

代码如下:

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;
import java.security.KeyFactory;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;


    /**
     * RSA算法
     */
    public static String RSA = "RSA";
    /**
     * 加密方式,android的
     */
    public static  String TRANSFORMATION = "RSA/ECB/PKCS1Padding";

    public static  int DEFAULT_KEY_SIZE = 512;//秘钥默认长度

    public static  String charsetName = "ISO8859-1";//指定编码  密文实质就是乱码  UTF-8 无法解码

    /**
     * RSA最大加密明文大小
     */
    private static  int MAX_ENCRYPT_BLOCK = (DEFAULT_KEY_SIZE / 8) - 11;

    /**
     * RSA最大解密密文大小
     */
    private static  int MAX_DECRYPT_BLOCK = (DEFAULT_KEY_SIZE / 8);


    public static String toURLEncoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }

        try {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLEncoder.encode(str, "UTF-8");
            return str;
        } catch (Exception localException) {
        }
        return "";

    }


    public static String toURLDecoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }

        try {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLDecoder.decode(str, "UTF-8");
            return str;
        } catch (Exception localException) {
        }

        return "";
    }

    /**
     * Return the urlencoded string.
     *
     * @param input       The input.
     * @param charsetName The name of charset.
     * @return the urlencoded string
     */
    public static String urlEncode(String input, String charsetName) {
        if (input == null || input.length() == 0) return "";
        try {
            return URLEncoder.encode(input, charsetName);
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    }


    /**
     * Return Base64-encode bytes.
     *
     * @param input The input.
     * @return Base64-encode bytes
     */
    public static byte[] base64Encode(byte[] input) {
        if (input == null || input.length == 0) return new byte[0];
        return Base64.encode(input, Base64.NO_WRAP);
    }

    /**
     * Return the bytes of decode Base64-encode string.
     *
     * @param input The input.
     * @return the string of decode Base64-encode string
     */
    public static byte[] base64Decode(String input) {
        if (input == null || input.length() == 0){
            return new byte[0];
            } 
        return Base64.decode(input, Base64.NO_WRAP);
    }

    /**
     * RSA公钥加密
     *
     * @param str       加密字符串
     * @param publicKey 公钥
     * @return 密文
     * @throws Exception 加密过程中的异常信息
     */
    public static String encryptPublicKey(String str, String publicKey,String charsetName) {
        //base64编码的公钥
        String encrypted = null;
        try {
            byte[] decoded = Base64.decodeBase64(publicKey);
           

            // 得到公钥对象
            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decoded);
            KeyFactory keyFactory = KeyFactory.getInstance(RSA);
            Key publicK = keyFactory.generatePublic(x509KeySpec);


            //RSA加密
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(Cipher.ENCRYPT_MODE, publicK);

            byte[] strBytes = str.getBytes(charsetName);

            int inputLen = strBytes.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(strBytes, offSet, MAX_ENCRYPT_BLOCK);
                } else {
                    cache = cipher.doFinal(strBytes, offSet, inputLen - offSet);
                }
                out.write(cache, 0, cache.length);
                i++;
                offSet = i * MAX_ENCRYPT_BLOCK;
            }
            byte[] bytes = out.toByteArray();
            out.close();
            encrypted = Base64.encodeBase64String(bytes);
        } catch (Exception e) {

        }


        return encrypted;

    }


String requestTime = String.valueOf(System.currentTimeMillis());//使用该脚本实时的获取时间戳
String toURLEncoded = "app_id=6666666666666666666666666666&app_key=66666666666666666666666666666&ext_info={\"sdk_version\":\"1\",\"rom_platform\":\"android\",\"sdk_platform\":\"1565\",\"device_id\":\"9CE9E2613F0C04C1607A29AE716017BAC3E8ADAF\",\"game_id\":\"78\",\"phone_model\":\"Meizu_M973Q\"}&is_sign=20&request_time=" + requestTime;//这里填写需要加密的值

String temp = toURLEncoded(toURLEncoded);



String PUNLIC_KEY = "此处填入公钥";


String encrypt = encryptPublicKey(temp, PUNLIC_KEY, "UTF-8");
vars.put("time",requestTime);//把时间戳当成jmeter变量传回请求参数中
vars.put("sign",encrypt);//把加密值传回请求参数中


    
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值