提交 | 用户 | age
|
5c5945
|
1 |
package com.hx.util; |
E |
2 |
import sun.misc.BASE64Decoder; |
|
3 |
import sun.misc.BASE64Encoder; |
|
4 |
|
|
5 |
/** |
|
6 |
* BASE64加密解密 |
|
7 |
*/ |
|
8 |
public class BASE64 |
|
9 |
{ |
|
10 |
|
|
11 |
/** |
|
12 |
* BASE64解密 |
|
13 |
* @param key |
|
14 |
* @return |
|
15 |
* @throws Exception |
|
16 |
*/ |
|
17 |
public static byte[] decryptBASE64(String key) { |
|
18 |
byte[] t = new byte[]{}; |
|
19 |
try{ |
|
20 |
t = (new BASE64Decoder()).decodeBuffer(key); |
|
21 |
}catch (Exception e) { |
|
22 |
e.printStackTrace(); |
|
23 |
} |
|
24 |
return t; |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* BASE64加密 |
|
29 |
* @param key |
|
30 |
* @return |
|
31 |
* @throws Exception |
|
32 |
*/ |
|
33 |
public static String encryptBASE64(byte[] key){ |
|
34 |
String s = ""; |
|
35 |
try{ |
|
36 |
s = (new BASE64Encoder()).encodeBuffer(key); |
|
37 |
}catch (Exception e) { |
|
38 |
e.printStackTrace(); |
|
39 |
} |
|
40 |
return s; |
|
41 |
} |
|
42 |
|
|
43 |
|
|
44 |
public static void main(String[] args) throws Exception |
|
45 |
{ |
|
46 |
String data = BASE64.encryptBASE64("123456789".getBytes()); |
|
47 |
System.out.println("加密前:"+data); |
|
48 |
byte[] byteArray = BASE64.decryptBASE64(data); |
|
49 |
System.out.println("解密后:"+new String(byteArray)); |
|
50 |
} |
|
51 |
} |