JkToken.java 3.19 KB
package com.bsth.serviec_jk;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;

public class JkToken {
    //加密的token码
    public static String jkdata() {
        try {
           String token ="abfed98867c64fde99f7f88a4532efa8";
            String endpoint = "http://180.167.126.126:8992/api/getcode";
            InputStream in = null;
            try {
                HttpURLConnection con = (HttpURLConnection) new URL(endpoint).openConnection();
                con.setDoInput(true);
                con.setDoOutput(true);
                con.setRequestMethod("GET");
                con.setConnectTimeout(10000);
                con.setReadTimeout(10000);
                con.setRequestProperty("keep-alive", "true");
                con.setRequestProperty("accept", "application/json");
                con.setRequestProperty("content-type", "application/json");

                if (con.getResponseCode() == 200) {
                    in = con.getInputStream();
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    IOUtils.copy(in, bout);
                    Map map = new ObjectMapper().readValue(bout.toByteArray(), Map.class);
                    String sd = encrypt(token,(String)map.get("result"));
                    return  sd;
                }
                
                
                
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String encrypt(String token, String code) throws Exception {
        if (token.length() != 32) throw new Exception("token 长度不正确");
        code = padRight(code, 16, '0');
        
        String tmpToken = "";
        for (int i = 0; i < 8; i++){
            String subString = code.substring(i * 2, (i + 1) * 2);
            tmpToken += subString + token.substring(i * 4, (i + 1) * 4);
        }
        int pos = tmpToken.length() / 2;
        tmpToken = tmpToken.substring(pos) + tmpToken.substring(0, pos);

        pos = tmpToken.length() / 4;
        tmpToken = tmpToken.substring(pos * 3) + tmpToken.substring(pos, pos * 3) + tmpToken.substring(0, pos);
        return URLEncoder.encode(tmpToken, "GBK");
    }


    public static String padRight(String s, int len, char ch){
        int diff = len - s.length();
        if (diff <= 0) {
            return s;
        }

        char[] charr = new char[len];
        System.arraycopy(s.toCharArray(), 0, charr, 0, s.length());
        for (int i = s.length(); i < len; i++) {
            charr[i] = ch;
        }
        return new String(charr);
    }

    public static void main(String[] args) {
        jkdata();
    }

}