jmeter encryption and decryption (encryption)

Just happened to get jmeter encryption and decryption recently, you can share it. (I haven't written it for a while, I don't know where to start it, this one is a bit messy o (╥﹏╥) o)

The requirement is: some parameters of the request body in the charging interface need to be encrypted and then requested, and the returned result needs to be decrypted. In a word, encryption is requested, and decryption is returned.

1. First of all, make it according to the normal request.

 

 2. Then, add the encrypted and decrypted jar package provided to the test to the test plan. Of course, you can also directly put these jar packages in the jmeter installation directory jmeter \ apache-jmeter-5.1.1 \ lib \ ext so that you do n’t need to add them to the test plan. (The jar package is sometimes not complete, how to confirm that your jar package is complete, the following will explain)

 

3. Then, create a new Beanshell preprocessor under the request (billing). Like the jar packages we added, the Beanshell preprocessor also needs to be imported. Import is used here. (Because there are class files in the jar package, you can't see the actual code. If you want to see the corresponding method of encryption and decryption, you can ask the developer to develop the java file of this encryption and decryption class (RSAUtils))

Beanshell and Java coding are the same logic, but some types are different in format.

 

4. Then, you can write the code below. According to the RSAUtils.java file, there are two types of encryption, private key encryption and public key encryption, and the corresponding decryptions are private key decryption and public key decryption. Only one explanation is used here, and the examples used are public key encryption and public key decryption.

The public key encryption method is publicEncrypt (data, publicKey), which means we need to set 2 variables: data and publicKey. The data is in json format.

 

First write according to the simplest idea. In order to ensure that the code in the beanshell preprocessing program can be used normally, first write the values ​​of the data and publicKey public keys. (One thing to note here is that json needs to be escaped in jmeter's beanshell, otherwise an error will be reported)

If json is not escaped, an error will be reported. such as:

String data = "{"cityAreaCode":"110100","distAreaCode":"110119","provinceAreaCode":"110000","templateId":"1239454139274485761","productId": "1245235805219303425","insuranceFlag": 1,"insurance": 300,"insuranceNum": 5.0,"insuranceValue":"9000"}";

System.out.println("json:"+data);//打印

Error information in the log:

Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    In file: inline evaluation of: ``import scloudcoreSNAPSHOT.*; import com.ems.gov.cloud.core.util.RSAUtils; import . . . '' Encountered "cityAreaCode" at line 13, column 18.

 

(If part of the json value itself has an escape character, it needs to be escaped twice)

 

5. From the RSAUtils.java file, you can see the encryption method publicEncrypt (data, publicKey). Then I want to get the encryption result, which is RSAUtils.publicEncrypt (data, publicKey). Then the encryption code of the beanshell preprocessor is written:

 

Print out the results, and see here that the encryption has been successful.

 

6. How do we give the encrypted value to the request body? If you directly use the beanshell preprocessing to define the encryptedMsg variable, execute:

 

 

 

Then we need to define a global variable, but here we also need to pay attention to, can not be set in a fixed variable, because the value obtained is empty. To set it up in the test, I set it to sas, beanshell also needs to add a line of code: vars.put ("sas", encryptMsg); // Assign the encrypted value to the global variable

 

 

 

 

 

Execution, look at the results: you can see that the request body has obtained the encrypted value.

 

At this point, the encryption is complete.

How to judge whether the jar package is complete:

Here I first comment out a part of the imported jar packages and classes. After executing the following, you will find that the JSONObject class and RSAUtils.publicEncrypt method are not found. Because it has been prompted, but I don't know which jar packages are missing. Occasionally, try {} catch () {} was added, and I found that it would prompt which specific classes are missing, so that the development can provide the corresponding jar package ~

 

After adding try {} catch () {}, execute it and find out which jar packages are missing:

 

Found that com / baomidou / mybatisplus / toolkit / IOUtils is missing, let the development export the corresponding jar package, assuming the jar package name: mybatisplussup, then we need to add this jar package in the test plan, and in the beanshell preprocessing program, we need to add 2 Line code: import mybatisplussupport. *;

import com.baomidou.mybatisplus.toolkit.IOUtils;

Summary: The log shows compilation errors, such as variable types and missing methods. It is more intuitive to see the problem; try {} catch () {} can throw missing classes and result in null.

Guess you like

Origin www.cnblogs.com/lv-lxz/p/12672655.html