The code comes from the following links
http://www.oschina.net/code/piece_full?code=18383#30455
First of all, the code is not written by me, thanks to the author, but I do an Android client, with the server Servlet interface Communication project, the protocol was DES+BASE64 encryption processing, the use of this class,
During the encounter a more tangled problem is that if the Chinese characters exist in the protocol, the server encryption, client decryption will appear garbled, client-side encryption, the server decryption is the same.
The above problem is actually very simple to solve, but the process of finding the problem is a bit of a waste of time, so record here, to tell other people are destined to use this kind of friends, can save a little precious time.
Modify the code as follows:
/**
* Description encryption based on key value
* *
@param data
* @param key
* encryption key byte array
* @return
* Throws Exception
*
/public static string encrypt (string data, String key) throws Exception {
byte[] bt = ENC Rypt (Data.getbytes ("UTF-8"), Key.getbytes ());
String STRs = new Base64encoder (). Encode (BT);
return strs;
}
/**
* Description decryption based on key value
* *
@param data
* @param key
* encryption key byte array
* @return
* @throws IOException
* @throws Exception
/public static string decrypt (string data, string key) T Hrows IOException,
Exception {
if (data = null) return
null;
Base64decoder decoder = new Base64decoder ();
byte[] buf = decoder.decodebuffer (data);
byte[] bt = Decrypt (Buf,key.getbytes ());
return new String (BT, "UTF-8");
}
Please note that the modified two places are "UTF-8".
I hope everyone has not encountered this problem.