Comment Jmeter utilise RSA pour le chiffrement


Préface

Lors des tests d'interface ou des tests de performances, nous rencontrons souvent des situations dans lesquelles les données doivent être cryptées. Cet article présente principalement l'utilisation du cryptage RSA. Le code de cryptage cité provient d’Internet. Je ne sais pas quel expert l’a partagé. Je tiens à vous exprimer ma gratitude !


1. Étapes d'utilisation

1. Ajouter le préprocesseur BeanShell

Insérer la description de l'image ici

2.Écrivez un script

le code s'affiche comme ci-dessous :

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;
import java.net.URLEncoder;
String RSA_PUB_KEY = props.get("pukey");//RSA加密的公钥
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 = "12345678";//这里设置为自己的密码
String result = "";

try {
    
    
    result = Base64.encodeBase64String(encryptByPublicKey(str.getBytes(), RSA_PUB_KEY));

    result1 = encryptByPublicKey(str.getBytes(), RSA_PUB_KEY);
    System.out.println(result);
} catch(Exception e) {
    
    
    // TODO Auto-generated catch block
    e.printStackTrace();
}

print(result);
vars.put("rsa_pwd", result);//rsa_pwd是jmeter脚本中引用的变量名
return result1;

 


Analyse du code :
1. La ligne 10 remplit la clé publique de RSA.
Insérer la description de l'image ici
2. La ligne 81 remplit la chaîne d'origine qui doit être cryptée. Par exemple, si vous souhaitez crypter le mot de passe, remplissez le mot de passe ici.
Insérer la description de l'image ici

3. La ligne 95 du code stocke le résultat de la chaîne cryptée en tant que variable Jmeter pour une référence facile dans Jmeter.
Insérer la description de l'image ici


Résumer

C'est de ce qui précède que je vais parler aujourd'hui. Cet article ne présente que brièvement comment utiliser RSA pour le chiffrement dans Jmeter.

Je suppose que tu aimes

Origine blog.csdn.net/liangxiaoyan0426/article/details/130864278
conseillé
Classement