https 原理和应用
2020.01.14
codewindy
 热度
℃
Principles and Applications of https
一. 背景
- 理解http 和https 的区别
- 理解http 1.0 和http 1.1 和http2.0 的区别
- 在对外提供接口的情况下,如何设计和保证数据的安全可靠性
二. 原理
- www.ichenpeng.net
三. 应用
package com.codewindy.mongodb.utils;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.RSA; import cn.hutool.crypto.asymmetric.Sign; import cn.hutool.crypto.asymmetric.SignAlgorithm; import cn.hutool.http.HttpRequest; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType;
import java.util.HashMap;
public class TestAESByHutool { public static void main(String[] args) {
RSA rsa = new RSA(); String privateKeyBase64 = rsa.getPrivateKeyBase64(); String publicKeyBase64 = rsa.getPublicKeyBase64(); System.out.println("keyPair.getPrivate() 私钥= " + privateKeyBase64); System.out.println("keyPair.getPublic() 公钥= " + publicKeyBase64);
String content = HttpRequest .get("https://api.myip.com/") .timeout(20000) .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) .execute() .body(); String aesKey = RandomStringUtils.randomNumeric(16); System.out.println("每次生成AES 16 位随机密码aesKey = " + aesKey);
String encryptAesKey = rsa.encryptBase64(aesKey, KeyType.PrivateKey); System.out.println("使用公钥加密 aes对称密码 后的encryptAesKey = " + encryptAesKey); String encryptContent = SecureUtil.aes(aesKey.getBytes()).encryptBase64(content); System.out.println("AES 加密后的内容 encryptContent = " + encryptContent);
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA); byte[] signedContent = sign.sign(content.getBytes()); System.err.println("对原始数据进行签名signedContent = " + signedContent);
String urlPath = "/admin/queryUserInfo"; String httpMethod = "POST"; long clientTimeStamp = DateUtil.currentSeconds(); HashMap<String, Object> requestParams = MapUtil.newHashMap(); requestParams.put("urlPath", urlPath); requestParams.put("httpMethod", httpMethod);
requestParams.put("clientTimeStamp", clientTimeStamp + ""); String sign2String = MapUtil.sortJoin(MapUtil.sort(requestParams), "&", "=", true);
String calculateSign = SecureUtil.hmacSha1(aesKey.getBytes()).digestHex(sign2String);
System.out.println("计算出来的sign和request 请求里面的参数clientSign 进行比较 = " + calculateSign); System.out.println("解密-----------------------------------------------------");
String decryptAesKey = rsa.decryptStr(encryptAesKey, KeyType.PublicKey); System.out.println("使用私钥解密后的decryptAesKey = " + decryptAesKey);
String decryptContent = SecureUtil.aes(decryptAesKey.getBytes()).decryptStr(encryptContent); System.out.println("解密得到明文的 aes 对称密码 解密加密的content数据 = " + decryptContent);
boolean verify = sign.verify(content.getBytes(), signedContent); System.err.println("验签状态verify = " + verify); }
}
|
四. 参考