jmeter介面測試-使用rsa加密解密演算法

來源:互聯網
上載者:User

標籤:sign   print   array   off   new   nat   throws   div   except   

 加入Jmeter 進階技術qq群:572445436,參與Jmeter 技術交流

本篇介紹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_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDNPFO1OaKJbLOH7hVzjj8s+k+spSgG7D2imIpR1ukC3xqgEUYP/vYIiZHXnK04Ddk0ELYee5xDbFfTHSWOK6d2lqK0ydWtLFHCdKpBehM/YKa72zf5KaSJGGgag8EQw4o5ZBS/Ia9w2OxYZ1S94OeRXaA+Z4cy8rBui0hTW9Z0pwIDAQAB";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 = "idNum=633335199606143151&name=藺四十&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 block    e.printStackTrace();}    print(result);vars.put("sign",result);return result;

 

看運行效果

上述代碼,直接把加密結果放入變數sign中,在其他地方,如果需要調用加密結果,只需要 使用代碼:${sign}即可

import org.apache.commons.codec.binary.Base64;
引入了jmeter包中的類,如果本代碼在jmeter環境運行,不需要載入第三方jar包
如果在eclipse 或者其他環境中運行,需要其他base64的類替換,請注意!


jmeter介面測試-使用rsa加密解密演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.