Base64.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package com.jpsoft.smart.modules.common.utils;
  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. System.out.println("number of triplets = " + numberTriplets);
  74. }
  75. for (int i = 0; i < numberTriplets; i++) {
  76. b1 = binaryData[dataIndex++];
  77. b2 = binaryData[dataIndex++];
  78. b3 = binaryData[dataIndex++];
  79. if (fDebug) {
  80. System.out.println("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3);
  81. }
  82. l = (byte) (b2 & 0x0f);
  83. k = (byte) (b1 & 0x03);
  84. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  85. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  86. byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
  87. if (fDebug) {
  88. System.out.println("val2 = " + val2);
  89. System.out.println("k4 = " + (k << 4));
  90. System.out.println("vak = " + (val2 | (k << 4)));
  91. }
  92. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  93. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  94. encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
  95. encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
  96. }
  97. // form integral number of 6-bit groups
  98. if (fewerThan24bits == EIGHTBIT) {
  99. b1 = binaryData[dataIndex];
  100. k = (byte) (b1 & 0x03);
  101. if (fDebug) {
  102. System.out.println("b1=" + b1);
  103. System.out.println("b1<<2 = " + (b1 >> 2));
  104. }
  105. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  106. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  107. encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
  108. encodedData[encodedIndex++] = PAD;
  109. encodedData[encodedIndex++] = PAD;
  110. } else if (fewerThan24bits == SIXTEENBIT) {
  111. b1 = binaryData[dataIndex];
  112. b2 = binaryData[dataIndex + 1];
  113. l = (byte) (b2 & 0x0f);
  114. k = (byte) (b1 & 0x03);
  115. byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
  116. byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
  117. encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
  118. encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
  119. encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
  120. encodedData[encodedIndex++] = PAD;
  121. }
  122. return new String(encodedData);
  123. }
  124. /**
  125. * Decodes Base64 data into octects
  126. *
  127. * @param encoded string containing Base64 data
  128. * @return Array containind decoded data.
  129. */
  130. public static byte[] decode(String encoded) {
  131. if (encoded == null) {
  132. return null;
  133. }
  134. char[] base64Data = encoded.toCharArray();
  135. // remove white spaces
  136. int len = removeWhiteSpace(base64Data);
  137. if (len % FOURBYTE != 0) {
  138. return null;//should be divisible by four
  139. }
  140. int numberQuadruple = (len / FOURBYTE);
  141. if (numberQuadruple == 0) {
  142. return new byte[0];
  143. }
  144. byte decodedData[] = null;
  145. byte b1 = 0, b2 = 0, b3 = 0, b4 = 0;
  146. char d1 = 0, d2 = 0, d3 = 0, d4 = 0;
  147. int i = 0;
  148. int encodedIndex = 0;
  149. int dataIndex = 0;
  150. decodedData = new byte[(numberQuadruple) * 3];
  151. for (; i < numberQuadruple - 1; i++) {
  152. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))
  153. || !isData((d3 = base64Data[dataIndex++]))
  154. || !isData((d4 = base64Data[dataIndex++]))) {
  155. return null;
  156. }//if found "no data" just return null
  157. b1 = base64Alphabet[d1];
  158. b2 = base64Alphabet[d2];
  159. b3 = base64Alphabet[d3];
  160. b4 = base64Alphabet[d4];
  161. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  162. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  163. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  164. }
  165. if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
  166. return null;//if found "no data" just return null
  167. }
  168. b1 = base64Alphabet[d1];
  169. b2 = base64Alphabet[d2];
  170. d3 = base64Data[dataIndex++];
  171. d4 = base64Data[dataIndex++];
  172. if (!isData((d3)) || !isData((d4))) {//Check if they are PAD characters
  173. if (isPad(d3) && isPad(d4)) {
  174. if ((b2 & 0xf) != 0)//last 4 bits should be zero
  175. {
  176. return null;
  177. }
  178. byte[] tmp = new byte[i * 3 + 1];
  179. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  180. tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
  181. return tmp;
  182. } else if (!isPad(d3) && isPad(d4)) {
  183. b3 = base64Alphabet[d3];
  184. if ((b3 & 0x3) != 0)//last 2 bits should be zero
  185. {
  186. return null;
  187. }
  188. byte[] tmp = new byte[i * 3 + 2];
  189. System.arraycopy(decodedData, 0, tmp, 0, i * 3);
  190. tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  191. tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  192. return tmp;
  193. } else {
  194. return null;
  195. }
  196. } else { //No PAD e.g 3cQl
  197. b3 = base64Alphabet[d3];
  198. b4 = base64Alphabet[d4];
  199. decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
  200. decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
  201. decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
  202. }
  203. return decodedData;
  204. }
  205. /**
  206. * remove WhiteSpace from MIME containing encoded Base64 data.
  207. *
  208. * @param data the byte array of base64 data (with WS)
  209. * @return the new length
  210. */
  211. private static int removeWhiteSpace(char[] data) {
  212. if (data == null) {
  213. return 0;
  214. }
  215. // count characters that's not whitespace
  216. int newSize = 0;
  217. int len = data.length;
  218. for (int i = 0; i < len; i++) {
  219. if (!isWhiteSpace(data[i])) {
  220. data[newSize++] = data[i];
  221. }
  222. }
  223. return newSize;
  224. }
  225. }