base64

package top.ibase4j.core.util;

import java.util.Map;
import top.ibase4j.core.support.security.BASE64Encoder;

public final class SecurityUtil {
    private SecurityUtil() {
    }

    public static final byte[] decryptBASE64(String key) {
        try {
            return (new BASE64Encoder()).decode(key);
        } catch (Exception var2) {
            throw new RuntimeException("解密错误,错误信息:", var2);
        }
    }

    public static final String encryptBASE64(byte[] key) {
        try {
            return (new BASE64Encoder()).encode(key);
        } catch (Exception var2) {
            throw new RuntimeException("加密错误,错误信息:", var2);
        }
    }
package com.ibase4jTest.core.security;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PushbackInputStream;

public class BASE64Encoder {
    private static final char[] PEM_ARRAY = 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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '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', '+', '/'};
    private static final byte[] pem_convert_array = new byte[256];
    private byte[] decode_buffer = new byte[4];

    public BASE64Encoder() {
    }

    public String encode(byte[] bt) {
        int totalBits = bt.length * 8;
        int nn = totalBits % 6;
        int curPos = 0;

        StringBuilder toReturn;
        for(toReturn = new StringBuilder(32); curPos < totalBits; curPos += 6) {
            int bytePos = curPos / 8;
            int pos;
            switch(curPos % 8) {
                case 0:
                    toReturn.append(PEM_ARRAY[(bt[bytePos] & 252) >> 2]);
                case 1:
                case 3:
                case 5:
                default:
                    break;
                case 2:
                    toReturn.append(PEM_ARRAY[bt[bytePos] & 63]);
                    break;
                case 4:
                    if(bytePos == bt.length - 1) {
                        toReturn.append(PEM_ARRAY[(bt[bytePos] & 15) << 2 & 63]);
                    } else {
                        pos = ((bt[bytePos] & 15) << 2 | (bt[bytePos + 1] & 192) >> 6) & 63;
                        toReturn.append(PEM_ARRAY[pos]);
                    }
                    break;
                case 6:
                    if(bytePos == bt.length - 1) {
                        toReturn.append(PEM_ARRAY[(bt[bytePos] & 3) << 4 & 63]);
                    } else {
                        pos = ((bt[bytePos] & 3) << 4 | (bt[bytePos + 1] & 240) >> 4) & 63;
                        toReturn.append(PEM_ARRAY[pos]);
                    }
            }
        }

        if(nn == 2) {
            toReturn.append("==");
        } else if(nn == 4) {
            toReturn.append("=");
        }

        return toReturn.toString();
    }

    public byte[] decode(String str) throws IOException {
        byte[] arrayOfByte = str.getBytes();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(arrayOfByte);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        this.decodeBuffer(inputStream, outputStream);
        return outputStream.toByteArray();
    }

    private void decodeBuffer(InputStream paramInputStream, OutputStream paramOutputStream) throws IOException {
        PushbackInputStream localPushbackInputStream = new PushbackInputStream(paramInputStream);
        int j = 0;

        while(true) {
            while(true) {
                try {
                    int e = this.bytesPerLine();
                    byte i = 0;
                    if(i + this.bytesPerAtom() < e) {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, this.bytesPerAtom());
                        j += this.bytesPerAtom();
                        int var10000 = i + this.bytesPerAtom();
                    } else if(i + this.bytesPerAtom() == e) {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, this.bytesPerAtom());
                        j += this.bytesPerAtom();
                    } else {
                        this.decodeAtom(localPushbackInputStream, paramOutputStream, e - i);
                        j += e - i;
                    }
                } catch (RuntimeException var7) {
                    String.valueOf(j);
                    return;
                }
            }
        }
    }

    private int bytesPerAtom() {
        return 4;
    }

    private int bytesPerLine() {
        return 72;
    }

    private void decodeAtom(PushbackInputStream paramPushbackInputStream, OutputStream paramOutputStream, int paramInt) throws IOException {
        byte j = -1;
        byte k = -1;
        byte m = -1;
        byte n = -1;
        if(paramInt < 2) {
            throw new ArrayStoreException("BASE64Decoder: Not enough bytes for an atom.");
        } else {
            int i;
            do {
                i = paramPushbackInputStream.read();
                if(i == -1) {
                    throw new RuntimeException();
                }
            } while(i == 10 || i == 13);

            this.decode_buffer[0] = (byte)i;
            i = this.readFully(paramPushbackInputStream, this.decode_buffer, 1, paramInt - 1);
            if(i == -1) {
                throw new RuntimeException();
            } else {
                if(paramInt > 3 && this.decode_buffer[3] == 61) {
                    paramInt = 3;
                }

                if(paramInt > 2 && this.decode_buffer[2] == 61) {
                    paramInt = 2;
                }

                switch(paramInt) {
                    case 4:
                        n = pem_convert_array[this.decode_buffer[3] & 255];
                    case 3:
                        m = pem_convert_array[this.decode_buffer[2] & 255];
                    case 2:
                        k = pem_convert_array[this.decode_buffer[1] & 255];
                        j = pem_convert_array[this.decode_buffer[0] & 255];
                    default:
                        switch(paramInt) {
                            case 2:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                break;
                            case 3:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                paramOutputStream.write((byte)(k << 4 & 240 | m >>> 2 & 15));
                                break;
                            case 4:
                                paramOutputStream.write((byte)(j << 2 & 252 | k >>> 4 & 3));
                                paramOutputStream.write((byte)(k << 4 & 240 | m >>> 2 & 15));
                                paramOutputStream.write((byte)(m << 6 & 192 | n & 63));
                        }

                }
            }
        }
    }

    private int readFully(InputStream paramInputStream, byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException {
        for(int i = 0; i < paramInt2; ++i) {
            int j = paramInputStream.read();
            if(j == -1) {
                return i == 0?-1:i;
            }

            paramArrayOfByte[i + paramInt1] = (byte)j;
        }

        return paramInt2;
    }

    static {
        int i;
        for(i = 0; i < 255; ++i) {
            pem_convert_array[i] = -1;
        }

        for(i = 0; i < PEM_ARRAY.length; ++i) {
            pem_convert_array[PEM_ARRAY[i]] = (byte)i;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值