首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java加密解密(6、BASE64)

2012-08-24 
Java加密解密(六、BASE64)public class Base64Codec {private static byte[] emapprivate static byte[] d

Java加密解密(六、BASE64)

public class Base64Codec {    private static byte[] emap;    private static byte[] dmap;        //    static {byte[] map = {    (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)'/', };emap = map;dmap = new byte[128];for (int i = 0; i < emap.length; i++) {    dmap[emap[i]] = (byte) i;}    }    /**     * Encode a String.     */    public final String encode(String str) {if (str == null) {    return null;}return new String(encode(str.getBytes()));    }    /**     * Encode a byte array.     */    public final byte[] encode(byte[] buf) {if (buf == null) {return null;}int sidx;int didx;byte dest[] = new byte[((buf.length + 2) / 3) * 4];// 3-byte to 4-byte conversion + 0-63 to ascii printable conversionfor (sidx = 0, didx = 0; sidx < buf.length - 2; sidx += 3) {dest[didx++] = emap[(buf[sidx] >>> 2) & 077];dest[didx++] = emap[(buf[sidx + 1] >>> 4) & 017 | (buf[sidx] << 4) & 077];dest[didx++] = emap[(buf[sidx + 2] >>> 6) & 003 | (buf[sidx + 1] << 2) & 077];dest[didx++] = emap[buf[sidx + 2] & 077];}if (sidx < buf.length) {dest[didx++] = emap[(buf[sidx] >>> 2) & 077];if (sidx < buf.length - 1) {dest[didx++] = emap[(buf[sidx + 1] >>> 4) & 017 | (buf[sidx] << 4) & 077];dest[didx++] = emap[(buf[sidx + 1] << 2) & 077];} else {dest[didx++] = emap[(buf[sidx] << 4) & 077];}}// add paddingfor (; didx < dest.length; didx++) {dest[didx] = (byte) '=';}return dest;    }    /**     * Decode a string, use the bytes inside the string.     */    public final String decode(String str) {if (str == null) {return null;}return new String(decode(str.getBytes()));    }    /**     * Decode a byte array.     */    public final byte[] decode(byte[] buf) {if (buf == null) {return null;}int tail = buf.length;while (buf[tail - 1] == '=') {tail--;}byte dest[] = new byte[tail - buf.length / 4];// ascii printable to 0-63 conversionfor (int idx = 0; idx < buf.length; idx++) {buf[idx] = dmap[buf[idx]];}// 4-byte to 3-byte conversionint sidx;int didx;for (sidx = 0, didx = 0; didx < dest.length - 2; sidx += 4, didx += 3) {dest[didx] = (byte) (((buf[sidx] << 2) & 255) | ((buf[sidx + 1] >>> 4) & 003));dest[didx + 1] = (byte) (((buf[sidx + 1] << 4) & 255) | ((buf[sidx + 2] >>> 2) & 017));dest[didx + 2] = (byte) (((buf[sidx + 2] << 6) & 255) | (buf[sidx + 3] & 077));}if (didx < dest.length) {dest[didx] = (byte) (((buf[sidx] << 2) & 255) | ((buf[sidx + 1] >>> 4) & 003));}if (++didx < dest.length) {dest[didx] = (byte) (((buf[sidx + 1] << 4) & 255) | ((buf[sidx + 2] >>> 2) & 017));}return dest;    }    //     public final static void main(String argv[]) {Base64Codec codec = new Base64Codec();if ("-e".equals(argv[0])) {System.out.println(codec.encode(argv[1]));} if ("-d".equals(argv[0])) {System.out.println(codec.decode(argv[1]));}    }}

热点排行