zoukankan      html  css  js  c++  java
  • java Base64算法的使用

      Base64是常见的网络加密算法,Base64编码可用于在HTTP环境下传递较长的标识信息。详见 Base64介绍

    1 自定义的base64算法

    Base64Encrypt.java

    public class Base64Encrypt {
        private static final String CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
        // base64解密
        private static byte[] base64Decode(String input) {
            if (input.length() % 4 != 0) {
                throw new IllegalArgumentException("Invalid base64 input");
            }
            byte decoded[] = new byte[((input.length() * 3) / 4)
                    - (input.indexOf('=') > 0 ? (input.length() - input.indexOf('=')) : 0)];
            char[] inChars = input.toCharArray();
            int j = 0;
            int b[] = new int[4];
            for (int i = 0; i < inChars.length; i += 4) {
                // This could be made faster (but more complicated) by precomputing
                // these index locations.
                b[0] = CODES.indexOf(inChars[i]);
                b[1] = CODES.indexOf(inChars[i + 1]);
                b[2] = CODES.indexOf(inChars[i + 2]);
                b[3] = CODES.indexOf(inChars[i + 3]);
                decoded[j++] = (byte) ((b[0] << 2) | (b[1] >> 4));
                if (b[2] < 64) {
                    decoded[j++] = (byte) ((b[1] << 4) | (b[2] >> 2));
                    if (b[3] < 64) {
                        decoded[j++] = (byte) ((b[2] << 6) | b[3]);
                    }
                }
            }
            return decoded;
        }
    
        // base64加密
        private static String base64Encode(byte[] in) {
            StringBuilder out = new StringBuilder((in.length * 4) / 3);
            int b;
            for (int i = 0; i < in.length; i += 3) {
                b = (in[i] & 0xFC) >> 2;
                out.append(CODES.charAt(b));
                b = (in[i] & 0x03) << 4;
                if (i + 1 < in.length) {
                    b |= (in[i + 1] & 0xF0) >> 4;
                    out.append(CODES.charAt(b));
                    b = (in[i + 1] & 0x0F) << 2;
                    if (i + 2 < in.length) {
                        b |= (in[i + 2] & 0xC0) >> 6;
                        out.append(CODES.charAt(b));
                        b = in[i + 2] & 0x3F;
                        out.append(CODES.charAt(b));
                    } else {
                        out.append(CODES.charAt(b));
                        out.append('=');
                    }
                } else {
                    out.append(CODES.charAt(b));
                    out.append("==");
                }
            }
            return out.toString();
        }

    测试代码:

        public static void main(String[] args) {
            String input = "we are a team.";
            String encode = base64Encode(input.getBytes());
            System.out.println("encode: " + encode);
    
            String decode = new String(base64Decode(encode));
            System.out.println("decode: " + decode);
        }

    2 bcprov的Base64算法

      引入bcprov-jdk15on-154.jar,提供对base64算法的支持

    bcprov-jdk15on-154.jar地址: https://commons.apache.org/proper/commons-codec/download_codec.cgi

      测试代码:

    String input ="we are a team .";
     byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(input.getBytes());
        System.out.println("encode: " + new String(encodeBytes));
    
        byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
        System.out.println("decode: " + new String(decodeBytes));
  • 相关阅读:
    信息的封装和隐藏
    力扣 20. 有效的括号
    servlet执行原理
    当请求一个Servlet时,后台如何运作?
    req.getAttribute 和 req.getParameter
    Servlet 实现登录页面,并在条件下跳转
    request.getRequestDispatcher(a.jsp).forward(request,response)和response.sendRedirect的差别
    通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检查 SQL Server 的实例正在
    Cocos2d-x 3.0 精灵帧缓存(SpriteFrameCache)
    lua 中处理cocos2dx 的button 事件
  • 原文地址:https://www.cnblogs.com/wangshuo1/p/5797864.html
Copyright © 2011-2022 走看看