Base64Util.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.behosoft.util;
  2. /**
  3. *
  4. * Copyright (c) behosoft Co.,Ltd.
  5. * All Rights Reserved.
  6. *
  7. * This software is the confidential and proprietary information of behosoft.
  8. * (Social Security Department). You shall not disclose such
  9. * Confidential Information and shall use it only in accordance with
  10. * the terms of the license agreement you entered into with behosoft.
  11. *
  12. * Distributable under GNU LGPL license by gnu.org
  13. */
  14. import java.io.ByteArrayOutputStream;
  15. /**
  16. * <p>
  17. * Title: behosoft_[子系统统名]_[模块名]
  18. * </p>
  19. * <p>
  20. * Description: [描述该类概要功能介绍]
  21. * </p>
  22. *
  23. * @author xu
  24. * @version $Revision$ 2013-4-23
  25. * @author (lastest modification by $Author$)
  26. * @since 1.0
  27. */
  28. public class Base64Util {
  29. private static char[] base64EncodeChars =
  30. new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
  31. 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  32. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6',
  33. '7', '8', '9', '+', '/' };
  34. private static byte[] base64DecodeChars =
  35. new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  36. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  37. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  38. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28,
  39. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1,
  40. -1, -1, -1 };
  41. public static String encode(byte[] data) {
  42. StringBuffer sb = new StringBuffer();
  43. int len = data.length;
  44. int i = 0;
  45. int b1, b2, b3;
  46. while (i < len) {
  47. b1 = data[i++] & 0xff;
  48. if (i == len) {
  49. sb.append(base64EncodeChars[b1 >>> 2]);
  50. sb.append(base64EncodeChars[(b1 & 0x3) << 4]);
  51. sb.append("==");
  52. break;
  53. }
  54. b2 = data[i++] & 0xff;
  55. if (i == len) {
  56. sb.append(base64EncodeChars[b1 >>> 2]);
  57. sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
  58. sb.append(base64EncodeChars[(b2 & 0x0f) << 2]);
  59. sb.append("=");
  60. break;
  61. }
  62. b3 = data[i++] & 0xff;
  63. sb.append(base64EncodeChars[b1 >>> 2]);
  64. sb.append(base64EncodeChars[((b1 & 0x03) << 4) | ((b2 & 0xf0) >>> 4)]);
  65. sb.append(base64EncodeChars[((b2 & 0x0f) << 2) | ((b3 & 0xc0) >>> 6)]);
  66. sb.append(base64EncodeChars[b3 & 0x3f]);
  67. }
  68. return sb.toString();
  69. }
  70. public static byte[] decode(String str) {
  71. byte[] data = str.getBytes();
  72. int len = data.length;
  73. ByteArrayOutputStream buf = new ByteArrayOutputStream(len);
  74. int i = 0;
  75. int b1, b2, b3, b4;
  76. while (i < len) {
  77. do {
  78. b1 = base64DecodeChars[data[i++]];
  79. }
  80. while (i < len && b1 == -1);
  81. if (b1 == -1) {
  82. break;
  83. }
  84. do {
  85. b2 = base64DecodeChars[data[i++]];
  86. }
  87. while (i < len && b2 == -1);
  88. if (b2 == -1) {
  89. break;
  90. }
  91. buf.write(((b1 << 2) | ((b2 & 0x30) >>> 4)));
  92. do {
  93. b3 = data[i++];
  94. if (b3 == 61) {
  95. return buf.toByteArray();
  96. }
  97. b3 = base64DecodeChars[b3];
  98. }
  99. while (i < len && b3 == -1);
  100. if (b3 == -1) {
  101. break;
  102. }
  103. buf.write((((b2 & 0x0f) << 4) | ((b3 & 0x3c) >>> 2)));
  104. do {
  105. b4 = data[i++];
  106. if (b4 == 61) {
  107. return buf.toByteArray();
  108. }
  109. b4 = base64DecodeChars[b4];
  110. }
  111. while (i < len && b4 == -1);
  112. if (b4 == -1) {
  113. break;
  114. }
  115. buf.write((((b3 & 0x03) << 6) | b4));
  116. }
  117. return buf.toByteArray();
  118. }
  119. }