zoukankan      html  css  js  c++  java
  • 对接https数据(3des加密)

    private void checkThread() {
    Urls urls = new Urls(type);//根据唯一识别类型初始化参数,可根据实际情况修改此构造函数

    //访问国家平台接口,取出模板数据
    JSONObject jo = new JSONObject();
    jo.put("wstdid","ff80808167179c450167c015a1a30188");
    urls.setData(jo.toString());
    // String data = Self3des.self3desJiam(urls);//数据加密,根据实际加密方式改写加密方法
    String data = "testAccount%25%25%25%256%2BmbF25%2F62mHJz%2FYH6y7Woh4VROXmIf8uT4Kg7qPY66SlKpx1D%2F8nP32lbFMh6ZU%25%25%25%250fccdb9d8b2ef8dfa9e86312ad6967fd1139b6cbc99da1c2d55ba302fbc8cfde%25%25%25%251550542486952%25%25%25%25report_down_tpl";
    //urls.setUrl(urls.getUrl()+"?datainfo="+data);
    /* jo.put("datainfo", data);
    String jostr = jo.toString();*/
    String gjRlt = HttpClientUtil.doPost(urls.getUrl(), data, "utf-8");
    JSONObject rlt = JSONObject.parseObject(gjRlt);

    //对反馈结果进行判断处理,取出数据录入供水系统
    if("0".equals(rlt.get("code"))){
    urls.setData(rlt.getString("data"));
    data = Self3des.self3desJiam(urls);//数据解密,根据实际解密方式改写解密方法

    //将取出的数据导入供水系统
    String cxgsRlt = HttpClientUtil.doPost(urls.getcxgsurl(), data, "utf-8");
    urls.setData(cxgsRlt);
    }

    }

    package cxgshttp.utils;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.StatusLine;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.util.EntityUtils;
    /**
    * 利用HttpClient进行post请求的工具类
    * @ClassName: HttpClientUtil
    * @Description: TODO
    * @author Devin <xxx>
    * @date 2017年2月7日 下午1:43:38
    *
    */
    public class HttpClientUtil {
    @SuppressWarnings("resource")
    public static String doPost(String url,String jsonstr,String charset){
    HttpClient httpClient = null;
    HttpPost httpPost = null;
    String result = null;
    try{
    httpClient = new SSLClient();
    httpPost = new HttpPost(url);
    httpPost.addHeader("Content-Type", "application/json");
    /* StringEntity se = new StringEntity(jsonstr);
    se.setContentType("text/json");
    se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
    httpPost.setEntity(se);*/
    //httpPost.setEntity(new StringEntity(jsonstr, "UTF-8"));
    HttpResponse response = httpClient.execute(httpPost);
    if(response != null){
    HttpEntity resEntity = response.getEntity();
    if(resEntity != null){
    result = EntityUtils.toString(resEntity,charset);
    }
    }
    }catch(Exception ex){
    ex.printStackTrace();
    }
    return result;
    }
    }

    public class SSLClient extends DefaultHttpClient {
    public SSLClient() throws Exception{
    super();
    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {
    @Override
    public void checkClientTrusted(X509Certificate[] chain,
    String authType) throws CertificateException {
    }
    @Override
    public void checkServerTrusted(X509Certificate[] chain,
    String authType) throws CertificateException {
    }
    @Override
    public X509Certificate[] getAcceptedIssuers() {
    return null;
    }
    };
    ctx.init(null, new TrustManager[]{tm}, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = this.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, ssf));
    }
    }

    package cxgshttp.utils;

    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;

    public class DESCryptography {
    public static byte[] DES_CBC_Encrypt(byte[] content, byte[] keyBytes){
    try {
    DESKeySpec keySpec=new DESKeySpec(keyBytes);
    SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
    SecretKey key=keyFactory.generateSecret(keySpec);

    Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
    byte[] result=cipher.doFinal(content);
    return result;
    } catch (Exception e) {
    System.out.println("exception:"+e.toString());
    }
    return null;
    }

    public static byte[] DES_CBC_Decrypt(byte[] content, byte[] keyBytes){
    try {
    DESKeySpec keySpec=new DESKeySpec(keyBytes);
    SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("DES");
    SecretKey key=keyFactory.generateSecret(keySpec);

    Cipher cipher=Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));
    byte[] result=cipher.doFinal(content);
    return result;
    } catch (Exception e) {
    System.out.println("exception:"+e.toString());
    }
    return null;
    }

    }

  • 相关阅读:
    设计模式一 Simple Factory, Factory Method, Abstract Factory以及Builder模式简述
    SQL Server中对XML操作
    开发常用小工具介绍
    强制休息程序 EyeGuardian 眼睛守护者 Beta测试版
    定时计划任务方案比较以及通过脚本创建计划任务(SchTasks命令)
    在Myeclipse中配置Maven
    Jena的环境配置
    0x01_go代码简单示例
    0x00_go语言安装
    信息收集工具
  • 原文地址:https://www.cnblogs.com/wby-666/p/10536875.html
Copyright © 2011-2022 走看看