關於Jmeter作Rsa加密解密的一些總結

先上加密腳本:html

import org.apache.commons.codec.binary.Base64; import java.io.ByteArrayOutputStream; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; String RSA_PUB_KEY="你的公鑰";//去掉首尾
String KEY_ALGORITHM = "RSA"; String SIGNATURE_ALGORITHM = "MD5withRSA"; int MAX_ENCRYPT_BLOCK = 117; int MAX_DECRYPT_BLOCK = 128; public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) throws Exception { byte[] keyBytes = Base64.decodeBase64(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // 對數據分段解密
        while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; } public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64.decodeBase64(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key publicK = keyFactory.generatePublic(x509KeySpec); // 對數據加密
        Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicK); int inputLen = data.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(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; } String str=vars.get("body"); String result =""; try { result = Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY)); System.out.println(result); } catch (Exception e) { // TODO Auto-generated catch block
 e.printStackTrace(); } print(result); vars.put("sign",result); vars.put("RSA_PUB_KEY",RSA_PUB_KEY); log.info("加密串=",result); log.info("加密body=",str); return result;

1.由於是加密後的參數放到請求體中,因此我在另一個beanshell定義了一個json格式的字符串,而後這就形成了一個問題:字符串帶轉義符,jmeter對帶轉義符的字符串用$來引用會直接報錯!!!!解決辦法是改用vars.get(「body」)來獲取公共變量java

2.加密後的字符串直接塞進請求體裏了shell

3.返回的報文是私鑰加密,公鑰解密的密文,這裏我選擇了用後置處理器來解密:apache

輸出:將解密結果輸出的參數命名json

輸入:導入rsa公鑰,這在上面的腳本已經提早定義好jsonp

操做:要是解密的是響應的json,則勾選響應文本-jsonpath表達式加密

   要是返回的直接是加密的密文,則只勾選響應文本,不勾選jsonpath,這裏我是使用了這種方法spa

      針對其餘變量解密,勾選變量名並輸入便可.net

所用jmeter的版本:5.1.1 r18551733d

 

下載地址:
連接:https://pan.baidu.com/s/1lpvmNd7jd5v7vG3IGfum2A 提取碼:q8ym

 

 

 

參考連接:https://www.cnblogs.com/artoftest/p/7298929.html

     https://blog.csdn.net/q13554515812/article/details/99298387

相關文章
相關標籤/搜索