Base64.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package info.aspirecn.iov.sjjh.supplier10000043.util;
  2. public final class Base64 {
  3. static private final int BASELENGTH = 128;
  4. static private final int LOOKUPLENGTH = 64;
  5. static private final int TWENTYFOURBITGROUP = 24;
  6. static private final int EIGHTBIT = 8;
  7. static private final int SIXTEENBIT = 16;
  8. static private final int FOURBYTE = 4;
  9. static private final int SIGN = -128;
  10. static private final char PAD = '=';
  11. static private final boolean fDebug = false;
  12. static final private byte[] base64Alphabet = new byte[BASELENGTH];
  13. static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
  14. static {
  15. for (int i = 0; i < BASELENGTH; ++i) {
  16. base64Alphabet[i] = -1;
  17. }
  18. for (int i = 'Z'; i >= 'A'; i--) {
  19. base64Alphabet[i] = (byte) (i - 'A');
  20. }
  21. for (int i = 'z'; i >= 'a'; i--) {
  22. base64Alphabet[i] = (byte) (i - 'a' + 26);
  23. }
  24. for (int i = '9'; i >= '0'; i--) {
  25. base64Alphabet[i] = (byte) (i - '0' + 52);
  26. }
  27. base64Alphabet['+'] = 62;
  28. base64Alphabet['/'] = 63;
  29. for (int i = 0; i <= 25; i++) {
  30. lookUpBase64Alphabet[i] = (char) ('A' + i);
  31. }
  32. for (int i = 26, j = 0; i <= 51; i++, j++) {
  33. lookUpBase64Alphabet[i] = (char) ('a' + j);
  34. }
  35. for (int i = 52, j = 0; i <= 61; i++, j++) {
  36. lookUpBase64Alphabet[i] = (char) ('0' + j);
  37. }
  38. lookUpBase64Alphabet[62] = (char) '+';
  39. lookUpBase64Alphabet[63] = (char) '/';
  40. }
  41. private static boolean isWhiteSpace(char octect) {
  42. return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
  43. }
  44. private static boolean isPad(char octect) {
  45. return (octect == PAD);
  46. }
  47. private static boolean isData(char octect) {
  48. return (octect < BASELENGTH && base64Alphabet[octect] != -1);
  49. }
  50. /**
  51. * Encodes hex octects into Base64
  52. *
  53. * @param binaryData Array containing binaryData
  54. * @return Encoded Base64 array
  55. */
  56. public static String encode(byte[] binaryData) {
  57. if (binaryData == null) {
  58. return null;
  59. }
  60. int lengthDataBits = binaryData.length * EIGHTBIT;
  61. if (lengthDataBits == 0) {
  62. return "";
  63. }
  64. int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
  65. int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
  66. int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;
  67. char encodedData[] = null;
  68. encodedData = new char[numberQuartet * 4];
  69. byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
  70. int encodedIndex = 0;
  71. int dataIndex = 0;
  72. if (fDebug) {
  73. }
  74. for (int i = 0; i < numberTriplets; i++) {
  75. b1 = binaryData[dataIndex++];
  76. b2 = binaryData[dataIndex++];
  77. b3 = binaryData[dataIndex++];
  78. if (fDebug) {
  79. }
  80. l = (byte) (b2 & 0x0f);
  81. k = (byte) (b1 & 0x03);
  82. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  83. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  84. byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
  85. if (fDebug) {
  86. }
  87. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  88. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  89. encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
  90. encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
  91. }
  92. // form integral number of 6-bit groups
  93. if (fewerThan24bits == EIGHTBIT) {
  94. b1 = binaryData[dataIndex];
  95. k = (byte) (b1 & 0x03);
  96. if (fDebug) {
  97. }
  98. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  99. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  100. encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
  101. encodedData[encodedIndex++] = PAD;
  102. encodedData[encodedIndex++] = PAD;
  103. } else if (fewerThan24bits == SIXTEENBIT) {
  104. b1 = binaryData[dataIndex];
  105. b2 = binaryData[dataIndex + 1];
  106. l = (byte) (b2 & 0x0f);
  107. k = (byte) (b1 & 0x03);
  108. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  109. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  110. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  111. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  112. encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
  113. encodedData[encodedIndex++] = PAD;
  114. }
  115. return new String(encodedData);
  116. }
  117. /**
  118. * Decodes Base64 data into octects
  119. *
  120. * @param encoded string containing Base64 data
  121. * @return Array containind decoded data.
  122. */
  123. public static byte[] decode(String encoded) {
  124. if (encoded == null) {
  125. return null;
  126. }
  127. char[] base64Data = encoded.toCharArray();
  128. // remove white spaces
  129. int len = removeWhiteSpace(base64Data);
  130. if (len % FOURBYTE != 0) {
  131. return null;//should be divisible by four
  132. }
  133. int numberQuadruple = (len / FOURBYTE);
  134. if (numberQuadruple == 0) {
  135. return new byte[0];
  136. }
  137. byte decodedData[] = null;
  138. byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  139. char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
  140. int i = 0;
  141. int encodedIndex = 0;
  142. int dataIndex = 0;
  143. decodedData = new byte[(numberQuadruple) * 3];
  144. for (; i < numberQuadruple - 1; i++) {
  145. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))
  146. || !isData((d3 = base64Data[dataIndex++]))
  147. || !isData((d4 = base64Data[dataIndex++]))) {
  148. return null;
  149. }//if found "no data" just return null
  150. b1 = base64Alphabet[d1];
  151. b2 = base64Alphabet[d2];
  152. b3 = base64Alphabet[d3];
  153. b4 = base64Alphabet[d4];
  154. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  155. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  156. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  157. }
  158. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
  159. return null;//if found "no data" just return null
  160. }
  161. b1 = base64Alphabet[d1];
  162. b2 = base64Alphabet[d2];
  163. d3 = base64Data[dataIndex++];
  164. d4 = base64Data[dataIndex++];
  165. if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters
  166. if (isPad(d3) && isPad(d4)) {
  167. if ((b2 & 0xf) != 0)//last 4 bits should be zero
  168. {
  169. return null;
  170. }
  171. byte[] tmp = new byte[i * 3 + 1];
  172. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  173. tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
  174. return tmp;
  175. } else if (!isPad(d3) && isPad(d4)) {
  176. b3 = base64Alphabet[d3];
  177. if ((b3 & 0x3) != 0)//last 2 bits should be zero
  178. {
  179. return null;
  180. }
  181. byte[] tmp = new byte[i * 3 + 2];
  182. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  183. tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  184. tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  185. return tmp;
  186. } else {
  187. return null;
  188. }
  189. } else { //No PAD e.g 3cQl
  190. b3 = base64Alphabet[d3];
  191. b4 = base64Alphabet[d4];
  192. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  193. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  194. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  195. }
  196. return decodedData;
  197. }
  198. /**
  199. * remove WhiteSpace from MIME containing encoded Base64 data.
  200. *
  201. * @param data the byte array of base64 data (with WS)
  202. * @return the new length
  203. */
  204. private static int removeWhiteSpace(char[] data) {
  205. if (data == null) {
  206. return 0;
  207. }
  208. // count characters that's not whitespace
  209. int newSize = 0;
  210. int len = data.length;
  211. for (int i = 0; i < len; i++) {
  212. if (!isWhiteSpace(data[i])) {
  213. data[newSize++] = data[i];
  214. }
  215. }
  216. return newSize;
  217. }
  218. }