通过http协议调用api接口进行加密与解密操作

需求:通过对方提供的接口,对其接口进行解析它的api的json数据,并进行判断是否是同一数据,代码直接实现。

1、pom文件的引入



    4.0.0

    org.example
    desd
    1.0-SNAPSHOT

    
    
        org.projectlombok
        lombok
        true
    
    
    
        com.alibaba
        fastjson
        1.2.60
    
    
    
        org.apache.commons
        commons-lang3
        3.8.1
    
    
    
        org.bouncycastle
        bcprov-jdk15on
        1.55
    
    
        org.projectlombok
        lombok
        RELEASE
        compile
    
    
        org.apache.directory.studio
        org.apache.commons.codec
        1.8
    
    
        org.springframework
        spring-web
        5.3.19
    
    
        org.testng
        testng
        RELEASE
        compile
    

    
    
        commons-httpclient
        commons-httpclient
        3.1
    
    
    
        org.apache.httpcomponents
        httpclient
        4.5.10
    

    
    
        com.alibaba
        fastjson
        1.2.32
    

    
        org.testng
        testng
        RELEASE
        compile
    
    
        org.testng
        testng
        RELEASE
        compile
    


    
        org.apache.commons
        commons-lang3
        3.9
    




(2)加密算法工具类

package com.gblfy.util;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

/**
 * sm4加密算法工具类
 * @explain sm4加密、解密与加密结果验证 可逆算法
 */
public class Sm4Util {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    private static final String ENCODING = "UTF-8";
    public static final String ALGORITHM_NAME = "SM4";
    // 加密算法/分组加密模式/分组填充方式
    // PKCS5Padding-以8个字节为一组进行分组加密
    // 定义分组加密模式使用:PKCS5Padding
    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
    // 128-32位16进制;256-64位16进制
    public static final int DEFAULT_KEY_SIZE = 128;

    /**
     * 生成ECB暗号
     * @explain ECB模式(电子密码本模式:Electronic codebook)
     * @param algorithmName 算法名称
     * @param mode 模式
     * @param key
     * @return
     * @throws Exception
     */
    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
        Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
        cipher.init(mode, sm4Key);
        return cipher;
    }

    /**
     * 自动生成密钥
     * @explain
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchProviderException
     */
    public static byte[] generateKey() throws Exception {
        return generateKey(DEFAULT_KEY_SIZE);
    }


    //加密******************************************
    /**
     * @explain 系统产生秘钥
     * @param keySize
     * @return
     * @throws Exception
     */
    public static byte[] generateKey(int keySize) throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
        kg.init(keySize, new SecureRandom());
        return kg.generateKey().getEncoded();
    }

    /**
     * sm4加密
     * @explain 加密模式:ECB 密文长度不固定,会随着被加密字符串长度的变化而变化
     * @param hexKey 16进制密钥(忽略大小写)
     * @param paramStr 待加密字符串
     * @return 返回16进制的加密字符串
     * @throws Exception
     */
    public static String encryptEcb(String hexKey, String paramStr) throws Exception {
        String cipherText = "";
        // 16进制字符串-->byte[]
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        // String-->byte[]
        byte[] srcData = paramStr.getBytes(ENCODING);
        // 加密后的数组
        byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
        // byte[]-->hexString
        cipherText = ByteUtils.toHexString(cipherArray);
        return cipherText;
    }

    /**
     * 加密模式之Ecb
     * @param key
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);//声称Ecb暗号,通过第二个参数判断加密还是解密
        return cipher.doFinal(data);
    }

    //解密****************************************
    /**
     * sm4解密
     * @explain 解密模式:采用ECB
     * @param hexKey 16进制密钥
     * @param cipherText 16进制的加密字符串(忽略大小写)
     * @return 解密后的字符串
     * @throws Exception
     */
    public static String decryptEcb(String hexKey, String cipherText) throws Exception {
        // 用于接收解密后的字符串
        String decryptStr = "";
        // hexString-->byte[]
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        // hexString-->byte[]
        byte[] cipherData = ByteUtils.fromHexString(cipherText);
        // 解密
        byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
        // byte[]-->String
        decryptStr = new String(srcData, ENCODING);
        return decryptStr;
    }

    /**
     * 解密
     * @explain
     * @param key
     * @param cipherText
     * @return
     * @throws Exception
     */
    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);//生成Ecb暗号,通过第二个参数判断加密还是解密
        return cipher.doFinal(cipherText);
    }

    /**
     * 校验加密前后的字符串是否为同一数据
     * @explain
     * @param hexKey 16进制密钥(忽略大小写)
     * @param cipherText 16进制加密后的字符串
     * @param paramStr 加密前的字符串
     * @return 是否为同一数据
     * @throws Exception
     */
    public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
        // 用于接收校验结果
        boolean flag = false;
        // hexString-->byte[]
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        // 将16进制字符串转换成数组
        byte[] cipherData = ByteUtils.fromHexString(cipherText);
        // 解密
        byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
        // 将原字符串转换成byte[]
        byte[] srcData = paramStr.getBytes(ENCODING);
        // 判断2个数组是否一致
        flag = Arrays.equals(decryptData, srcData);
        return flag;
    }

}

(3)httpApi工具类与测试

package com.gblfy.util;


import com.alibaba.fastjson.JSONObject;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;

/**
 * @author Mundo
 * @ClassName: HttpClientUtil
 * @Description: TODO
 */

public class HttpApiUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpApiUtil.class);

    /**
     *
     * @param url 请求路径
     * @param params 参数
     * @return
     */
    public static String doGet(String url, Map params) {

        // 返回结果
        String result = "";
        // 创建HttpClient对象
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = null;
        try {
            // 拼接参数,可以用URIBuilder,也可以直接拼接在?传值,拼在url后面,如下--httpGet = new
            // HttpGet(uri+"?id=123");
            URIBuilder uriBuilder = new URIBuilder(url);
            if (null != params && !params.isEmpty()) {
                for (Map.Entry entry : params.entrySet()) {
                    uriBuilder.addParameter(entry.getKey(), entry.getValue());
                    // 或者用
                    // 顺便说一下不同(setParameter会覆盖同名参数的值,addParameter则不会)
                    // uriBuilder.setParameter(entry.getKey(), entry.getValue());
                }
            }
            URI uri = uriBuilder.build();
            // 创建get请求
            httpGet = new HttpGet(uri);
            logger.info("访问路径:" + uri);
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 返回200,请求成功
                // 结果返回
                result = EntityUtils.toString(response.getEntity());
                logger.info("请求成功!,返回数据:" + result);
            } else {
                logger.info("请求失败!");
            }
        } catch (Exception e) {
            logger.info("请求失败!");
            logger.error(ExceptionUtils.getStackTrace(e));
        } finally {
            // 释放连接
            if (null != httpGet) {
                httpGet.releaseConnection();
            }
        }
        return result;
    }

    /**
     * @param url
     * @param params
     * @return
     * @Title: doPost
     * @Description: post请求
     * @author Mundo
     */
    public static String doPost(String url, Map params) {
        String result = "";
        // 创建httpclient对象
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        try { // 参数键值对
            if (null != params && !params.isEmpty()) {
                List pairs = new ArrayList();
                NameValuePair pair = null;
                for (String key : params.keySet()) {
                    pair = new BasicNameValuePair(key, params.get(key));
                    pairs.add(pair);
                }
                // 模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity(), "utf-8");
                logger.info("返回数据:>>>" + result);
            } else {
                logger.info("请求失败!,url:" + url);
            }
        } catch (Exception e) {
            logger.error("请求失败");
            logger.error(ExceptionUtils.getStackTrace(e));
            e.printStackTrace();
        } finally {
            if (null != httpPost) {
                // 释放连接
                httpPost.releaseConnection();
            }
        }
        return result;
    }

    /**
     * post发送json字符串
     * @param url
     * @param params
     * @return 返回数据
     * @Title: sendJsonStr
     */
    public static String sendJsonStr(String url, String params) {
        String result = "";

        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        try {
            httpPost.addHeader("Content-type", "application/json; charset=utf-8");
            httpPost.setHeader("Accept", "application/json");
            if (StringUtils.isNotBlank(params)) {
                httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(response.getEntity());
                logger.info("返回数据:" + result);
            } else {
                logger.info("请求失败");
            }
        } catch (IOException e) {
            logger.error("请求异常");
            logger.error(ExceptionUtils.getStackTrace(e));
        }
        return result;
    }

    /**
     * 发送http请求的obj报文(内置转json)
     *
     * @param url
     * @param obj
     * @return
     */
    public static String postJson(String url, Object obj) {
        HttpURLConnection conn = null;
        try {
            // 创建一个URL对象
            URL mURL = new URL(url);
            // 调用URL的openConnection()方法,获取HttpURLConnection对象
            conn = (HttpURLConnection) mURL.openConnection();
            conn.setRequestMethod("POST");// 设置请求方法为post
           /* conn.setReadTimeout(5000);// 设置读取超时为5秒
            conn.setConnectTimeout(10000);// 设置连接网络超时为10秒*/
            conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容
            // 设置文件类型:
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            // 设置接收类型否则返回415错误
            conn.setRequestProperty("accept", "application/json");
            int len = 0;
            // post请求的参数
            byte[] buf = new byte[10240];
            //
            String data = JSONObject.toJSONString(obj);

            // 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容
            OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据
            out.write(data.getBytes());
            out.flush();
            out.close();

            int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
            if (responseCode == 200) {
                InputStream is = conn.getInputStream();
                String state = getStringFromInputStream(is);
                return state;
            } else {
                System.out.print("访问失败" + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();// 关闭连接
            }
        }
        return null;
    }

    public static String getStringFromInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        // 模板代码 必须熟练
        byte[] buffer = new byte[1024];
        int len = -1;
        // 一定要写len=is.read(buffer)
        // 如果while((is.read(buffer))!=-1)则无法将数据写入buffer中
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码)
        os.close();
        return state;
    }

    /**
     * post方式请求服务器(http协议)
     *
     * @param url     请求地址
     * @param content 参数
     * @param charset 编码
     * @return
     * @throws
     * @throws
     * @throws IOException
     */
    public static byte[] post(String url, String content, String charset) throws IOException {

        URL console = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) console.openConnection();
        conn.setDoOutput(true);
        // 设置请求头
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        conn.connect();
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.write(content.getBytes(charset));
        // 刷新、关闭
        out.flush();
        out.close();
        InputStream is = conn.getInputStream();
        if (is != null) {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                outStream.write(buffer, 0, len);
            }
            is.close();
            return outStream.toByteArray();
        }
        return null;
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("id", UUID.randomUUID().toString());
        map.put("name", "gblfy");
//        String json = doGet("https://api.instagram.com/locations/search", map);
        String json  = doPost("https://oapi.dingtalk.com/topapi/org/union/trunk/get", map);
        System.out.println("post请求调用成功,返回数据是:" + json);
//        System.out.println("get请求调用成功,返回数据是:" + json);
         try {
            System.out.println("开始测试SM4加密解密====================");
//            String json ="https://api.instagram.com/locations/search";
             System.out.println("加密前:"+json);
            //自定义的32位16进制秘钥
            String key = "86C63180C2806ED1F47B859DE501215B";
            String cipher = Sm4Util.encryptEcb(key,json);//sm4加密
            System.out.println("加密后:"+cipher);
            System.out.println("校验:"+Sm4Util.verifyEcb(key,cipher,json));//校验加密前后是否为同一数据
            json = Sm4Util.decryptEcb(key,cipher);//解密
            System.out.println("解密后:"+json);
            System.out.println("结束===================");



        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

(4)运行输出,如图所示:

通过http协议调用api接口进行加密与解密操作_第1张图片

即输出数据:

post请求调用成功,返回数据是:{"errcode":88,"sub_code":"40000","sub_msg":"access_token is blank","errmsg":"ding talk error[subcode=40000,submsg=access_token is blank]","request_id":"15rrg0f4hakki"}
开始测试SM4加密解密====================
加密前:{"errcode":88,"sub_code":"40000","sub_msg":"access_token is blank","errmsg":"ding talk error[subcode=40000,submsg=access_token is blank]","request_id":"15rrg0f4hakki"}
加密后:1fc77cf74a5af18d46e8b5da00cf31683c2cd2ef6d2f0b0bcb99ee5a62e916b39a4674ae91abaaea095555a644059c4945c4264b0c751296842c24fc1913fe7a60a670c4933ff70c2fcbb720e69ccea9c893b7fb3d2733f880fbc67c414432ff342e23ba974e2a22667b2073511f91fe93068a1d5d0c07db710276d3923ca05459dcff8e81b2d9a71ec0d69052f66cfda37d354d9d850d8884ee4cd4c9d1cf20afc7c0060921ff02dbad5da0ecbbeee5
校验:true
解密后:{"errcode":88,"sub_code":"40000","sub_msg":"access_token is blank","errmsg":"ding talk error[subcode=40000,submsg=access_token is blank]","request_id":"15rrg0f4hakki"}
结束===================

你可能感兴趣的:(Java框架技术,http,postman,json)