base64加密解密utils

摘要:
base64加密解密工具:importjava.io.ByteArrayOutputStream;publicclassBase64Util{privatestaticchar[]base64EncodeChars=newchar[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U

base64加密解密工具:

importjava.io.ByteArrayOutputStream;

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

    private static byte[] base64DecodeChars = new byte[] { -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59,
            60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1,
            -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
            38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1,
            -1, -1};

    /*** 加密
     * 
     * @paramdata
     * @return
     */
    public static String encode(byte[] data) {
        StringBuffer sb = newStringBuffer();
        int len =data.length;
        int i = 0;
        intb1, b2, b3;

        while (i <len) {
            b1 = data[i++] & 0xff;
            if (i ==len) {
                sb.append(base64EncodeChars[b1 >>> 2]);
                sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
                sb.append("==");
                break;
            }
            b2 = data[i++] & 0xff;
            if (i ==len) {
                sb.append(base64EncodeChars[b1 >>> 2]);
                sb.append(base64EncodeChars[((b1 & 0x03) << 4)
                        | ((b2 & 0xf0) >>> 4)]);
                sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
                sb.append("=");
                break;
            }
            b3 = data[i++] & 0xff;
            sb.append(base64EncodeChars[b1 >>> 2]);
            sb.append(base64EncodeChars[((b1 & 0x03) << 4)
                    | ((b2 & 0xf0) >>> 4)]);
            sb.append(base64EncodeChars[((b2 & 0x0f) << 2)
                    | ((b3 & 0xc0) >>> 6)]);
            sb.append(base64EncodeChars[b3 & 0x3f]);
        }
        returnsb.toString();
    }

    /*** 解密
     * 
     * @paramstr
     * @return
     */
    public static byte[] decode(String str) {
        byte[] data =str.getBytes();
        int len =data.length;
        ByteArrayOutputStream buf = newByteArrayOutputStream(len);
        int i = 0;
        intb1, b2, b3, b4;

        while (i <len) {
            do{
                b1 = base64DecodeChars[data[i++]];
            } while (i < len && b1 == -1);
            if (b1 == -1) {
                break;
            }

            do{
                b2 = base64DecodeChars[data[i++]];
            } while (i < len && b2 == -1);
            if (b2 == -1) {
                break;
            }
            buf.write((int) ((b1 << 2) | ((b2 & 0x30) >>> 4)));

            do{
                b3 = data[i++];
                if (b3 == 61) {
                    returnbuf.toByteArray();
                }
                b3 =base64DecodeChars[b3];
            } while (i < len && b3 == -1);
            if (b3 == -1) {
                break;
            }
            buf.write((int) (((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));

            do{
                b4 = data[i++];
                if (b4 == 61) {
                    returnbuf.toByteArray();
                }
                b4 =base64DecodeChars[b4];
            } while (i < len && b4 == -1);
            if (b4 == -1) {
                break;
            }
            buf.write((int) (((b3 & 0x03) << 6) |b4));
        }
        returnbuf.toByteArray();
    }
}

免责声明:文章转载自《base64加密解密utils》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇redhat linux 从/home目录扩展空间至/根目录Nginx配置https兼容http下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

前端js几种加密/解密方法

一、base64加密 base64的github地址 示例 <html> <head> <title>前端的base64使用方法</title> </head> <body> </body> <script>...

Apache Commons 工具类介绍及简单使用

转自:http://www.cnblogs.com/younggun/p/3247261.html Apache Commons包含了很多开源的工具,用于解决平时编程经常会遇到的问题,减少重复劳动。下面是我这几年做开发过程中自己用过的工具类做简单介绍。 组件 功能介绍 BeanUtils 提供了对于JavaBean进行各种操作,克隆对象,属性等等....

用html2canvas转html为图片遇到的那些问题

1.图片跨域问题 在html转化为canvas在转化成图片地址的 时候 canvas.toDataURL("image/png") 遇到报错: Access to image at 'png' from origin 'null' has been blocked by CORS policy: Cross origin requests are only...

接口上传base64编码图片

1 package com.*.util; 2 3 import java.io.FileInputStream; 4 5 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.io.InputStream; 9 import j...

PHP的加密方法汇总

PHP的加密主要有4种方法,除此之外还有一种是URL的加密和解密。希望可以对你们开发有用。 顺带,我会在后面把我整理的一整套CSS3,PHP,MYSQL的开发的笔记打包放到百度云,有需要可以直接去百度云下载,这样以后你们开发就可以直接翻笔记不用百度搜那么麻烦了。  笔记链接:http://pan.baidu.com/s/1qYdQdKK 密码:pvj2  ...

JavaToken:JWT教程

Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该token被设计为紧凑且安全的,特别适用于分布式站点的单点登录(SSO)场景。JWT的声明一般被用来在身份提供者和服务提供者间传递被认证的用户身份信息,以便于从资源服务器获取资源,也可以增加一些额外的其它业务逻辑所必须的声明...