Base64的加密解密 @author SeanDragon Create By 2017-04-20 14:43
| 13 | * Create By 2017-04-20 14:43 |
| 14 | */ |
| 15 | public final class ToolBase64 { |
| 16 | private ToolBase64() { |
| 17 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * 功能:编码字符串 |
| 22 | * |
| 23 | * @param data |
| 24 | * 源字符串 |
| 25 | * |
| 26 | * @return String |
| 27 | */ |
| 28 | public static String encode(String data) { |
| 29 | return new String(encode(data.getBytes())); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * 功能:解码字符串 |
| 34 | * |
| 35 | * @param data |
| 36 | * 源字符串 |
| 37 | * |
| 38 | * @return String |
| 39 | */ |
| 40 | public static String decode(String data) { |
| 41 | return new String(decode(data.toCharArray())); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * 功能:编码byte[] |
| 46 | * |
| 47 | * @param data |
| 48 | * 源 |
| 49 | * |
| 50 | * @return char[] |
| 51 | */ |
| 52 | public static char[] encode(byte[] data) { |
| 53 | char[] out = new char[((data.length + 2) / 3) * 4]; |
| 54 | for (int i = 0, index = 0; i < data.length; i += 3, index += 4) { |
| 55 | boolean quad = false; |
| 56 | boolean trip = false; |
| 57 | |
| 58 | int val = (0xFF & (int) data[i]); |
| 59 | val <<= 8; |
| 60 | if ((i + 1) < data.length) { |
| 61 | val |= (0xFF & (int) data[i + 1]); |
| 62 | trip = true; |
| 63 | } |
| 64 | val <<= 8; |
| 65 | if ((i + 2) < data.length) { |
| 66 | val |= (0xFF & (int) data[i + 2]); |
| 67 | quad = true; |
| 68 | } |
| 69 | out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)]; |
| 70 | val >>= 6; |
| 71 | out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)]; |
| 72 | val >>= 6; |
nothing calls this directly
no outgoing calls
no test coverage detected