Desutil.java on the network on the encryption and decryption of Chinese characters and garbled problems

Source: Internet
Author: User
Tags decrypt
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.