Android gzip、base64 加密、解密

jopen 10年前發布 | 55K 次閱讀 Android 加密 Android開發 移動開發

    import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import android.util.Base64;

public class EncryptUtil {  

    private static final int BUFFER_SIZE = 1024;  

    /** 
     * BASE64 加密 
     * @param str 
     * @return 
     */  
    public static  String encryptBASE64(String str) {  
        if (str == null || str.length() == 0) {  
            return null;  
        }  
        try {  
            byte[] encode = str.getBytes("UTF-8");  
            // base64 加密  
            return new String(Base64.encode(encode, 0, encode.length, Base64.DEFAULT), "UTF-8");  

        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  

        return null;  
    }  

    /** 
     * BASE64 解密 
     * @param str 
     * @return 
     */  
    public static  String decryptBASE64(String str) {  
        if (str == null || str.length() == 0) {  
            return null;  
        }  
        try {  
            byte[] encode = str.getBytes("UTF-8");  
            // base64 解密  
            return new String(Base64.decode(encode, 0, encode.length, Base64.DEFAULT), "UTF-8");  

        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  

        return null;  
    }  

    /** 
     * GZIP 加密 
     *  
     * @param str 
     * @return 
     */  
    public static  byte[] encryptGZIP(String str) {  
        if (str == null || str.length() == 0) {  
            return null;  
        }  

        try {  
            // gzip壓縮  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            GZIPOutputStream gzip = new GZIPOutputStream(baos);  
            gzip.write(str.getBytes("UTF-8"));  

            gzip.close();  

            byte[] encode = baos.toByteArray();  

            baos.flush();  
            baos.close();  

            // base64 加密  
            return encode;  
//          return new String(encode, "UTF-8");  

        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return null;  
    }  

    /** 
     * GZIP 解密 
     *  
     * @param str 
     * @return 
     */  
    public static  String decryptGZIP(String str) {  
        if (str == null || str.length() == 0) {  
            return null;  
        }  

        try {  

            byte[] decode = str.getBytes("UTF-8");  

            //gzip 解壓縮  
            ByteArrayInputStream bais = new ByteArrayInputStream(decode);  
            GZIPInputStream gzip = new GZIPInputStream(bais);  

            byte[] buf = new byte[BUFFER_SIZE];  
            int len = 0;  

            ByteArrayOutputStream baos = new ByteArrayOutputStream();  

            while((len=gzip.read(buf, 0, BUFFER_SIZE))!=-1){  
                 baos.write(buf, 0, len);  
            }  
            gzip.close();  
            baos.flush();  

            decode = baos.toByteArray();  

            baos.close();  

            return new String(decode, "UTF-8");  

        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return null;  
    }  

    /** 
     * 十六進制字符串 轉換為 byte[] 
     *  
     * @param hexString 
     *            the hex string 
     * @return byte[] 
     */  
    public static byte[] hexStringToBytes(String hexString) {  
        if (hexString == null || hexString.equals("")) {  
            return null;  
        }  
        int length = hexString.length() / 2;  
        char[] hexChars = hexString.toCharArray();  
        byte[] d = new byte[length];  
        for (int i = 0; i < length; i++) {  
            int pos = i * 2;  
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
        }  
        return d;  
    }  

    /** 
     * Convert char to byte 
     *  
     * @param c 
     *            char 
     * @return byte 
     */  
    private static byte charToByte(char c) {  
        return (byte) "0123456789abcdef".indexOf(c);  
        // return (byte) "0123456789ABCDEF".indexOf(c);  
    }  

    /** 
     * byte[] 轉換為 十六進制字符串 
     *  
     * @param src 
     * @return 
     */  
    public static String bytesToHexString(byte[] src) {  
        StringBuilder stringBuilder = new StringBuilder("");  

        if (src == null || src.length <= 0) {  
            return null;  
        }  

        for (int i = 0; i < src.length; i++) {  
            int v = src[i] & 0xFF;  
            String hv = Integer.toHexString(v);  
            if (hv.length() < 2) {  
                stringBuilder.append(0);  
            }  
            stringBuilder.append(hv);  
        }  
        return stringBuilder.toString();  
    }  
}  </pre><br />
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!