代码来自以下链接
http://www.oschina.net/code/piece_full?code=18383#30455

首先声明的是,代码不是本人写的,感谢一下作者,我的是一个JavaWeb项目,与服务器servlet接口通信的项目时,对协议作了des+base64加密处理,使用了该类,
期间遇到了一个比较纠结的问题,就是如果汉字存在于协议中,服务器加密,客户端解密会出现乱码,客户端加密,服务器解密亦然。


以上问题的解决其实很简单,只不过找问题原因的过程有点浪费时间,所以记录在此,以告知其他有缘也用到该类的朋友,能够节省一点宝贵的时间。

修改代码如下:

 1 /**
 2      * Description 根据键值进行加密
 3      * 
 4      * @param data
 5      * @param key
 6      *            加密键byte数组
 7      * @return
 8      * @throws Exception
 9      */
10     public static String encrypt(String data, String key) throws Exception {
11         byte[] bt = encrypt(data.getBytes("UTF-8"), key.getBytes());
12         String strs = new BASE64Encoder().encode(bt);
13         return strs;
14     }
15 
16     /**
17      * Description 根据键值进行解密
18      * 
19      * @param data
20      * @param key
21      *            加密键byte数组
22      * @return
23      * @throws IOException
24      * @throws Exception
25      */
26     public static String decrypt(String data, String key) throws IOException,
27             Exception {
28         if (data == null)
29             return null;
30         BASE64Decoder decoder = new BASE64Decoder();
31         byte[] buf = decoder.decodeBuffer(data);
32         byte[] bt = decrypt(buf,key.getBytes());
33         return new String(bt, "UTF-8");
34     }

注意:修改的两处为"UTF-8"的位置。