首页

关于dbunit开源包org.dbunit.util.Base64的对常用的类型进行加密和对不同类型解密操作

标签:Base64,dbunit,decode,encode,加解密     发布时间:2018-03-03   

一、前言

关于dbunit(2.4.8)开源包中的org.dbunit.util.Base64源码类,进行Base64算法方式对字符串、二进制、序列化Serializable类型进行加解密,具体参见源码说明。

二、源码说明

package org.dbunit.util;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.FileInputStream;@b@import java.io.FilterInputStream;@b@import java.io.FilterOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.ObjectInputStream;@b@import java.io.ObjectOutputStream;@b@import java.io.OutputStream;@b@import java.io.PrintStream;@b@import java.io.Serializable;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class Base64@b@{@b@  private static final Logger logger = LoggerFactory.getLogger(Base64.class);@b@  public static final boolean ENCODE = 1;@b@  public static final boolean DECODE = 0;@b@  private static final int MAX_LINE_LENGTH = 76;@b@  private static final byte EQUALS_SIGN = 61;@b@  private static final byte NEW_LINE = 10;@b@  private static final byte[] ALPHABET = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 };@b@  private static final byte[] DECODABET = { -9, -9, -9, -9, -9, -9, -9, -9, -9, -5, -5, -9, -9, -5, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -5, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, 62, -9, -9, -9, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -9, -9, -9, -1, -9, -9, -9, 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, -9, -9, -9, -9, -9, -9, 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, -9, -9, -9, -9 };@b@  private static final byte BAD_ENCODING = -9;@b@  private static final byte WHITE_SPACE_ENC = -5;@b@  private static final byte EQUALS_SIGN_ENC = -1;@b@  static Class class$org$dbunit$util$Base64$InputStream;@b@  static Class class$org$dbunit$util$Base64$OutputStream;@b@@b@  public static void main(String[] args)@b@  {@b@    logger.debug("main(args=" + args + ") - start");@b@@b@    String s = "Hello, world";@b@    s = "abcd";@b@@b@    byte[] b = encodeString(s).getBytes();@b@    byte[] c = decode(b, 0, b.length);@b@@b@    System.out.println("\n\n" + s + ":" + new String(b) + ":" + new String(c));@b@    try@b@    {@b@      FileInputStream fis = new FileInputStream("c:\\abcd.txt");@b@      InputStream b64is = new InputStream(fis, false);@b@      int ib = 0;@b@      while ((ib = b64is.read()) > 0);@b@    }@b@    catch (Exception e)@b@    {@b@      logger.error("main()", e);@b@@b@      e.printStackTrace();@b@    }@b@  }@b@@b@  private static byte[] encode3to4(byte[] threeBytes)@b@  {@b@    logger.debug("encode3to4(threeBytes=" + threeBytes + ") - start");@b@@b@    return encode3to4(threeBytes, 3);@b@  }@b@@b@  private static byte[] encode3to4(byte[] threeBytes, int numSigBytes)@b@  {@b@    logger.debug("encode3to4(threeBytes=" + threeBytes + ", numSigBytes=" + numSigBytes + ") - start");@b@@b@    byte[] dest = new byte[4];@b@    encode3to4(threeBytes, 0, numSigBytes, dest, 0);@b@    return dest;@b@  }@b@@b@  private static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination, int destOffset)@b@  {@b@    if (logger.isDebugEnabled()) {@b@      logger.debug("encode3to4(source=" + source + ", srcOffset=" + srcOffset + ", numSigBytes=" + numSigBytes + ", destination=" + destination + ", destOffset=" + destOffset + ") - start");@b@    }@b@@b@    int inBuff = ((numSigBytes > 0) ? source[srcOffset] << 24 >>> 8 : 0) | ((numSigBytes > 1) ? source[(srcOffset + 1)] << 24 >>> 16 : 0) | ((numSigBytes > 2) ? source[(srcOffset + 2)] << 24 >>> 24 : 0);@b@@b@    switch (numSigBytes)@b@    {@b@    case 3:@b@      destination[destOffset] = ALPHABET[(inBuff >>> 18)];@b@      destination[(destOffset + 1)] = ALPHABET[(inBuff >>> 12 & 0x3F)];@b@      destination[(destOffset + 2)] = ALPHABET[(inBuff >>> 6 & 0x3F)];@b@      destination[(destOffset + 3)] = ALPHABET[(inBuff & 0x3F)];@b@      return destination;@b@    case 2:@b@      destination[destOffset] = ALPHABET[(inBuff >>> 18)];@b@      destination[(destOffset + 1)] = ALPHABET[(inBuff >>> 12 & 0x3F)];@b@      destination[(destOffset + 2)] = ALPHABET[(inBuff >>> 6 & 0x3F)];@b@      destination[(destOffset + 3)] = 61;@b@      return destination;@b@    case 1:@b@      destination[destOffset] = ALPHABET[(inBuff >>> 18)];@b@      destination[(destOffset + 1)] = ALPHABET[(inBuff >>> 12 & 0x3F)];@b@      destination[(destOffset + 2)] = 61;@b@      destination[(destOffset + 3)] = 61;@b@      return destination;@b@    }@b@@b@    return destination;@b@  }@b@@b@  public static String encodeObject(Serializable serializableObject)@b@  {@b@    if (logger.isDebugEnabled())@b@      logger.debug("encodeObject(serializableObject=" + serializableObject + ") - start");@b@@b@    ByteArrayOutputStream baos = null;@b@    OutputStream b64os = null;@b@    ObjectOutputStream oos = null;@b@    try@b@    {@b@      baos = new ByteArrayOutputStream();@b@      b64os = new OutputStream(baos, true);@b@      oos = new ObjectOutputStream(b64os);@b@@b@      oos.writeObject(serializableObject);@b@    }@b@    catch (IOException e)@b@    {@b@      logger.error("encodeObject()", e);@b@@b@      e.printStackTrace();@b@      Object localObject1 = null;@b@@b@      return localObject1;@b@    }@b@    finally@b@    {@b@      try@b@      {@b@        oos.close();@b@      }@b@      catch (Exception e)@b@      {@b@        logger.error("encodeObject()", e);@b@      }@b@      try@b@      {@b@        b64os.close();@b@      }@b@      catch (Exception e)@b@      {@b@        logger.error("encodeObject()", e);@b@      }@b@      try@b@      {@b@        baos.close();@b@      }@b@      catch (Exception e)@b@      {@b@        logger.error("encodeObject()", e);@b@      }@b@    }@b@@b@    return new String(baos.toByteArray());@b@  }@b@@b@  public static String encodeBytes(byte[] source)@b@  {@b@    if (logger.isDebugEnabled())@b@      logger.debug("encodeBytes(source=" + source + ") - start");@b@@b@    return encodeBytes(source, 0, source.length);@b@  }@b@@b@  public static String encodeBytes(byte[] source, int off, int len)@b@  {@b@    if (logger.isDebugEnabled())@b@      logger.debug("encodeBytes(source=" + source + ", off=" + off + ", len=" + len + ") - start");@b@@b@    int len43 = len * 4 / 3;@b@    byte[] outBuff = new byte[len43 + ((len % 3 > 0) ? 4 : 0) + len43 / 76];@b@@b@    int d = 0;@b@    int e = 0;@b@    int len2 = len - 2;@b@    int lineLength = 0;@b@    while (d < len2)@b@    {@b@      encode3to4(source, d, 3, outBuff, e);@b@@b@      lineLength += 4;@b@      if (lineLength == 76)@b@      {@b@        outBuff[(e + 4)] = 10;@b@        ++e;@b@        lineLength = 0;@b@      }@b@      d += 3; e += 4;@b@    }@b@@b@    if (d < len)@b@    {@b@      encode3to4(source, d, len - d, outBuff, e);@b@      e += 4;@b@    }@b@@b@    return new String(outBuff, 0, e);@b@  }@b@@b@  public static String encodeString(String s)@b@  {@b@    logger.debug("encodeString(s={}) - start", s);@b@@b@    return encodeBytes(s.getBytes());@b@  }@b@@b@  private static byte[] decode4to3(byte[] fourBytes)@b@  {@b@    if (logger.isDebugEnabled())@b@      logger.debug("decode4to3(fourBytes=" + fourBytes + ") - start");@b@@b@    byte[] outBuff1 = new byte[3];@b@    int count = decode4to3(fourBytes, 0, outBuff1, 0);@b@    byte[] outBuff2 = new byte[count];@b@@b@    for (int i = 0; i < count; ++i)@b@      outBuff2[i] = outBuff1[i];@b@@b@    return outBuff2;@b@  }@b@@b@  private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset)@b@  {@b@    if (logger.isDebugEnabled()) {@b@      logger.debug("decode4to3(source=" + source + ", srcOffset=" + srcOffset + ", destination=" + destination + ", destOffset=" + destOffset + ") - start");@b@    }@b@@b@    if (source[(srcOffset + 2)] == 61)@b@    {@b@      outBuff = DECODABET[source[srcOffset]] << 24 >>> 6 | DECODABET[source[(srcOffset + 1)]] << 24 >>> 12;@b@@b@      destination[destOffset] = (byte)(outBuff >>> 16);@b@      return 1;@b@    }@b@@b@    if (source[(srcOffset + 3)] == 61)@b@    {@b@      outBuff = DECODABET[source[srcOffset]] << 24 >>> 6 | DECODABET[source[(srcOffset + 1)]] << 24 >>> 12 | DECODABET[source[(srcOffset + 2)]] << 24 >>> 18;@b@@b@      destination[destOffset] = (byte)(outBuff >>> 16);@b@      destination[(destOffset + 1)] = (byte)(outBuff >>> 8);@b@      return 2;@b@    }@b@@b@    int outBuff = DECODABET[source[srcOffset]] << 24 >>> 6 | DECODABET[source[(srcOffset + 1)]] << 24 >>> 12 | DECODABET[source[(srcOffset + 2)]] << 24 >>> 18 | DECODABET[source[(srcOffset + 3)]] << 24 >>> 24;@b@@b@    destination[destOffset] = (byte)(outBuff >> 16);@b@    destination[(destOffset + 1)] = (byte)(outBuff >> 8);@b@    destination[(destOffset + 2)] = (byte)outBuff;@b@    return 3;@b@  }@b@@b@  public static byte[] decode(String s)@b@  {@b@    logger.debug("decode(s={}) - start", s);@b@@b@    byte[] bytes = s.getBytes();@b@    return decode(bytes, 0, bytes.length);@b@  }@b@@b@  public static String decodeToString(String s)@b@  {@b@    logger.debug("decodeToString(s={}) - start", s);@b@@b@    return new String(decode(s));@b@  }@b@@b@  public static Object decodeToObject(String encodedObject)@b@  {@b@    logger.debug("decodeToObject(encodedObject={} - start", encodedObject);@b@@b@    byte[] objBytes = decode(encodedObject);@b@@b@    ByteArrayInputStream bais = null;@b@    ObjectInputStream ois = null;@b@    try@b@    {@b@      bais = new ByteArrayInputStream(objBytes);@b@      ois = new ObjectInputStream(bais);@b@@b@      Object localObject1 = ois.readObject();@b@@b@      return localObject1;@b@    }@b@    catch (IOException e)@b@    {@b@      logger.error("decodeToObject()", e);@b@@b@      e.printStackTrace();@b@      e = null;@b@@b@      return e;@b@    }@b@    catch (ClassNotFoundException e)@b@    {@b@      logger.error("decodeToObject()", e);@b@@b@      e.printStackTrace();@b@      e = null;@b@@b@      return e;@b@    }@b@    finally@b@    {@b@      try@b@      {@b@        bais.close();@b@      }@b@      catch (Exception e)@b@      {@b@        logger.error("decodeToObject()", e);@b@      }@b@      try@b@      {@b@        ois.close();@b@      }@b@      catch (Exception e)@b@      {@b@        logger.error("decodeToObject()", e);@b@      }@b@    }@b@  }@b@@b@  public static byte[] decode(byte[] source, int off, int len)@b@  {@b@    if (logger.isDebugEnabled())@b@      logger.debug("decode(source=" + source + ", off=" + off + ", len=" + len + ") - start");@b@@b@    int len34 = len * 3 / 4;@b@    byte[] outBuff = new byte[len34];@b@    int outBuffPosn = 0;@b@@b@    byte[] b4 = new byte[4];@b@    int b4Posn = 0;@b@    int i = 0;@b@    byte sbiCrop = 0;@b@    byte sbiDecode = 0;@b@    for (i = 0; i < len; ++i)@b@    {@b@      sbiCrop = (byte)(source[i] & 0x7F);@b@      sbiDecode = DECODABET[sbiCrop];@b@@b@      if (sbiDecode >= -5)@b@      {@b@        if (sbiDecode < -1)@b@          break label221;@b@        b4[(b4Posn++)] = sbiCrop;@b@        if (b4Posn <= 3)@b@          break label221;@b@        outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);@b@        b4Posn = 0;@b@@b@        if (sbiCrop != 61) break label221;@b@        break;@b@      }@b@@b@      System.err.println("Bad Base64 input character at " + i + ": " + source[i] + "(decimal)");@b@      return null;@b@    }@b@@b@    label221: byte[] out = new byte[outBuffPosn];@b@    System.arraycopy(outBuff, 0, out, 0, outBuffPosn);@b@    return out;@b@  }@b@@b@  static byte[] access$000(byte[] x0, int x1, int x2, byte[] x3, int x4)@b@  {@b@    return encode3to4(x0, x1, x2, x3, x4); } @b@  static byte[] access$100() { return DECODABET; } @b@  static int access$200(byte[] x0, int x1, byte[] x2, int x3) { return decode4to3(x0, x1, x2, x3); } @b@  static byte[] access$300(byte[] x0, int x1) { return encode3to4(x0, x1); } @b@  static byte[] access$400(byte[] x0) { return decode4to3(x0);@b@  }@b@@b@  public static class OutputStream extends FilterOutputStream@b@  {@b@    private static final Logger logger = LoggerFactory.getLogger((Base64.class$org$dbunit$util$Base64$OutputStream == null) ? (Base64.class$org$dbunit$util$Base64$OutputStream = Base64.class$("org.dbunit.util.Base64$OutputStream")) : Base64.class$org$dbunit$util$Base64$OutputStream);@b@    private boolean encode;@b@    private int position;@b@    private byte[] buffer;@b@    private int bufferLength;@b@    private int lineLength;@b@@b@    public OutputStream(OutputStream out)@b@    {@b@      this(out, true);@b@    }@b@@b@    public OutputStream(OutputStream out, boolean encode)@b@    {@b@      super(out);@b@      this.encode = encode;@b@      this.bufferLength = ((encode) ? 3 : 4);@b@      this.buffer = new byte[this.bufferLength];@b@      this.position = 0;@b@      this.lineLength = 0;@b@    }@b@@b@    public void write(int theByte)@b@      throws IOException@b@    {@b@      if (logger.isDebugEnabled())@b@        logger.debug("write(theByte=" + theByte + ") - start");@b@@b@      this.buffer[(this.position++)] = (byte)theByte;@b@      if (this.position >= this.bufferLength)@b@      {@b@        if (this.encode)@b@        {@b@          this.out.write(Base64.access$300(this.buffer, this.bufferLength));@b@@b@          this.lineLength += 4;@b@          if (this.lineLength >= 76)@b@          {@b@            this.out.write(10);@b@            this.lineLength = 0;@b@          }@b@        }@b@        else {@b@          this.out.write(Base64.access$400(this.buffer));@b@        }@b@        this.position = 0;@b@      }@b@    }@b@@b@    public void write(byte[] theBytes, int off, int len)@b@      throws IOException@b@    {@b@      if (logger.isDebugEnabled())@b@        logger.debug("write(theBytes=" + theBytes + ", off=" + off + ", len=" + len + ") - start");@b@@b@      for (int i = 0; i < len; ++i)@b@      {@b@        write(theBytes[(off + i)]);@b@      }@b@    }@b@@b@    public void flush()@b@      throws IOException@b@    {@b@      logger.debug("flush() - start");@b@@b@      if (this.position > 0)@b@      {@b@        if (this.encode)@b@        {@b@          this.out.write(Base64.access$300(this.buffer, this.position));@b@        }@b@        else@b@        {@b@          throw new IOException("Base64 input not properly padded.");@b@        }@b@      }@b@@b@      super.flush();@b@      this.out.flush();@b@    }@b@@b@    public void close()@b@      throws IOException@b@    {@b@      logger.debug("close() - start");@b@@b@      flush();@b@@b@      super.close();@b@      this.out.close();@b@@b@      this.buffer = null;@b@      this.out = null;@b@    }@b@  }@b@@b@  public static class InputStream extends FilterInputStream@b@  {@b@    private static final Logger logger = LoggerFactory.getLogger((Base64.class$org$dbunit$util$Base64$InputStream == null) ? (Base64.class$org$dbunit$util$Base64$InputStream = Base64.class$("org.dbunit.util.Base64$InputStream")) : Base64.class$org$dbunit$util$Base64$InputStream);@b@    private boolean encode;@b@    private int position;@b@    private byte[] buffer;@b@    private int bufferLength;@b@    private int numSigBytes;@b@@b@    public InputStream(InputStream in)@b@    {@b@      this(in, false);@b@    }@b@@b@    public InputStream(InputStream in, boolean encode)@b@    {@b@      super(in);@b@      this.encode = encode;@b@      this.bufferLength = ((encode) ? 4 : 3);@b@      this.buffer = new byte[this.bufferLength];@b@      this.position = -1;@b@    }@b@@b@    public int read()@b@      throws IOException@b@    {@b@      logger.debug("read() - start");@b@@b@      if (this.position < 0)@b@      {@b@        int i;@b@        int b;@b@        if (this.encode)@b@        {@b@          byte[] b3 = new byte[3];@b@          this.numSigBytes = 0;@b@          for (i = 0; i < 3; ++i)@b@          {@b@            try@b@            {@b@              b = this.in.read();@b@@b@              if (b >= 0)@b@              {@b@                b3[i] = (byte)b;@b@                this.numSigBytes += 1;@b@              }@b@@b@            }@b@            catch (IOException e)@b@            {@b@              logger.error("read()", e);@b@@b@              if (i == 0)@b@                throw e;@b@            }@b@@b@          }@b@@b@          if (this.numSigBytes > 0)@b@          {@b@            Base64.access$000(b3, 0, this.numSigBytes, this.buffer, 0);@b@            this.position = 0;@b@          }@b@@b@        }@b@        else@b@        {@b@          byte[] b4 = new byte[4];@b@          i = 0;@b@          for (i = 0; i < 4; ++i)@b@          {@b@            b = 0;@b@            do@b@            {@b@              b = this.in.read();@b@            }@b@            while ((b >= 0) && (Base64.access$100()[(b & 0x7F)] < -5));@b@@b@            if (b < 0)@b@              break;@b@@b@            b4[i] = (byte)b;@b@          }@b@@b@          if (i == 4)@b@          {@b@            this.numSigBytes = Base64.access$200(b4, 0, this.buffer, 0);@b@            this.position = 0;@b@          }@b@@b@        }@b@@b@      }@b@@b@      if (this.position >= 0)@b@      {@b@        if (this.position >= this.numSigBytes)@b@          return -1;@b@@b@        int b = this.buffer[(this.position++)];@b@@b@        if (this.position >= this.bufferLength)@b@          this.position = -1;@b@@b@        return b;@b@      }@b@@b@      return -1;@b@    }@b@@b@    public int read(byte[] dest, int off, int len)@b@      throws IOException@b@    {@b@      if (logger.isDebugEnabled()) {@b@        logger.debug("read(dest=" + dest + ", off=" + off + ", len=" + len + ") - start");@b@      }@b@@b@      for (int i = 0; i < len; ++i)@b@      {@b@        int b = read();@b@@b@        if (b < 0)@b@          return -1;@b@@b@        dest[(off + i)] = (byte)b;@b@      }@b@      return i;@b@    }@b@  }@b@}