Join JMeter Advanced Technology QQ Group: 572445436, participate in JMeter technical Exchange
This article describes jmeter encryption parameters using the RSA algorithm
If the test process, some of the interface using RSA encryption algorithm, our JMeter can also be called directly, do not need to develop and remove the encryption code!
Directly on the code
Importorg.apache.commons.codec.binary.Base64;ImportJava.io.ByteArrayOutputStream;ImportJava.security.Key;Importjava.security.KeyFactory;ImportJava.security.KeyPair;ImportJava.security.KeyPairGenerator;ImportJava.security.PrivateKey;ImportJava.security.PublicKey;Importjava.security.Signature;ImportJava.security.interfaces.RSAPrivateKey;ImportJava.security.interfaces.RSAPublicKey;ImportJava.security.spec.PKCS8EncodedKeySpec;ImportJava.security.spec.X509EncodedKeySpec;ImportJava.util.HashMap;ImportJava.util.Map;ImportJavax.crypto.Cipher; String Rsa_pub_key= "migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqdnpfo1oakjbloh7hvzjj8s+k+spsgg7d2imipr1ukc3xqgeuyp/ vyiizhxnk04ddk0elyee5xdbffthswok6d2lqk0ydwtlfhcdkpbehm/yka72zf5kasjgggag8eqw4o5zbs/ia9w2oxyz1s94oerxaa+ Z4cy8rbui0htw9z0pwidaqab "; String Key_algorithm= "RSA"; String Signature_algorithm= "Md5withrsa";intMax_encrypt_block = 117;intMax_decrypt_block = 128; Public Static byte[] Decryptbypublickey (byte[] EncryptedData, String publickey)throwsException {byte[] Keybytes =base64.decodebase64 (PublicKey); X509encodedkeyspec X509keyspec=NewX509encodedkeyspec (keybytes); Keyfactory keyfactory=keyfactory.getinstance (Key_algorithm); Key Publick=keyfactory.generatepublic (X509KEYSPEC); Cipher Cipher=cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.decrypt_mode, Publick); intInputlen =encrypteddata.length; Bytearrayoutputstream out=NewBytearrayoutputstream (); intOffSet = 0; byte[] Cache; inti = 0; //fragment decryption of data 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 (); returnDecrypteddata; } Public Static byte[] Encryptbypublickey (byte[] data, String PublicKey)throwsException {byte[] Keybytes =base64.decodebase64 (PublicKey); X509encodedkeyspec X509keyspec=NewX509encodedkeyspec (keybytes); Keyfactory keyfactory=keyfactory.getinstance (Key_algorithm); Key Publick=keyfactory.generatepublic (X509KEYSPEC); //Encrypt the dataCipher Cipher =cipher.getinstance (Keyfactory.getalgorithm ()); Cipher.init (Cipher.encrypt_mode, Publick); intInputlen =data.length; Bytearrayoutputstream out=NewBytearrayoutputstream (); intOffSet = 0; byte[] Cache; inti = 0; //fragment encryption of data 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 (); returnEncryptedData; } String str= "Idnum=633335199606143151&name= Rush 40 &phone=17610010005"; 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);returnResult
See the effect of running
The above code, directly put the encryption results in the variable sign, elsewhere, if you need to invoke the encryption results, only need to use the code: ${sign}
Import org.apache.commons.codec.binary.Base64;
The classes in the JMeter package are introduced, and if this code runs in the JMeter environment, no third-party jar packages need to be loaded
If you are running in eclipse or another environment, you need to replace the class with other Base64, please note!
JMeter interface test-using RSA encryption decryption algorithm