性能测试:登录接口密码使用rsa加密
1、出于安全考虑,有的网站在登陆的时候为了防止用户在登录时账户密码泄漏,会使用各种加密,给登录的账户密码加密,比如:明文保存,对称加密算法,MD5、SHA1等单向HASH算法,RSA算法,加密FBPDF2算法,bcrypt,scrypt等。所以我们在做注册或者登录的性能测试时需要借助脚本来帮助自己解密成功调用接口。
我这次的登录密码使用的RSA加密。注册或者登录的时候我们需要使用公钥给密码加密后,传给接口。这时我们需要借助脚本代码将我们的密码用公钥加密(我这里用的是java的脚本)。
RSA算法加密原理图:
详细的操作过程:
1、将ip地址和端口参数化
2、给密码使用rsa算法加密,添加一个取样器:BeanShell取样器,粘贴脚本代码,代码如下
脚本:
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="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 = "登录密码"; 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;
3、在登录接口直接调用加密后的密码:{$sign}
4、查看结果树: