BASE64加解密,加密与解密实际是指编码(encode)和解码(decode)的过程,其变换是非常简单的,仅仅能够避免信息被直接识别。
Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。
Base64使用A--Z,a--z,0--9,+,/ 这64个字符.
Base64是一种很常见的编码规范,其作用是将二进制序列转换为人类可读的ASCII字符序列
Base64编码表
Value Encoding Value Encoding Value Encoding Value Encoding
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v(pad) =
14 O 31 f 48 w
15 P 32 g 49 x
16 Q 33 h 50 y
package com.encryption;
import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 *	BASE64加解密
 * @author Roger
 */
@SuppressWarnings("restriction")
public class BASE64Util {
	public static void main(String[] args) {
		
		String key = "123456789";
		String encrypt = "";
		try {
			encrypt = BASE64Util.encryptBASE64(key.getBytes("UTF-8"));
			System.out.println("encrypt : "+encrypt);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			byte[] decrypt = BASE64Util.decryptBASE64(encrypt);
			System.out.println("decrypt : "+new String(decrypt, "UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 进行BASE64加密(编码)
	 * 
	 * @param key
	 * 		加密字符
	 * @return 
	 * 		字符加密后byte[]
	 * @throws Exception
	 */
	public static byte[] decryptBASE64(String key) throws Exception {
		return (new BASE64Decoder()).decodeBuffer(key);
	}
	/**
	 * 进行BASE64解密(解码)
	 * 
	 * @param key
	 * 		解密字符byte[]
	 * @return 
	 * 		解密后字符串
	 * @throws Exception
	 */
	public static String encryptBASE64(byte[] key) throws Exception {
		return (new BASE64Encoder()).encodeBuffer(key);
	}
	
}原文:http://10960988.blog.51cto.com/10950988/1794832