Base64Utils.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package com.qmth.cdut.supwisdom.utils;
  2. public abstract class Base64Utils {
  3. private static final byte[] ENC_TAB_BYTES = new byte[64];
  4. static {
  5. int j = 0;
  6. for (byte i = 'A'; i <= 'Z'; i++) {
  7. ENC_TAB_BYTES[j] = i;
  8. j++;
  9. }
  10. for (byte i = 'a'; i <= 'z'; i++) {
  11. ENC_TAB_BYTES[j] = i;
  12. j++;
  13. }
  14. for (byte i = '0'; i <= '9'; i++) {
  15. ENC_TAB_BYTES[j] = i;
  16. j++;
  17. }
  18. ENC_TAB_BYTES[j++] = '+';
  19. ENC_TAB_BYTES[j] = '/';
  20. }
  21. public static String encodeBase64Str(String str) {
  22. byte[] bytes = StringUtils.getBytes(str);
  23. return encodeBase64Str(bytes);
  24. }
  25. public static String encodeBase64Str(byte[] bytes) {
  26. byte[] base64Bytes = encodeBase64(bytes);
  27. return StringUtils.newString(base64Bytes);
  28. }
  29. public static byte[] encodeBase64(byte[] bytes) {
  30. byte[] base64Bytes;
  31. int modulus = bytes.length % 3;
  32. if (modulus == 0) {
  33. base64Bytes = new byte[(4 * bytes.length) / 3];
  34. } else {
  35. base64Bytes = new byte[4 * ((bytes.length / 3) + 1)];
  36. }
  37. int dataLength = (bytes.length - modulus);
  38. int a1;
  39. int a2;
  40. int a3;
  41. for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
  42. a1 = bytes[i] & 0xff;
  43. a2 = bytes[i + 1] & 0xff;
  44. a3 = bytes[i + 2] & 0xff;
  45. base64Bytes[j] = ENC_TAB_BYTES[(a1 >>> 2) & 0x3f];
  46. base64Bytes[j + 1] = ENC_TAB_BYTES[((a1 << 4) | (a2 >>> 4)) & 0x3f];
  47. base64Bytes[j + 2] = ENC_TAB_BYTES[((a2 << 2) | (a3 >>> 6)) & 0x3f];
  48. base64Bytes[j + 3] = ENC_TAB_BYTES[a3 & 0x3f];
  49. }
  50. int b1;
  51. int b2;
  52. int b3;
  53. int d1;
  54. int d2;
  55. switch (modulus) {
  56. case 0:
  57. break;
  58. case 1:
  59. d1 = bytes[bytes.length - 1] & 0xff;
  60. b1 = (d1 >>> 2) & 0x3f;
  61. b2 = (d1 << 4) & 0x3f;
  62. base64Bytes[base64Bytes.length - 4] = ENC_TAB_BYTES[b1];
  63. base64Bytes[base64Bytes.length - 3] = ENC_TAB_BYTES[b2];
  64. base64Bytes[base64Bytes.length - 2] = '=';
  65. base64Bytes[base64Bytes.length - 1] = '=';
  66. break;
  67. case 2:
  68. d1 = bytes[bytes.length - 2] & 0xff;
  69. d2 = bytes[bytes.length - 1] & 0xff;
  70. b1 = (d1 >>> 2) & 0x3f;
  71. b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
  72. b3 = (d2 << 2) & 0x3f;
  73. base64Bytes[base64Bytes.length - 4] = ENC_TAB_BYTES[b1];
  74. base64Bytes[base64Bytes.length - 3] = ENC_TAB_BYTES[b2];
  75. base64Bytes[base64Bytes.length - 2] = ENC_TAB_BYTES[b3];
  76. base64Bytes[base64Bytes.length - 1] = '=';
  77. break;
  78. }
  79. return base64Bytes;
  80. }
  81. private static final byte[] DEC_TAB_BYTES = new byte[128];
  82. static {
  83. for (int i = 0; i < 128; i++) {
  84. DEC_TAB_BYTES[i] = (byte) -1;
  85. }
  86. for (int i = 'A'; i <= 'Z'; i++) {
  87. DEC_TAB_BYTES[i] = (byte) (i - 'A');
  88. }
  89. for (int i = 'a'; i <= 'z'; i++) {
  90. DEC_TAB_BYTES[i] = (byte) (i - 'a' + 26);
  91. }
  92. for (int i = '0'; i <= '9'; i++) {
  93. DEC_TAB_BYTES[i] = (byte) (i - '0' + 52);
  94. }
  95. DEC_TAB_BYTES['+'] = 62;
  96. DEC_TAB_BYTES['/'] = 63;
  97. }
  98. public static String decodeBase64Str(String data) {
  99. byte[] base64Bytes = StringUtils.getBytes(data);
  100. return decodeBase64Str(base64Bytes);
  101. }
  102. public static String decodeBase64Str(byte[] base64Bytes) {
  103. byte[] bytes = decodeBase64(base64Bytes);
  104. return StringUtils.newString(bytes);
  105. }
  106. public static byte[] decodeBase64(byte[] base64Bytes) {
  107. byte[] bytes;
  108. byte b1;
  109. byte b2;
  110. byte b3;
  111. byte b4;
  112. base64Bytes = discardNonBase64Bytes(base64Bytes);
  113. if (base64Bytes[base64Bytes.length - 2] == '=') {
  114. bytes = new byte[(((base64Bytes.length / 4) - 1) * 3) + 1];
  115. } else if (base64Bytes[base64Bytes.length - 1] == '=') {
  116. bytes = new byte[(((base64Bytes.length / 4) - 1) * 3) + 2];
  117. } else {
  118. bytes = new byte[((base64Bytes.length / 4) * 3)];
  119. }
  120. for (int i = 0, j = 0; i < (base64Bytes.length - 4); i += 4, j += 3) {
  121. b1 = DEC_TAB_BYTES[base64Bytes[i]];
  122. b2 = DEC_TAB_BYTES[base64Bytes[i + 1]];
  123. b3 = DEC_TAB_BYTES[base64Bytes[i + 2]];
  124. b4 = DEC_TAB_BYTES[base64Bytes[i + 3]];
  125. bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
  126. bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
  127. bytes[j + 2] = (byte) ((b3 << 6) | b4);
  128. }
  129. if (base64Bytes[base64Bytes.length - 2] == '=') {
  130. b1 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 4]];
  131. b2 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 3]];
  132. bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
  133. } else if (base64Bytes[base64Bytes.length - 1] == '=') {
  134. b1 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 4]];
  135. b2 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 3]];
  136. b3 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 2]];
  137. bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
  138. bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
  139. } else {
  140. b1 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 4]];
  141. b2 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 3]];
  142. b3 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 2]];
  143. b4 = DEC_TAB_BYTES[base64Bytes[base64Bytes.length - 1]];
  144. bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
  145. bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
  146. bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
  147. }
  148. return bytes;
  149. }
  150. private static byte[] discardNonBase64Bytes(byte[] datas) {
  151. byte[] temp = new byte[datas.length];
  152. int bytesCopied = 0;
  153. for (byte data : datas) {
  154. if (isValidBase64Byte(data)) {
  155. temp[bytesCopied++] = data;
  156. }
  157. }
  158. byte[] newData = new byte[bytesCopied];
  159. System.arraycopy(temp, 0, newData, 0, bytesCopied);
  160. return newData;
  161. }
  162. private static boolean isValidBase64Byte(byte b) {
  163. if (b == '=') {
  164. return true;
  165. } else if ((b < 0) || (b >= 128)) {
  166. return false;
  167. } else if (DEC_TAB_BYTES[b] == -1) {
  168. return false;
  169. }
  170. return true;
  171. }
  172. }