base64

base64的java实现

public class BASE64 {
    private static final String TAG = "BASE64";
    public static final byte[] encodingTable = { (byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E',
            (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N',
            (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W',
            (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
            (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',
            (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x',
            (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
            (byte) '7', (byte) '8', (byte) '9', (byte) '+', (byte) '/' };

    public static final byte[] decodingTable;
    static {
        decodingTable = new byte[128];
        for (int i = 0; i < 128; ++i) {
            decodingTable[i] = (byte) -1;
        }
        for (int i = 'A'; i <= 'Z'; ++i) {
            decodingTable[i] = (byte) (i - 'A');
        }
        for (int i = 'a'; i <= 'z'; ++i) {
            decodingTable[i] = (byte) (i - 'a' + 26);
        }
        for (int i = '0'; i < '9'; ++i) {
            decodingTable[i] = (byte) (i - '0' + 52);
        }

        decodingTable['+'] = 62;
        decodingTable['/'] = 63;
    }

    public static String encode(byte[] data) {
        if (data == null) {
            return "";
        }
        byte[] base64 = null;
        final int remainder = data.length % 3;
        final int len = data.length / 3;
        if (remainder == 0) {
            base64 = new byte[len * 4];
        } else {
            base64 = new byte[(len + 1) * 4];
        }
        int base;
        int a1, a2, a3;
        for (int i = 0, j = 0; i < len; ++i, j += 4) {
            base = i * 3;
            a1 = data[base] & 0xff;
            a2 = data[base + 1] & 0xff;
            a3 = data[base + 2] & 0xff;

            base64[j] = encodingTable[(a1 >>> 2) & 0x3f];
            base64[j + 1] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
            base64[j + 2] = encodingTable[((a2 << 2) | (a3 >>> 6)) & 0x3f];
            base64[j + 3] = encodingTable[a3 & 0x3f];
        }

        if (remainder == 1) {
            a1 = data[data.length - 1];

            base64[base64.length - 4] = encodingTable[(a1 >>> 2) & 0x3f];
            base64[base64.length - 3] = encodingTable[(a1 << 4) & 0x3f];
            base64[base64.length - 2] = (byte) '=';
            base64[base64.length - 1] = (byte) '=';
        } else if (remainder == 2) {
            a1 = data[data.length - 2];
            a2 = data[data.length - 1];

            base64[base64.length - 4] = encodingTable[(a1 >>> 2) & 0x3f];
            base64[base64.length - 3] = encodingTable[((a1 << 4) | (a2 >>> 4)) & 0x3f];
            base64[base64.length - 2] = encodingTable[(a2 << 2) & 0x3f];
            base64[base64.length - 1] = (byte) '=';
        }

        return new String(base64);
    }

    public static boolean isValidCharacter(byte x) {
        if ('=' == x) {
            return true;
        }
        if (x < 0 || x > 128) {
            return false;
        }
        if (decodingTable[x] == -1) {
            Logger.d(TAG, "data is not valid. decoding table x:" + x);
            return false;
        }
        return true;
    }

    public static String decode(byte[] data) {
        if (data == null) {
            return "";
        }
        Logger.d(TAG, "data is not null");
        for (int i = 0; i < data.length; ++i) {
            if (isValidCharacter(data[i]) == false) {
                return "";
            }
        }
        Logger.d(TAG, "data is valid");
        byte[] base64 = null;
        int len = data.length / 4;
        if (data[data.length - 2] == '=') {
            len = len - 1;
            base64 = new byte[len * 3 + 1];
        } else if (data[data.length - 1] == '=') {
            len = len - 1;
            base64 = new byte[len * 3 + 2];
        } else {
            base64 = new byte[len * 3];
        }

        int base;
        byte a1, a2, a3, a4;
        for (int i = 0, j = 0; i < len; ++i, j += 3) {
            base = i * 4;
            a1 = decodingTable[data[base]];
            a2 = decodingTable[data[base + 1]];
            a3 = decodingTable[data[base + 2]];
            a4 = decodingTable[data[base + 3]];

            base64[j] = (byte) ((a1 << 2) | (a2 >>> 4));
            base64[j + 1] = (byte) ((a2 << 4) | (a3 >>> 2));
            base64[j + 2] = (byte) ((a3 << 6) | a4);
        }
        if (data[data.length - 2] == '=') {
            a1 = decodingTable[data[data.length - 4]];
            a2 = decodingTable[data[data.length - 3]];
            base64[base64.length - 1] = (byte) ((a1 << 2) | (a2 >>> 4));

        } else if (data[data.length - 1] == '=') {
            a1 = decodingTable[data[data.length - 4]];
            a2 = decodingTable[data[data.length - 3]];
            a3 = decodingTable[data[data.length - 2]];
            base64[base64.length - 2] = (byte) ((a1 << 2) | (a2 >>> 4));
            base64[base64.length - 1] = (byte) ((a2 << 4) | (a3 >>> 2));
        }
        return new String(base64);
    }
}

 

 android 下使用base64:

import android.util.Base64;

编码:

String str = Base64.encodeToString(ori_str.getBytes(), Base64.DEFAULT);

解码:

byte b[]=android.util.Base64.decode(ori_str, Base64.DEFAULT);

String str=new String(b);

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值