网站轮播图怎么做的,设置网站解析,明星个人网站建设方案,google seo是什么啊本篇介绍jmeter 使用rsa算法进行加密参数
如果测试过程中#xff0c;部分接口采用了rsa加密算法#xff0c;我们的jmeter 也是可以直接拿来调用的#xff0c;不需要开发配合去掉加密代码#xff01;
直接上代码 import org.apache.commons.codec.binary.Base64;
import j…本篇介绍jmeter 使用rsa算法进行加密参数
如果测试过程中部分接口采用了rsa加密算法我们的jmeter 也是可以直接拿来调用的不需要开发配合去掉加密代码
直接上代码 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_KEYMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNPFO1OaKJbLOH7hVzjj8skspSgG7D2imIpR1ukC3xqgEUYP/vYIiZHXnK04Ddk0ELYee5xDbFfTHSWOK6d2lqK0ydWtLFHCdKpBehM/YKa72zf5KaSJGGgag8EQw4o5ZBS/Ia9w2OxYZ1S94OeRXaAZ4cy8rBui0hTW9Z0pwIDAQAB;
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(KEY_ALGORITHM);Key publicK keyFactory.generatePublic(x509KeySpec);// 对数据加密Cipher cipher Cipher.getInstance(keyFactory.getAlgorithm());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 idNum633335199606143151name蔺四十phone17610010005;
String result ;
try {result Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY));System.out.println(result);
} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();
}print(result);
vars.put(sign,result);
return result; 看运行效果 上述代码直接把加密结果放入变量sign中在其他地方如果需要调用加密结果只需要 使用代码${sign}即可
import org.apache.commons.codec.binary.Base64;
引入了jmeter包中的类如果本代码在jmeter环境运行不需要加载第三方jar包
如果在eclipse 或者其他环境中运行需要其他base64的类替换请注意 2023最新Jmeter接口测试从入门到精通全套项目实战教程