Base64.java 9.2 KB

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