菜单

java中常用的多寡加密算法

2019年3月15日 - 金沙编程资讯

利用工具:SecureFX 7.0,SecureCHighlanderT 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里总是报错(额,具体错误没记等之后复现再贴)。

声称:亲测,是足以互通的。
宣示:本文章摘要录来源http://www.jianshu.com/p/581380445eb4
作者totzcc
iOS代码地址https://gist.github.com/lvjian700/204c23226fdffd6a505d

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;

(本身小白,第3回写 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=”MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvyhMMmQTsZYpQX1iLM1QPWT+hD0Y/Z1wNvvxvavDLBQN9vASkjRnR8S4rlQBHAM/WbC+KC14KJcHTydYjmAIwREux20WxxbrdZZXey/BTv9MRHs2rhckYpGRaVGfpsFVDTFq2468i50xqcraYxcpPxpeohZMxKeixzMbnp/cf4UJiQJ0w0ARQyLJhgenA0hOJ3iGm8JRKxtxmZ6nA6oStV9VrtcUAm2N5F/Oiu5eMQIeWpuYkfMhplqU+/fr7Zx6hBAR/VVvsiGD/PMCYk4nKVKZ1hCCZCz+zBusonRobx+93wu0V7j11xJiC1gTUhRSa60Ox4OlQGXB0A3zxNSh4wIDAQAB”;//公钥

String
miKey=”MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC/KEwyZBOxlilBfWIszVA9ZP6EPRj9nXA2+/G9q8MsFA328BKSNGdHxLiuVAEcAz9ZsL4oLXgolwdPJ1iOYAjBES7HbRbHFut1lld7L8FO/0xEezauFyRikZFpUZ+mwVUNMWrbjryLnTGpytpjFyk/Gl6iFkzEp6LHMxuen9x/hQmJAnTDQBFDIsmGB6cDSE4neIabwlErG3GZnqcDqhK1X1Wu1xQCbY3kX86K7l4xAh5am5iR8yGmWpT79+vtnHqEEBH9VW+yIYP88wJiTicpUpnWEIJkLP7MG6yidGhvH73fC7RXuPXXEmILWBNSFFJrrQ7Hg6VAZcHQDfPE1KHjAgMBAAECggEAF0CrpCWQT7XYZuL9oj2HWTCD1UopVBBmqgmTqmLOZvo5iKRDXg2J0q0XWf1V9TZf6mUZfIGrcCSO+w3qM8dyySlx60hV0Pn0wmT7VzzD4vwjJuFmEV83SdYzPgBzzzENS4GLYhHG1aTVebX3Qr56gfaPNGBKDlHl9x08ats/UzOB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;

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();

// 成立二个空的5人字节数组

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);


作者代码里面分别用到了奥迪Q5SA加密和3DES加密,vars.put()那么些方法能够对请求中的钦定参数举行传参,且HTTP请求中的参数要拓展引用,如图:

图片 3

参数引用

拍卖完那几个点击运转查看结果:

图片 4

参数被加密

从上海教室能够看到请求中的”telNum”与”password”参数已经通过加密了。

上述便是本身的开首消除方法,第三次写果然语无伦次0.0 。

  1. 应用openssl生成密钥队,谷歌(谷歌)时而。注意:Java要求动用pkcs8 private
    key解密。
    2.pod ‘Base64nl’, ‘~> 1.2’
    3.iOS加密
    RSAEncryptor *rsa = [[RSAEncryptor alloc] init];
    NSLog(@”encryptor using rsa”);
    NSString *publicKeyPath = [[NSBundle mainBundle]
    pathForResource:@”public_key” ofType:@”der”];
    NSLog(@”public key: %@”, publicKeyPath);
    [rsa loadPublicKeyFromFile:publicKeyPath];
    NSString *securityText = @”hello ~”;
    NSString *encryptedString = [rsa
    rsaEncryptString:securityText];
    NSLog(@”encrypted data: %@”, encryptedString);
    4.Java解密
    大旨步骤:
    加载pkcs8 private key:
    读取private key文件
    去掉private key头尾的”—–BEGIN PRIVATE KEY—–“和”—–BEGIN
    PRIVATE KEY—–“
    剔除private key中的换行
    对处理后的多少举行Base64解码
    应用解码后的数据变化private key.
    解密数据:
    对数据开始展览Base64解码
    使用RSA decrypt数据.

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

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

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import static java.lang.String.format;
public class Encryptor {
public static void main(String[] args) throws IOException,
NoSuchAlgorithmException, InvalidKeySpecException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException,
IllegalBlockSizeException {
PrivateKey privateKey = readPrivateKey();
String message =
“AFppaFPTbmboMZD55cjCfrVaWUW7+hZkaq16Od+6fP0lwz/yC+Rshb/8cf5BpBlUao2EunchnzeKxzpiPqtCcCITKvk6HcFKZS0sN9wOhlQFYT+I4f/CZITwBVAJaldZ7mkyOiuvM+raXMwrS+7MLKgYXkd5cFPxEsTxpMSa5Nk=”;
System.out.println(format(“- decrypt rsa encrypted base64 message: %s”,
message));
// hello ~, encrypted and encoded with Base64:
byte[] data = encryptedData(message);
String text = decrypt(privateKey, data);
System.out.println(text);
}
private static String decrypt(PrivateKey privateKey, byte[] data)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedData = cipher.doFinal(data);
return new String(decryptedData);
}
private static byte[] encryptedData(String base64Text) {
return
Base64.getDecoder().decode(base64Text.getBytes(Charset.forName(“UTF-8”)));
}
private static PrivateKey readPrivateKey() throws IOException,
NoSuchAlgorithmException, InvalidKeySpecException {
byte[] privateKeyData = Files.readAllBytes(
Paths.get(“/Users/twer/macspace/ios_workshop/Security/SecurityLogin/tools/pkcs8_private_key.pem”));
byte[] decodedKeyData = Base64.getDecoder()
.decode(new String(privateKeyData)
.replaceAll(“—–\w+ PRIVATE KEY—–“, “”)
.replace(“\n”, “”)
.getBytes());
return KeyFactory.getInstance(“RSA”).generatePrivate(new
PKCS8EncodedKeySpec(decodedKeyData));
}
}

图片 5

public class util { /* \ 传入名文和公钥钥对数码进行RubiconSA解密 *
<br>生成时间:二〇一四年七月26日 早上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));

相关文章

发表评论

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

网站地图xml地图