• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
江湖侠客
帮助1

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

1、pom文件的引入

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.  
    <modelVersion>4.0.0</modelVersion>
  6.  
     
  7.  
    <groupId>org.example</groupId>
  8.  
    <artifactId>desd</artifactId>
  9.  
    <version>1.0-SNAPSHOT</version>
  10.  
    <dependencies>
  11.  
    <!-- lombok依赖 -->
  12.  
    <dependency>
  13.  
    <groupId>org.projectlombok</groupId>
  14.  
    <artifactId>lombok</artifactId>
  15.  
    <optional>true</optional>
  16.  
    </dependency>
  17.  
    <!-- alibaba的fastjson -->
  18.  
    <dependency>
  19.  
    <groupId>com.alibaba</groupId>
  20.  
    <artifactId>fastjson</artifactId>
  21.  
    <version>1.2.60</version>
  22.  
    </dependency>
  23.  
    <!-- 工具包 -->
  24.  
    <dependency>
  25.  
    <groupId>org.apache.commons</groupId>
  26.  
    <artifactId>commons-lang3</artifactId>
  27.  
    <version>3.8.1</version>
  28.  
    </dependency>
  29.  
    <!-- rsa加密工具-->
  30.  
    <dependency>
  31.  
    <groupId>org.bouncycastle</groupId>
  32.  
    <artifactId>bcprov-jdk15on</artifactId>
  33.  
    <version>1.55</version>
  34.  
    </dependency>
  35.  
    <dependency>
  36.  
    <groupId>org.projectlombok</groupId>
  37.  
    <artifactId>lombok</artifactId>
  38.  
    <version>RELEASE</version>
  39.  
    <scope>compile</scope>
  40.  
    </dependency>
  41.  
    <dependency>
  42.  
    <groupId>org.apache.directory.studio</groupId>
  43.  
    <artifactId>org.apache.commons.codec</artifactId>
  44.  
    <version>1.8</version>
  45.  
    </dependency>
  46.  
    <dependency>
  47.  
    <groupId>org.springframework</groupId>
  48.  
    <artifactId>spring-web</artifactId>
  49.  
    <version>5.3.19</version>
  50.  
    </dependency>
  51.  
    <dependency>
  52.  
    <groupId>org.testng</groupId>
  53.  
    <artifactId>testng</artifactId>
  54.  
    <version>RELEASE</version>
  55.  
    <scope>compile</scope>
  56.  
    </dependency>
  57.  
     
  58.  
    <!--HttpClient-->
  59.  
    <dependency>
  60.  
    <groupId>commons-httpclient</groupId>
  61.  
    <artifactId>commons-httpclient</artifactId>
  62.  
    <version>3.1</version>
  63.  
    </dependency>
  64.  
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  65.  
    <dependency>
  66.  
    <groupId>org.apache.httpcomponents</groupId>
  67.  
    <artifactId>httpclient</artifactId>
  68.  
    <version>4.5.10</version>
  69.  
    </dependency>
  70.  
     
  71.  
    <!--fastjson-->
  72.  
    <dependency>
  73.  
    <groupId>com.alibaba</groupId>
  74.  
    <artifactId>fastjson</artifactId>
  75.  
    <version>1.2.32</version>
  76.  
    </dependency>
  77.  
     
  78.  
    <dependency>
  79.  
    <groupId>org.testng</groupId>
  80.  
    <artifactId>testng</artifactId>
  81.  
    <version>RELEASE</version>
  82.  
    <scope>compile</scope>
  83.  
    </dependency>
  84.  
    <dependency>
  85.  
    <groupId>org.testng</groupId>
  86.  
    <artifactId>testng</artifactId>
  87.  
    <version>RELEASE</version>
  88.  
    <scope>compile</scope>
  89.  
    </dependency>
  90.  
     
  91.  
     
  92.  
    <dependency>
  93.  
    <groupId>org.apache.commons</groupId>
  94.  
    <artifactId>commons-lang3</artifactId>
  95.  
    <version>3.9</version>
  96.  
    </dependency>
  97.  
     
  98.  
     
  99.  
    </dependencies>
  100.  
     
  101.  
    </project>
学新通

(2)加密算法工具类

  1.  
    package com.gblfy.util;
  2.  
    import java.security.Key;
  3.  
    import java.security.NoSuchAlgorithmException;
  4.  
    import java.security.NoSuchProviderException;
  5.  
    import java.security.SecureRandom;
  6.  
    import java.security.Security;
  7.  
    import java.util.Arrays;
  8.  
    import javax.crypto.Cipher;
  9.  
    import javax.crypto.KeyGenerator;
  10.  
    import javax.crypto.spec.SecretKeySpec;
  11.  
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
  12.  
    import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
  13.  
     
  14.  
    /**
  15.  
    * sm4加密算法工具类
  16.  
    * @explain sm4加密、解密与加密结果验证 可逆算法
  17.  
    */
  18.  
    public class Sm4Util {
  19.  
    static {
  20.  
    Security.addProvider(new BouncyCastleProvider());
  21.  
    }
  22.  
    private static final String ENCODING = "UTF-8";
  23.  
    public static final String ALGORITHM_NAME = "SM4";
  24.  
    // 加密算法/分组加密模式/分组填充方式
  25.  
    // PKCS5Padding-以8个字节为一组进行分组加密
  26.  
    // 定义分组加密模式使用:PKCS5Padding
  27.  
    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
  28.  
    // 128-32位16进制;256-64位16进制
  29.  
    public static final int DEFAULT_KEY_SIZE = 128;
  30.  
     
  31.  
    /**
  32.  
    * 生成ECB暗号
  33.  
    * @explain ECB模式(电子密码本模式:Electronic codebook)
  34.  
    * @param algorithmName 算法名称
  35.  
    * @param mode 模式
  36.  
    * @param key
  37.  
    * @return
  38.  
    * @throws Exception
  39.  
    */
  40.  
    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
  41.  
    Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
  42.  
    Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
  43.  
    cipher.init(mode, sm4Key);
  44.  
    return cipher;
  45.  
    }
  46.  
     
  47.  
    /**
  48.  
    * 自动生成密钥
  49.  
    * @explain
  50.  
    * @return
  51.  
    * @throws NoSuchAlgorithmException
  52.  
    * @throws NoSuchProviderException
  53.  
    */
  54.  
    public static byte[] generateKey() throws Exception {
  55.  
    return generateKey(DEFAULT_KEY_SIZE);
  56.  
    }
  57.  
     
  58.  
     
  59.  
    //加密******************************************
  60.  
    /**
  61.  
    * @explain 系统产生秘钥
  62.  
    * @param keySize
  63.  
    * @return
  64.  
    * @throws Exception
  65.  
    */
  66.  
    public static byte[] generateKey(int keySize) throws Exception {
  67.  
    KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
  68.  
    kg.init(keySize, new SecureRandom());
  69.  
    return kg.generateKey().getEncoded();
  70.  
    }
  71.  
     
  72.  
    /**
  73.  
    * sm4加密
  74.  
    * @explain 加密模式:ECB 密文长度不固定,会随着被加密字符串长度的变化而变化
  75.  
    * @param hexKey 16进制密钥(忽略大小写)
  76.  
    * @param paramStr 待加密字符串
  77.  
    * @return 返回16进制的加密字符串
  78.  
    * @throws Exception
  79.  
    */
  80.  
    public static String encryptEcb(String hexKey, String paramStr) throws Exception {
  81.  
    String cipherText = "";
  82.  
    // 16进制字符串-->byte[]
  83.  
    byte[] keyData = ByteUtils.fromHexString(hexKey);
  84.  
    // String-->byte[]
  85.  
    byte[] srcData = paramStr.getBytes(ENCODING);
  86.  
    // 加密后的数组
  87.  
    byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
  88.  
    // byte[]-->hexString
  89.  
    cipherText = ByteUtils.toHexString(cipherArray);
  90.  
    return cipherText;
  91.  
    }
  92.  
     
  93.  
    /**
  94.  
    * 加密模式之Ecb
  95.  
    * @param key
  96.  
    * @param data
  97.  
    * @return
  98.  
    * @throws Exception
  99.  
    */
  100.  
    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
  101.  
    Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);//声称Ecb暗号,通过第二个参数判断加密还是解密
  102.  
    return cipher.doFinal(data);
  103.  
    }
  104.  
     
  105.  
    //解密****************************************
  106.  
    /**
  107.  
    * sm4解密
  108.  
    * @explain 解密模式:采用ECB
  109.  
    * @param hexKey 16进制密钥
  110.  
    * @param cipherText 16进制的加密字符串(忽略大小写)
  111.  
    * @return 解密后的字符串
  112.  
    * @throws Exception
  113.  
    */
  114.  
    public static String decryptEcb(String hexKey, String cipherText) throws Exception {
  115.  
    // 用于接收解密后的字符串
  116.  
    String decryptStr = "";
  117.  
    // hexString-->byte[]
  118.  
    byte[] keyData = ByteUtils.fromHexString(hexKey);
  119.  
    // hexString-->byte[]
  120.  
    byte[] cipherData = ByteUtils.fromHexString(cipherText);
  121.  
    // 解密
  122.  
    byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
  123.  
    // byte[]-->String
  124.  
    decryptStr = new String(srcData, ENCODING);
  125.  
    return decryptStr;
  126.  
    }
  127.  
     
  128.  
    /**
  129.  
    * 解密
  130.  
    * @explain
  131.  
    * @param key
  132.  
    * @param cipherText
  133.  
    * @return
  134.  
    * @throws Exception
  135.  
    */
  136.  
    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
  137.  
    Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);//生成Ecb暗号,通过第二个参数判断加密还是解密
  138.  
    return cipher.doFinal(cipherText);
  139.  
    }
  140.  
     
  141.  
    /**
  142.  
    * 校验加密前后的字符串是否为同一数据
  143.  
    * @explain
  144.  
    * @param hexKey 16进制密钥(忽略大小写)
  145.  
    * @param cipherText 16进制加密后的字符串
  146.  
    * @param paramStr 加密前的字符串
  147.  
    * @return 是否为同一数据
  148.  
    * @throws Exception
  149.  
    */
  150.  
    public static boolean verifyEcb(String hexKey, String cipherText, String paramStr) throws Exception {
  151.  
    // 用于接收校验结果
  152.  
    boolean flag = false;
  153.  
    // hexString-->byte[]
  154.  
    byte[] keyData = ByteUtils.fromHexString(hexKey);
  155.  
    // 将16进制字符串转换成数组
  156.  
    byte[] cipherData = ByteUtils.fromHexString(cipherText);
  157.  
    // 解密
  158.  
    byte[] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
  159.  
    // 将原字符串转换成byte[]
  160.  
    byte[] srcData = paramStr.getBytes(ENCODING);
  161.  
    // 判断2个数组是否一致
  162.  
    flag = Arrays.equals(decryptData, srcData);
  163.  
    return flag;
  164.  
    }
  165.  
     
  166.  
    }
学新通

(3)httpApi工具类与测试

  1.  
    package com.gblfy.util;
  2.  
     
  3.  
     
  4.  
    import com.alibaba.fastjson.JSONObject;
  5.  
    import lombok.val;
  6.  
    import org.apache.commons.lang3.StringUtils;
  7.  
    import org.apache.commons.lang3.exception.ExceptionUtils;
  8.  
    import org.apache.http.HttpResponse;
  9.  
    import org.apache.http.HttpStatus;
  10.  
    import org.apache.http.NameValuePair;
  11.  
    import org.apache.http.client.HttpClient;
  12.  
    import org.apache.http.client.entity.UrlEncodedFormEntity;
  13.  
    import org.apache.http.client.methods.HttpGet;
  14.  
    import org.apache.http.client.methods.HttpPost;
  15.  
    import org.apache.http.client.utils.URIBuilder;
  16.  
    import org.apache.http.entity.StringEntity;
  17.  
    import org.apache.http.impl.client.HttpClientBuilder;
  18.  
    import org.apache.http.message.BasicNameValuePair;
  19.  
    import org.apache.http.util.EntityUtils;
  20.  
    import org.slf4j.Logger;
  21.  
    import org.slf4j.LoggerFactory;
  22.  
    import java.io.*;
  23.  
    import java.net.HttpURLConnection;
  24.  
    import java.net.URI;
  25.  
    import java.net.URL;
  26.  
    import java.nio.charset.Charset;
  27.  
    import java.util.*;
  28.  
     
  29.  
    /**
  30.  
    * @author Mundo
  31.  
    * @ClassName: HttpClientUtil
  32.  
    * @Description: TODO
  33.  
    */
  34.  
     
  35.  
    public class HttpApiUtil {
  36.  
    private static final Logger logger = LoggerFactory.getLogger(HttpApiUtil.class);
  37.  
     
  38.  
    /**
  39.  
    *
  40.  
    * @param url 请求路径
  41.  
    * @param params 参数
  42.  
    * @return
  43.  
    */
  44.  
    public static String doGet(String url, Map<String, String> params) {
  45.  
     
  46.  
    // 返回结果
  47.  
    String result = "";
  48.  
    // 创建HttpClient对象
  49.  
    HttpClient httpClient = HttpClientBuilder.create().build();
  50.  
    HttpGet httpGet = null;
  51.  
    try {
  52.  
    // 拼接参数,可以用URIBuilder,也可以直接拼接在?传值,拼在url后面,如下--httpGet = new
  53.  
    // HttpGet(uri "?id=123");
  54.  
    URIBuilder uriBuilder = new URIBuilder(url);
  55.  
    if (null != params && !params.isEmpty()) {
  56.  
    for (Map.Entry<String, String> entry : params.entrySet()) {
  57.  
    uriBuilder.addParameter(entry.getKey(), entry.getValue());
  58.  
    // 或者用
  59.  
    // 顺便说一下不同(setParameter会覆盖同名参数的值,addParameter则不会)
  60.  
    // uriBuilder.setParameter(entry.getKey(), entry.getValue());
  61.  
    }
  62.  
    }
  63.  
    URI uri = uriBuilder.build();
  64.  
    // 创建get请求
  65.  
    httpGet = new HttpGet(uri);
  66.  
    logger.info("访问路径:" uri);
  67.  
    HttpResponse response = httpClient.execute(httpGet);
  68.  
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 返回200,请求成功
  69.  
    // 结果返回
  70.  
    result = EntityUtils.toString(response.getEntity());
  71.  
    logger.info("请求成功!,返回数据:" result);
  72.  
    } else {
  73.  
    logger.info("请求失败!");
  74.  
    }
  75.  
    } catch (Exception e) {
  76.  
    logger.info("请求失败!");
  77.  
    logger.error(ExceptionUtils.getStackTrace(e));
  78.  
    } finally {
  79.  
    // 释放连接
  80.  
    if (null != httpGet) {
  81.  
    httpGet.releaseConnection();
  82.  
    }
  83.  
    }
  84.  
    return result;
  85.  
    }
  86.  
     
  87.  
    /**
  88.  
    * @param url
  89.  
    * @param params
  90.  
    * @return
  91.  
    * @Title: doPost
  92.  
    * @Description: post请求
  93.  
    * @author Mundo
  94.  
    */
  95.  
    public static String doPost(String url, Map<String, String> params) {
  96.  
    String result = "";
  97.  
    // 创建httpclient对象
  98.  
    HttpClient httpClient = HttpClientBuilder.create().build();
  99.  
    HttpPost httpPost = new HttpPost(url);
  100.  
    try { // 参数键值对
  101.  
    if (null != params && !params.isEmpty()) {
  102.  
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
  103.  
    NameValuePair pair = null;
  104.  
    for (String key : params.keySet()) {
  105.  
    pair = new BasicNameValuePair(key, params.get(key));
  106.  
    pairs.add(pair);
  107.  
    }
  108.  
    // 模拟表单
  109.  
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
  110.  
    httpPost.setEntity(entity);
  111.  
    }
  112.  
    HttpResponse response = httpClient.execute(httpPost);
  113.  
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  114.  
    result = EntityUtils.toString(response.getEntity(), "utf-8");
  115.  
    logger.info("返回数据:>>>" result);
  116.  
    } else {
  117.  
    logger.info("请求失败!,url:" url);
  118.  
    }
  119.  
    } catch (Exception e) {
  120.  
    logger.error("请求失败");
  121.  
    logger.error(ExceptionUtils.getStackTrace(e));
  122.  
    e.printStackTrace();
  123.  
    } finally {
  124.  
    if (null != httpPost) {
  125.  
    // 释放连接
  126.  
    httpPost.releaseConnection();
  127.  
    }
  128.  
    }
  129.  
    return result;
  130.  
    }
  131.  
     
  132.  
    /**
  133.  
    * post发送json字符串
  134.  
    * @param url
  135.  
    * @param params
  136.  
    * @return 返回数据
  137.  
    * @Title: sendJsonStr
  138.  
    */
  139.  
    public static String sendJsonStr(String url, String params) {
  140.  
    String result = "";
  141.  
     
  142.  
    HttpClient httpClient = HttpClientBuilder.create().build();
  143.  
    HttpPost httpPost = new HttpPost(url);
  144.  
    try {
  145.  
    httpPost.addHeader("Content-type", "application/json; charset=utf-8");
  146.  
    httpPost.setHeader("Accept", "application/json");
  147.  
    if (StringUtils.isNotBlank(params)) {
  148.  
    httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));
  149.  
    }
  150.  
    HttpResponse response = httpClient.execute(httpPost);
  151.  
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  152.  
    result = EntityUtils.toString(response.getEntity());
  153.  
    logger.info("返回数据:" result);
  154.  
    } else {
  155.  
    logger.info("请求失败");
  156.  
    }
  157.  
    } catch (IOException e) {
  158.  
    logger.error("请求异常");
  159.  
    logger.error(ExceptionUtils.getStackTrace(e));
  160.  
    }
  161.  
    return result;
  162.  
    }
  163.  
     
  164.  
    /**
  165.  
    * 发送http请求的obj报文(内置转json)
  166.  
    *
  167.  
    * @param url
  168.  
    * @param obj
  169.  
    * @return
  170.  
    */
  171.  
    public static String postJson(String url, Object obj) {
  172.  
    HttpURLConnection conn = null;
  173.  
    try {
  174.  
    // 创建一个URL对象
  175.  
    URL mURL = new URL(url);
  176.  
    // 调用URL的openConnection()方法,获取HttpURLConnection对象
  177.  
    conn = (HttpURLConnection) mURL.openConnection();
  178.  
    conn.setRequestMethod("POST");// 设置请求方法为post
  179.  
    /* conn.setReadTimeout(5000);// 设置读取超时为5秒
  180.  
    conn.setConnectTimeout(10000);// 设置连接网络超时为10秒*/
  181.  
    conn.setDoOutput(true);// 设置此方法,允许向服务器输出内容
  182.  
    // 设置文件类型:
  183.  
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
  184.  
    // 设置接收类型否则返回415错误
  185.  
    conn.setRequestProperty("accept", "application/json");
  186.  
    int len = 0;
  187.  
    // post请求的参数
  188.  
    byte[] buf = new byte[10240];
  189.  
    //
  190.  
    String data = JSONObject.toJSONString(obj);
  191.  
     
  192.  
    // 获得一个输出流,向服务器写数据,默认情况下,系统不允许向服务器输出内容
  193.  
    OutputStream out = conn.getOutputStream();// 获得一个输出流,向服务器写数据
  194.  
    out.write(data.getBytes());
  195.  
    out.flush();
  196.  
    out.close();
  197.  
     
  198.  
    int responseCode = conn.getResponseCode();// 调用此方法就不必再使用conn.connect()方法
  199.  
    if (responseCode == 200) {
  200.  
    InputStream is = conn.getInputStream();
  201.  
    String state = getStringFromInputStream(is);
  202.  
    return state;
  203.  
    } else {
  204.  
    System.out.print("访问失败" responseCode);
  205.  
    }
  206.  
    } catch (Exception e) {
  207.  
    e.printStackTrace();
  208.  
    } finally {
  209.  
    if (conn != null) {
  210.  
    conn.disconnect();// 关闭连接
  211.  
    }
  212.  
    }
  213.  
    return null;
  214.  
    }
  215.  
     
  216.  
    public static String getStringFromInputStream(InputStream is) throws IOException {
  217.  
    ByteArrayOutputStream os = new ByteArrayOutputStream();
  218.  
    // 模板代码 必须熟练
  219.  
    byte[] buffer = new byte[1024];
  220.  
    int len = -1;
  221.  
    // 一定要写len=is.read(buffer)
  222.  
    // 如果while((is.read(buffer))!=-1)则无法将数据写入buffer中
  223.  
    while ((len = is.read(buffer)) != -1) {
  224.  
    os.write(buffer, 0, len);
  225.  
    }
  226.  
    is.close();
  227.  
    String state = os.toString();// 把流中的数据转换成字符串,采用的编码是utf-8(模拟器默认编码)
  228.  
    os.close();
  229.  
    return state;
  230.  
    }
  231.  
     
  232.  
    /**
  233.  
    * post方式请求服务器(http协议)
  234.  
    *
  235.  
    * @param url 请求地址
  236.  
    * @param content 参数
  237.  
    * @param charset 编码
  238.  
    * @return
  239.  
    * @throws
  240.  
    * @throws
  241.  
    * @throws IOException
  242.  
    */
  243.  
    public static byte[] post(String url, String content, String charset) throws IOException {
  244.  
     
  245.  
    URL console = new URL(url);
  246.  
    HttpURLConnection conn = (HttpURLConnection) console.openConnection();
  247.  
    conn.setDoOutput(true);
  248.  
    // 设置请求头
  249.  
    conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
  250.  
    conn.connect();
  251.  
    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
  252.  
    out.write(content.getBytes(charset));
  253.  
    // 刷新、关闭
  254.  
    out.flush();
  255.  
    out.close();
  256.  
    InputStream is = conn.getInputStream();
  257.  
    if (is != null) {
  258.  
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  259.  
    byte[] buffer = new byte[1024];
  260.  
    int len = 0;
  261.  
    while ((len = is.read(buffer)) != -1) {
  262.  
    outStream.write(buffer, 0, len);
  263.  
    }
  264.  
    is.close();
  265.  
    return outStream.toByteArray();
  266.  
    }
  267.  
    return null;
  268.  
    }
  269.  
     
  270.  
    public static void main(String[] args) {
  271.  
    Map<String, String> map = new HashMap<String, String>();
  272.  
    map.put("id", UUID.randomUUID().toString());
  273.  
    map.put("name", "gblfy");
  274.  
    // String json = doGet("https://api.instagram.com/locations/search", map);
  275.  
    String json = doPost("https://oapi.dingtalk.com/topapi/org/union/trunk/get", map);
  276.  
    System.out.println("post请求调用成功,返回数据是:" json);
  277.  
    // System.out.println("get请求调用成功,返回数据是:" json);
  278.  
    try {
  279.  
    System.out.println("开始测试SM4加密解密====================");
  280.  
    // String json ="https://api.instagram.com/locations/search";
  281.  
    System.out.println("加密前:" json);
  282.  
    //自定义的32位16进制秘钥
  283.  
    String key = "86C63180C2806ED1F47B859DE501215B";
  284.  
    String cipher = Sm4Util.encryptEcb(key,json);//sm4加密
  285.  
    System.out.println("加密后:" cipher);
  286.  
    System.out.println("校验:" Sm4Util.verifyEcb(key,cipher,json));//校验加密前后是否为同一数据
  287.  
    json = Sm4Util.decryptEcb(key,cipher);//解密
  288.  
    System.out.println("解密后:" json);
  289.  
    System.out.println("结束===================");
  290.  
     
  291.  
     
  292.  
     
  293.  
    } catch (Exception e) {
  294.  
    e.printStackTrace();
  295.  
    }
  296.  
     
  297.  
    }
  298.  
    }
学新通

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

学新通

即输出数据:

  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"}
  2.  
    开始测试SM4加密解密====================
  3.  
    加密前:{"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"}
  4.  
    加密后:1fc77cf74a5af18d46e8b5da00cf31683c2cd2ef6d2f0b0bcb99ee5a62e916b39a4674ae91abaaea095555a644059c4945c4264b0c751296842c24fc1913fe7a60a670c4933ff70c2fcbb720e69ccea9c893b7fb3d2733f880fbc67c414432ff342e23ba974e2a22667b2073511f91fe93068a1d5d0c07db710276d3923ca05459dcff8e81b2d9a71ec0d69052f66cfda37d354d9d850d8884ee4cd4c9d1cf20afc7c0060921ff02dbad5da0ecbbeee5
  5.  
    校验:true
  6.  
    解密后:{"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"}
  7.  
    结束===================

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfjjbg
系列文章
更多 icon
同类精品
更多 icon
继续加载