使用工具:SecureFX 7.0,SecureCRT 7.0

<无详细内容>“` java import
java.io.UnsupportedEncodingException; import
java.security.InvalidKeyException; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import
java.security.PrivateKey; import java.security.PublicKey; import
java.security.SecureRandom;

Jmeter接口加密测试小结

     
最近,公司做接口测试,但是发送请求前需要对个别参数做加密处理。之前一直没做过这种,于是在网上查了很多资料,但是千篇一律,都是讲的把加密工具类打成jar包导入JMeter。因为我写的工具类引入了很多第三方jar包,所以引入到JMeter里总是报错(额,具体错误没记等之后复现再贴)。

1.将web项目打包

import javax.crypto.BadPaddingException; import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException; import
javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

(本人小白,第一次写 0.0)

我的解决方法:把工具类里所有涉及到的第三方包全都找出来,我涉及的有jce.jar,local_policy.jar,rt.jar,sunjce_provider.jar,US_export_policy.jar导入到测试计划中如图:

图片 1

测试计划

然后在HTTP请求下面添加前置处理器BeanShell PreProcessor,

图片 2

添加BeanShell PreProcessor

之后在BeanShell PreProcessor的Script中进行加密逻辑的代码编写,代码如下:


import java.io.IOException;

import java.security.InvalidKeyException;

import java.security.Key;

import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Security;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import java.util.HashMap;

import java.util.Map;

import java.util.logging.Logger;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import com.sun.org.apache.xml.internal.security.utils.Base64;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

//————————————————-RSA加密——————————————————-

String
mingKey=”MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvyhMMmQTsZY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AlmFJo52IvOH9tPFwF90wQKBgQDe8DpCW9QfS3Myc8YZTj5TcdOF/HRlCkFTDztJJwFfFmr7NAPQVkSk+B0CT683Pz4RLSdxkXZ9DuZ3Wik39vIu5RFrx5ajXc3woUZpb98LTbZEh1n6oLLRx3qVMty9XnQ1jgd6cxS1ch2Us2ZjLKFi8qddALBbLvPf2NOjboXXewKBgQDbgYRnzf2xxS3fCII6cJR+qqfJVcqigLbhcNGo+HYYaPusm9p5vEAyhwuhjD85ZRh0AiadaxlBSlSzxjNDkopFRbGcB7xI9l3k05HgHr0Drm22U2a8mZ26qbh4ECvt28MgLdZfU+4OO51UszsMsvBZafHNAWkq8B9z7+okh/qeuQKBgQCJoG+2y422DBP/j2057g2X4esdCe6o+Z2+Mub8j/HOy74bec7o7HjQBsORy7N1PbuJSwDQoWYuaeZow+YyQGbeAFey27HpBF3AMS+Qo5lkFwNwZsZrbI036BeKx61x0j+XWCjRtP2RzfLo+583ljPDK92aEnTMtb2j8O0mNXK2xQKBgQDBFTyS0u/F39xpw+JJ5Z25jgPpZj3Ik9BKniLOrz+yWSaIvs9/JciSqZfhkqxKoFLSONIexoaAmBHJ0R5m4hAevx9sUKGezJfrIO/AZUl+Y4C+UL6eR8Im4AQUGKWLpTQOqkNXt0w/2NCcIMxgb0ZcYIUB/6uyyWXhZbhgSfx6gQKBgByJ1+ecDO3huojtyX+HLEQ1yTHy+4on9l29uB6ZKSXjj7nvaoypqX428sBOVUGwydVtXAbbxMSJn4NbPo97QK6CJXbLP8i4Xl5bykzBbYLyLnbkUu0J5GWeghTjS1SIKZ6mc91eAh9N6K2+EopeKpcVWEe8d0XMdJR67X4Qd8UZ”;//私钥

String threeDesKey=”ThisMy3DesKey”;

String phonenum=”17600662928″;

String password=”111111″;

Cipher cipher=Cipher.getInstance(“RSA”);

//GetPublicKey

byte[] keyBytesPu;

keyBytesPu = (new BASE64Decoder()).decodeBuffer(mingKey);

X509EncodedKeySpec keySpecPu = new X509EncodedKeySpec(keyBytesPu);

KeyFactory keyFactoryPu = KeyFactory.getInstance(“RSA”);

PublicKey publicKey = keyFactoryPu.generatePublic(keySpecPu);

//GetPrivateKey

byte[] keyBytesPv;

keyBytesPv = (new BASE64Decoder()).decodeBuffer(miKey);

PKCS8EncodedKeySpec keySpecPv = new PKCS8EncodedKeySpec(keyBytesPv);

KeyFactory keyFactoryPv = KeyFactory.getInstance(“RSA”);

PrivateKey privateKey = keyFactoryPv.generatePrivate(keySpecPv);

//公钥加密

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

byte[] enBytes = cipher.doFinal(threeDesKey.getBytes());

String myKey= (new BASE64Encoder()).encode(enBytes);

//私钥解密

cipher.init(Cipher.DECRYPT_MODE, privateKey);

byte[] deBytes = cipher.doFinal((new
BASE64Decoder()).decodeBuffer(myKey));

String myKeyJ=new String(deBytes);

//————————————————-RSA加密——————————————————-

//————————————————-3DES加密——————————————————-

Cipher encryptCipher = null;

Cipher decryptCipher = null;

Security.addProvider(new com.sun.crypto.provider.SunJCE());

byte[] array=threeDesKey.getBytes();

// 创建一个空的8位字节数组

byte[] arrayTemp = new byte[8];

int length = array.length;

// 长度是否大于8

if (length > 8)  {

System.arraycopy(array, 0, arrayTemp, 0, 8);

}  else  {

System.arraycopy(array, 0, arrayTemp, 0, length);

}

// 生成密钥

Key key = new javax.crypto.spec.SecretKeySpec(arrayTemp, “DES”);

encryptCipher = Cipher.getInstance(“DES”);

encryptCipher.init(Cipher.ENCRYPT_MODE, key);

decryptCipher = Cipher.getInstance(“DES”);

decryptCipher.init(Cipher.DECRYPT_MODE, key);

//3DES加密  threeDesKey=”ThisMy3DesKey”;

String content =
“{\”telNum\”:\”17600662928\”,\”password\”:\”111111\”,”

* + “\”blackBox\”:\”63ABCFB379461777733FFB402B5A3CA5\”,”*

*  + “\”client\”:\”H5\”,\”version\”:\”3.9.0\”,”*

+ “\”decodeNum\”:\”d1ad62b5a1f9200c8a03c5df77d10910\”}”;

//String content=”111111″;

byte[] byteMi = null;

byte[] byteMing = null;

// 加密后的字符串

String strEncrypt = “”;

byteMing = content.getBytes(“UTF8”);

byteMi = encryptCipher.doFinal(byteMing);

strEncrypt = Base64.encode(byteMi);

//3DES解密

byte[] byteMingJ = null;

byte[] byteMiJ = null;

//解密后的字符串

String strMing = “”;

byteMiJ = Base64.decode(strEncrypt);

byteMingJ = decryptCipher.doFinal(byteMiJ);

strMing = new String(byteMingJ, “UTF8”);

log.info(“3DES秘钥:”+threeDesKey);

log.info(“公钥加密:”+myKey);

log.info(“私钥解密:”+myKeyJ);

log.info(“加密前的content:”+content);

log.info(“加密后的content:”+strEncrypt);

log.info(“解密后的content:”+strMing);

vars.put(“telNum”,myKey);

vars.put(“password”,strEncrypt);


我代码里面分别用到了RSA加密和3DES加密,vars.put()这个方法可以对请求中的指定参数进行传参,且HTTP请求中的参数要进行引用,如图:

图片 3

参数引用

处理完这些点击运行查看结果:

图片 4

参数被加密

从上图可以看出请求中的”telNum”与”password”参数已经经过加密了。

以上就是我的初步解决方法,第一次写果然语无伦次0.0 。

2.将jar包上传到tomcat服务器的webapps目录下

import com.sun.mail.util.BASE64DecoderStream; import
com.sun.mail.util.BASE64EncoderStream;

图片 5

public class util { /* \ 传入名文和公钥钥对数据进行RSA解密 *
<br>生成时间:2014年5月2日 下午2:38:13 * <br>返回值:String
* <br>@param src * <br>@param pubkey * <br>@return*
/ public static String rsaEncoding(String src,PublicKey pubkey){ try {
Cipher cip = Cipher.getInstance(“RSA”); cip.init(cip.ENCRYPT_MODE,
pubkey); byte[] by = cip.doFinal(src.getBytes()); return new
String(BASE64EncoderStream.encode(by));

3.上传好了之后启动tomcat服务器

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    }

}
/**
 * 传入RSA密文和私钥对数据进行解密
 * <br>生成时间:2014年5月2日  下午2:37:08
 * <br>返回值:String
 * <br>@param sec
 * <br>@param privkey
 * <br>@return
 */
public static String rsaDeEncoding(String sec,PrivateKey privkey){
    try {
        Cipher cip = Cipher.getInstance("RSA");
        cip.init(cip.DECRYPT_MODE, privkey);
        byte[] by = BASE64DecoderStream.decode(sec.getBytes());
        return new String(cip.doFinal(by));

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    }

}

/**
 * 传入字符串、密钥,并加密字符串(对称加密加密),支持:DES、AES、DESede(3DES)
 * <br>生成时间:2014年5月2日  下午12:05:44
 * <br>返回值:String 密文
 * <br>@param src
 * <br>@param key
 * <br>@param method(DES、AES、DESede)
 * <br>@return
 */
//对称加密加密
public static String doubKeyEncoding(String src,String keysrc,String method) {
    SecretKey key;
    try {
        //生成密钥
        KeyGenerator kg =  KeyGenerator.getInstance(method);
        //初始化此密钥生成器。
        kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
        key = kg.generateKey();

        //加密
        Cipher ciph =  Cipher.getInstance(method);
        ciph.init(Cipher.ENCRYPT_MODE, key);
        ciph.update(src.getBytes("utf-8"));
        //使用64进行编码,一避免出现丢数据情景
        byte[] by = BASE64EncoderStream.encode(ciph.doFinal());
        return new String(by);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
/**
 * 传入字符串、密钥、加密方式,并解密字符串(对称加密解密密),支持:DES、AES、DESede(3DES)
 * <br>生成时间:2014年5月2日  下午1:12:13
 * <br>返回值:String 密钥原文
 * <br>@param sec
 * <br>@param key
 * <br>@param method(DES、AES、DESede)
 * <br>@return
 */
public static String doubKeyDencoding(String sec,String keysrc,String method) {
    SecretKey key;
    try {
        //生成密钥
        KeyGenerator kg =  KeyGenerator.getInstance(method);
        //初始化此密钥生成器。
        kg.init(new SecureRandom(keysrc.getBytes("utf-8")));
        key = kg.generateKey();
        //加密
        Cipher ciph =  Cipher.getInstance(method);
        ciph.init(ciph.DECRYPT_MODE, key);
        //使用64进行解码,一避免出现丢数据情景
        byte[] by = BASE64DecoderStream.decode(sec.getBytes());
        ciph.update(by);
        return new String(ciph.doFinal());

    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException(e);
    } catch (InvalidKeyException e) {
        throw new RuntimeException(e);
    } catch (IllegalBlockSizeException e) {
        throw new RuntimeException(e);
    } catch (BadPaddingException e) {
        throw new RuntimeException(e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

/**
 * 单向信息加密(信息摘要),支持:md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512,
 * <br>生成时间:2014年5月2日  上午11:13:44
 * <br>返回值:String         加密后的密文
 * <br>@param src     传入加密字符串(明文)
 * <br>@param method  指定算法(md5、md2、SHA(SHA-1,SHA1)、SHA-256、SHA-384、SHA-512)
 * <br>@return
 */
public static String ecodingPasswd(String src,String method){

    try {
        //信息摘要算法
        MessageDigest md5 = MessageDigest.getInstance(method);
        md5.update(src.getBytes());
        byte[] encoding = md5.digest();
        //使用64进行编码,一避免出现丢数据情景
        return new String(BASE64EncoderStream.encode(encoding));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e+"加密失败!!");
    }

}

图片 6

} “`

大部分的Linux系统默认都安装了OpenJDK,如果不把自己装的jdk优先级调高,那么系统会默认启动OpenJDK,此时就会报异常

javax.crypto.BadPaddingException: Invalid pad value!
at sun.security.pkcs11.P11Cipher$PKCS5Padding.unpad(P11Cipher.java:106)[sunpkcs11.jar:1.7.0_45]
at sun.security.pkcs11.P11Cipher.implDoFinal(P11Cipher.java:802)[sunpkcs11.jar:1.7.0_45]
at sun.security.pkcs11.P11Cipher.engineDoFinal(P11Cipher.java:542)[sunpkcs11.jar:1.7.0_45]
at sun.security.pkcs11.P11Cipher.engineDoFinal(P11Cipher.java:525)[sunpkcs11.jar:1.7.0_45]
at javax.crypto.Cipher.doFinal(Cipher.java:1922)[:1.7.0_45]
at com.idcq.appserver.utils.AESUtil.aesDecryptByBytes(AESUtil.java:193)[AESUtil.class:]
at com.idcq.appserver.utils.AESUtil.aesDecrypt(AESUtil.java:211)[AESUtil.class:]

解决方法:

4.启动之后在浏览器上输入ip:8080/项目名

如果出现无法访问,请检查端口8080是否被防火墙拦截 ping ip 端口号 

如果连接失败,证明该端口号被拦截

解决方法: 

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注