zoukankan      html  css  js  c++  java
  • MD5 / SHA 加密 , 和 url参数的值有中文问题

    MD5摘要:

    /**
         * MD5加密
         * @param str
         * @return
         */
         public static String encode(String str) {
                MessageDigest digest;
                try {
                    digest = MessageDigest.getInstance("MD5");
                    try {
                        digest.update(str.getBytes("utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    return new BigInteger(1, digest.digest()).toString(16);
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                    return null;
                }
            }

    解决url参数的值有中文:

        
         /**
          * 解决url参数的值有中文
          * @param url
          * @return
          * @throws UnsupportedEncodingException
          */
            public static String constructParam(String url) throws UnsupportedEncodingException{
                StringBuilder sb = new StringBuilder("");
                if(url == null) {
                    return url;
                }else {
                    String[] entry = url.split("&");
                    for(int i=0;i<entry.length;i++) {
                        String key = entry[i].split("=")[0];
                        String value = entry[i].split("=")[1];
                        String encodeValue = URLEncoder.encode(value, "UTF-8");
                        sb.append(key).append("=").append(encodeValue).append("&");
                    }
                }
                
                return sb.toString().substring(0, sb.lastIndexOf("&"));
            }

    SHA 加密 和 传参:

            
    package com...;
    
    import java.io.InputStream;
    import java.security.MessageDigest;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    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.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import com.alibaba.fastjson.JSONObject;
    import...StreamHelper;
    
    public class HttpClientPostFormDataUtils {
    
        //SHA 加密
        public static String shaEncode(String inStr) throws Exception {
            MessageDigest sha = null;
            try {
                sha = MessageDigest.getInstance("SHA");
            } catch (Exception e) {
                System.out.println(e.toString());
                e.printStackTrace();
                return "";
            }
    
            byte[] byteArray = inStr.getBytes("UTF-8");
            byte[] md5Bytes = sha.digest(byteArray);
            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }
            return hexValue.toString();
        }
    
    
        //SHA加密后 params形式传参
        public static String resultPostFormData(String url, JSONObject jsonObject,Map<String, Object> headers) {
            HttpPost httpPost = new HttpPost(url);
            HttpClient httpClient = HttpClients.createDefault();
            httpPost.setHeader("Content-Type", "application/json");
            Iterator var6 = headers.keySet().iterator();
            while (var6.hasNext()) {
                String key = (String) var6.next();
                System.out.println(key + headers.get(key));
                httpPost.addHeader(key, headers.get(key) + "");
            }
            
            List<BasicNameValuePair> params=new ArrayList<BasicNameValuePair>();
            params.add(new BasicNameValuePair("sid", jsonObject.getString("sid")));
            params.add(new BasicNameValuePair("xm", jsonObject.getString("xm")));
            params.add(new BasicNameValuePair("zjh", jsonObject.getString("zjh")));
            String responseString=null;
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity httpEntity = response.getEntity();
                StatusLine statusLine = response.getStatusLine();
                System.out.println("statusLine: "+statusLine);
                InputStream is = httpEntity.getContent();
                byte[] bytes =StreamHelper.toByteArray(is) ;
                is.close();
                responseString = new String(bytes, "UTF-8");
                System.out.println("反馈结果JSON: "+responseString);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                httpPost.releaseConnection(); // 释放连接
            }
            return responseString;
        }
        
        //SHA加密后 json形式传参
        public static String resultPostJsonData(String url, JSONObject jsonObject,Map<String, Object> headers) {
            HttpPost httpPost = new HttpPost(url);
            HttpClient httpClient = HttpClients.createDefault();
            httpPost.setHeader("Content-Type", "application/json");
            Iterator var6 = headers.keySet().iterator();
            while (var6.hasNext()) {
                String key = (String) var6.next();
                System.out.println(key + headers.get(key));
                httpPost.addHeader(key, headers.get(key) + "");
            }
            String responseString=null;
            try {
                httpPost.setEntity(
                        new StringEntity(jsonObject.toString(), ContentType.create("application/json", "utf-8")));
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity httpEntity = response.getEntity();
                StatusLine statusLine = response.getStatusLine();
                System.out.println("statusLine: "+statusLine);
                InputStream is = httpEntity.getContent();
                byte[] bytes =StreamHelper.toByteArray(is) ;
                is.close();
                responseString = new String(bytes, "UTF-8");
                System.out.println("反馈结果JSON: "+responseString);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                httpPost.releaseConnection(); // 释放连接
            }
            return responseString;
        }
        
        
    }
  • 相关阅读:
    SQL Server 日期和时间函数
    sql日期格式化函数
    C#中事件的使用
    CSS中的display:inline-block
    用aspnet_regiis注册Framework4.0框架
    什么是CSS hack
    第一次MySQL的SQL注入实验
    (二分)Block Towers(cf626)
    (多线程dp)Matrix (hdu 2686)
    (数位dp)Bomb (hdu 3555)
  • 原文地址:https://www.cnblogs.com/lifan12589/p/13516232.html
Copyright © 2011-2022 走看看