com.easyway.java.rpc.secure.security.Encodes.java Source code

Java tutorial

Introduction

Here is the source code for com.easyway.java.rpc.secure.security.Encodes.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.easyway.java.rpc.secure.security;

import com.easyway.java.rpc.secure.MD5;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringEscapeUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

/**
 * ??????.
 * 
 * 1.Commons-Codec hex/base64 ? 2.base62 ? 3.Commons-Langxml/html escape
 * 4.JDK??URLEncoder
 * 
 * @author calvin
 */
public class Encodes {

    private static final String DEFAULT_URL_ENCODING = "UTF-8";
    private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
            .toCharArray();
    private static int offset = 2;
    private static ArrayList<Character> list;
    static {
        list = new ArrayList<Character>();
        for (char c : BASE62) {
            list.add(c);
        }
    }

    /**
     * Hex?.
     */
    public static String encodeHex(byte[] input) {
        return Hex.encodeHexString(input);
    }

    /**
     * Hex?.
     */
    public static byte[] decodeHex(String input) {
        try {
            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * Base64?.
     */
    public static String encodeBase64(byte[] input) {
        return Base64.encodeBase64String(input);
    }

    /**
     * Base64?, URL(Base64URL?'+''/''-''_', ?RFC3548).
     */
    public static String encodeUrlSafeBase64(byte[] input) {
        return Base64.encodeBase64URLSafeString(input);
    }

    /**
     * Base64?.
     */
    public static byte[] decodeBase64(String input) {
        return Base64.decodeBase64(input);
    }

    /**
     * Base62?
     */
    public static String encodeBase62(byte[] input) {
        char[] chars = new char[input.length];
        for (int i = 0; i < input.length; i++) {
            chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
        }
        return new String(chars);
    }

    /**
     * Html ?.
     */
    public static String escapeHtml(String html) {
        return StringEscapeUtils.escapeHtml4(html);
    }

    /**
     * Html ?.
     */
    public static String unescapeHtml(String htmlEscaped) {
        return StringEscapeUtils.unescapeHtml4(htmlEscaped);
    }

    /**
     * Xml ?.
     */
    public static String escapeXml(String xml) {
        return StringEscapeUtils.escapeXml(xml);
    }

    /**
     * Xml ?.
     */
    public static String unescapeXml(String xmlEscaped) {
        return StringEscapeUtils.unescapeXml(xmlEscaped);
    }

    /**
     * URL ?, EncodeUTF-8.
     */
    public static String urlEncode(String part) {
        try {
            return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw Exceptions.unchecked(e);
        }
    }

    /**
     * URL ?, EncodeUTF-8.
     */
    public static String urlDecode(String part) {

        try {
            return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            throw Exceptions.unchecked(e);
        }
    }

    // encode rongxin.li
    public static String encodeXX(String str) {
        // Base64?, URL(Base64URL?'+''/''-''_'
        str = Encodes.encodeUrlSafeBase64(str.getBytes());
        StringBuffer buff1 = new StringBuffer();
        // ?
        int length = list.size();
        // ? ?? ?
        for (char c : str.toCharArray()) {
            // ?  
            int i = list.indexOf(c);
            // ? list ??? ??
            if (i < 0) {
                buff1.append(c);
                continue;
            }
            // ?
            i = i + offset < length ? i + offset : i + offset - length;
            buff1.append(list.get(i));
        }
        return buff1.toString();
    }

    // decode rongxin.li
    public static String decodeXX(String str) {
        StringBuffer buff2 = new StringBuffer();
        // ?
        int length = list.size();
        // ? ?? ?
        for (char c : str.toString().toCharArray()) {
            // ?  
            int i = list.indexOf(c);
            // ? list ??? ??
            if (i < 0) {
                buff2.append(c);
                continue;
            }
            // ??
            i = i - offset >= 0 ? i - offset : i - offset + length;
            // 
            buff2.append(list.get(i));
        }
        String deStr = buff2.toString();
        byte[] base64de = Encodes.decodeBase64(deStr);
        try {
            return new String(base64de, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    // encode rongxin.li
    public static String encode(String str) {
        // Base64?, URL(Base64URL?'+''/''-''_'
        try {
            str = new String(Base64.encodeBase64(str.getBytes("GBK"), false, true));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        StringBuffer buff1 = new StringBuffer();
        // ?
        int length = list.size();
        // ? ?? ?
        for (char c : str.toCharArray()) {
            // ?  
            int i = list.indexOf(c);
            // ? list ??? ??
            if (i < 0) {
                if (c == '-') {
                    buff1.append("+");
                } else if (c == '_') {
                    buff1.append("/");
                } else {
                    buff1.append(c);
                }
                continue;
            }

            // ?
            i = i + offset < length ? i + offset : i + offset - length;
            buff1.append(list.get(i));
        }
        return buff1.toString();
    }

    // decode rongxin.li
    public static String decode(String str) {
        StringBuffer buff2 = new StringBuffer();
        // ?
        int length = list.size();
        // ? ?? ?
        for (char c : str.toString().toCharArray()) {
            // ?  
            int i = list.indexOf(c);
            // ? list ??? ??
            if (i < 0) {
                if (c == '+') {
                    buff2.append("-");
                } else if (c == '/') {
                    buff2.append("_");
                } else {
                    buff2.append(c);
                }
                continue;
            }
            // ??
            i = i - offset >= 0 ? i - offset : i - offset + length;
            // 
            buff2.append(list.get(i));
        }
        String deStr = buff2.toString();
        try {
            // byte[] deStrbyte=deStr.getBytes("ISO-8859-1");
            byte[] base64de = Base64.decodeBase64(deStr);
            return new String(base64de, "GBK");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    /**
     * 
     * @Function : aesEncode
     * @Desc : AES   KEY  input keyMD5 ??16 ? AES 
     * @Author : YunLei.Huo
     * @param input
     * @param key
     * @return
     */
    public static String aesEncode(String input, String key) {
        String keyStr = MD5.Md5(key);
        byte[] keyByte = keyStr.getBytes();
        byte[] encryptResult = Cryptos.aesEncrypt(input.getBytes(), keyByte);
        return encodeHex(encryptResult);
    }

    /**
     * 
     * @Function : aesDecode
     * @Desc : AES  HEX?AES
     * @Author : YunLei.Huo
     * @param d5key
     * @return
     */
    public static String aesDecode(String input, String d5key) {
        byte[] inputByte = decodeHex(input);
        return Cryptos.aesDecrypt(inputByte, d5key.getBytes());
    }

    // SHA-1  ?? cuidongxue
    public static String operationAlgorithm(String secureData)
            throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md = MessageDigest.getInstance("SHA-1");

        md.update(secureData.getBytes("utf-8"), 0, secureData.length());
        byte[] sha1hash = md.digest();
        return convertToHex(sha1hash);
    }

    /**
     * Hex?
     * 
     * @param data
     * @return
     */
    private static String convertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfbyte = (data[i] >>> 4) & 0x0F;
            int two_halfs = 0;
            do {
                if ((0 <= halfbyte) && (halfbyte <= 9))
                    buf.append((char) ('0' + halfbyte));
                else
                    buf.append((char) ('a' + (halfbyte - 10)));
                halfbyte = data[i] & 0x0F;
            } while (two_halfs++ < 1);
        }
        return buf.toString();
    }

    public static void main(String[] args) throws Exception {
        try {
            //         String ss="";
            //         String mm=new String(ss.getBytes("GBK"),"GBK");
            //
            //         String str= Encodes.encode(mm);
            //
            //         System.out.println("strtrim ="+str);
            //         String decode=new String(str.getBytes(),"UTF-8");
            //         System.out.println("jiemi strtrim ="+Encodes.decode(decode));

            //         String ssa="R1A/Ry";
            //         String sms=new String (ssa.getBytes("GBK"),"UTF-8");
            //         System.out.println("jiemi strtrim ="+Encodes.decode(sms));

            String str = "3vE9sulNy/I7uttPwhtQuNV1yg1S7O8y";

            System.out.println("strtrim =" + Encodes.decode(str));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}