java-base64

1.encode

  

    public static String encode(byte[] bValue, String encode) {
       
        ByteArrayOutputStream o = new ByteArrayOutputStream();
        byte d[] = new byte[4];
        try {
            int count = 0;

            for(byte x[] = bValue; count < x.length;) {
                byte c = x[count];
                count++;
                d[0] = (byte) ((c & 0xfc) >> 2);
                d[1] = (byte) ((c & 3) << 4);

                if(count < x.length) {
                    c = x[count];
                    count++;
                    d[1] = (byte) (d[1] + (byte) ((c & 0xf0) >> 4));
                    d[2] = (byte) ((c & 0xf) << 2);

                    if(count < x.length) {
                        c = x[count];
                        count++;
                        d[2] = (byte) (d[2] + ((c & 0xc0) >> 6));
                        d[3] = (byte) (c & 0x3f);
                    }
                    else {
                        d[3] = 64;
                    }
                }
                else {
                    d[2] = 64;
                    d[3] = 64;
                }

                int n = 0;

                while(n <= 3) {
                    o.write(strTableBase64.charAt(d[n]));
                    n++;
                }
            }
        }
        catch(StringIndexOutOfBoundsException e) {
            logger.error(e.toString());
        }
        
        String temp = null;
        
        if(o != null){
            
            try {
                temp = new String(o.toByteArray(), encode);
            } catch (UnsupportedEncodingException e1) {
                logger.error(e1.toString(), e1);
            }
            
            try {
                o.close();
            } catch (IOException e) {
                o = null;
            }

        }
        
        return temp;
    }

2.decode

 public static byte[] decode(String strValue, String encode) {
        ByteArrayOutputStream o = new ByteArrayOutputStream();
        byte d[] = new byte[4];

        try{
            int count = 0;
            byte x[] = null;
            try {
                x = strValue.getBytes(encode);
            } catch (UnsupportedEncodingException e) {
                logger.error(e.toString(), e);
            }

            do {
                if(count >= x.length) {
                    break;
                }

                for(int n = 0; n <= 3; n++){
                    if(count >= x.length) {
                        d[n] = 64;
                    }
                    else {
                        int y = strTableBase64.indexOf(x[count]);

                        if(y < 0) {
                            y = 65;
                        }

                        d[n] = (byte) y;
                    }

                    count++;
                }

                o.write((byte) (((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));

                if(d[2] != 64) {
                    o.write((byte) (((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));

                    if(d[3] != 64) {
                        o.write( (byte) ( ((d[2] & 3) << 6) + (d[3] & 0x3f)));
                    }
                }
            } while(true);
        }
        catch(StringIndexOutOfBoundsException e) {
            logger.error(e.toString());
        }
        
        if(o != null){
            d = o.toByteArray();
            try {
                o.close();
                o = null;
            } catch (IOException e) {
                logger.error(e.toString(), e);
            }
            
        }
        return d;
    }
原文地址:https://www.cnblogs.com/jerry19890622/p/3282655.html