zoukankan      html  css  js  c++  java
  • java 接口转码、加密

    备用

    package http;
    
    import java.io.UnsupportedEncodingException;
    import sun.misc.*;  
      
    public class Base64 {  
        // ����  
        public String getBase64(String str) {  
            byte[] b = null;  
            String s = null;  
            try {  
                b = str.getBytes("utf-8");  
            } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
            }  
            if (b != null) {  
                s = new BASE64Encoder().encode(b);  
            }  
            return s;  
        }  
      
        // ����  
        public String getFromBase64(String s) {  
            byte[] b = null;  
            String result = null;  
            if (s != null) {  
                BASE64Decoder decoder = new BASE64Decoder();  
                try {  
                    b = decoder.decodeBuffer(s);  
                    result = new String(b, "utf-8");
                    System.out.println(result);
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }
            return result;  
        }  
    
    }  
    Base64.java
    package http;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class get {
    
        public static void main(String[] args) throws Exception {
            System.out.println(doGet());
        }
        
        public static String doGet() throws Exception {
            URL localURL = new URL("http://www.cnblogs.com/tester-hehehe/");
            URLConnection connection = localURL.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
            
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader reader = null;
            StringBuffer resultBuffer = new StringBuffer();
            String tempLine = null;
            
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }
            try {
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                }
            } finally {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            return resultBuffer.toString();
        }
    }
    get.java
    package http;
    
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    public class login {
        public JSONObject Res(){
            String s="123";
            String mi;
            mi = md5.getKeyedDigest(s, "");
    //        String res = "{'md':'###_pt_doc_cnt','page':'hehe'}";
            String res = "{'page':'gainview','md':'partner_gain_pt_doc_cnt'}";
            String result = new Base64().getBase64(res);
            System.out.println(result);
            ArrayList re = new ArrayList();
            Map req1 = new HashMap();
            req1.put("pid", "****");
            req1.put("authId", "***");
            req1.put("time", "***");
            req1.put("param", result);
            String req = "pid"+"authID"+"time"+result+"**私钥***";
            String req2;
            sha256 bb = new sha256();
            req2 = bb.Encrypt(req, "SHA-256");
            req1.put("sign", req2);
            System.out.println(req1);
            JSONObject jsonObject = JSONObject.fromObject(req1);
            //System.out.println(rr);
            return jsonObject;
        }
        public static void main(String[] argv){
            login a = new login();
            JSONObject b = a.Res();
            System.out.println(b);
            
        }
    }
    login.java
    内容为从网上转载,直接套用
    package http;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * @author King_wangyao
     */
    public class md5 {
        public static byte[] getKeyedDigest(byte[] buffer, byte[] key) {
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(buffer);
                return md5.digest(key);
            } catch (NoSuchAlgorithmException e) {
            }
            return null;
        }
        
            
        public static String getKeyedDigest(String strSrc, String key) {
            try {
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.update(strSrc.getBytes("UTF8"));
                
                String result="";
                byte[] temp;
                temp=md5.digest(key.getBytes("UTF8"));
                for (int i=0; i<temp.length; i++){
                    result+=Integer.toHexString((0x000000ff & temp[i]) | 0xffffff00).substring(6);
                }            
                return result;
                
            } catch (NoSuchAlgorithmException e) {
                
                e.printStackTrace();
                
            }catch(Exception e)
            {
              e.printStackTrace();
            }
            return null;
        }
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String mi;
            String s = "1qaz@WSX3edc";
            //第二个参数请填空字符串
            mi=md5.getKeyedDigest(s,"");
            System.out.println("mi:"+mi);
        }
    }
    md5.java
    package http;
    
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    
    import net.sf.json.JSONObject;
    
    
    public class post {
        public static void main(String[] args) throws Exception {
           // String result = new Base64().getFromBase64(doPost());
            System.out.println(doPost().getBytes());
        }
        
        public static String doPost() throws Exception {
            login lg = new login();
            JSONObject parameterData = lg.Res();
    //        JSONObject parameterData = lg.Res();
            System.out.println(parameterData);
            URL localURL = new URL("http://***.wanlitong.com/");
            URLConnection connection = localURL.openConnection();
            HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
            
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterData.toString().length()));
            
            OutputStream outputStream = null;
            OutputStreamWriter outputStreamWriter = null;
            InputStream inputStream = null;
            InputStreamReader inputStreamReader = null;
            BufferedReader reader = null;
            StringBuffer resultBuffer = new StringBuffer();
            String tempLine = null;
            
            try {
                outputStream = httpURLConnection.getOutputStream();
                outputStreamWriter = new OutputStreamWriter(outputStream);
                
                outputStreamWriter.write(parameterData.toString());
                outputStreamWriter.flush();
                
                if (httpURLConnection.getResponseCode() >= 300) {
                    throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
                }
                
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                }
            } finally {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader != null) {
                    inputStreamReader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            return resultBuffer.toString();
        }
    }
    post.java
    package http;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    public class sha256 {
        public String Encrypt(String strSrc, String encName) {
            MessageDigest md = null;
            String strDes = null;
    
            byte[] bt = strSrc.getBytes();
            try {
                if (encName == null || encName.equals("")) {
                    encName = "SHA-256";
                }
                md = MessageDigest.getInstance(encName);
                md.update(bt);
                strDes = bytes2Hex(md.digest()); // to HexString
            } catch (NoSuchAlgorithmException e) {
                return null;
            }
          //  System.out.println(strDes);
            return strDes;
            
        }
    
        public static String bytes2Hex(byte[] bts) {
            String des = "";
            String tmp = null;
            for (int i = 0; i < bts.length; i++) {
                tmp = (Integer.toHexString(bts[i] & 0xFF));
                if (tmp.length() == 1) {
                    des += "0";
                }
                des += tmp;
            }
            return des;
        }
    }
    sha256.java
  • 相关阅读:
    部署phpmyadmin登录不进去
    无法获取快照信息:锁定文件失败
    nginx: [emerg] BIO_new_file("/etc/nginx/ssl_key/server.crt") failed (SSL: error:02001002:syste
    nginx重启失败
    An error occurred (500 Error)
    Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
    clnt_create: RPC: Program not registered
    [error] 2230#2230: *84 client intended to send too large body: 1711341 bytes
    lnmp部署知乎出现403
    easyui下拉框过滤优化
  • 原文地址:https://www.cnblogs.com/tester-hehehe/p/5662763.html
Copyright © 2011-2022 走看看