apache commons工具类之 codec

Codec 提供了一些公共的编解码实现,比如Base64,Base32,Md5Crypt,Crypt等等。
package commons;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.Md5Crypt;

public class TestBase64 {

	// Base64编码
	public static String encodeTest(String str) {
		Base64 base64 = new Base64();
		try {
			str = base64.encodeToString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		System.out.println("Base64 编码后:" + str);
		return str;
	}
    
	// Base64解码
	public static void decodeTest(String str) {
		str = new String(Base64.decodeBase64(str));
		System.out.println("Base64 解码后:" + str);
	}
    
	
	@SuppressWarnings("static-access")
	public static void main(String[] args) throws UnsupportedEncodingException {
		decodeTest(encodeTest("123456"));
		Md5Crypt md5Crypt = new Md5Crypt();
		System.out.println(md5Crypt.md5Crypt("123456".getBytes("UTF-8")));
		System.out.println(md5Crypt.apr1Crypt("123456".getBytes("UTF-8")));
	}

}



运行结果
Base64 编码后:MTIzNDU2
Base64 解码后:123456
$1$t8PuI1u/$7B.krpsbywf6cDFYsb6RT0
$apr1$nl6D999g$ahRrFwNuW3V5gFfs.WS8P/

猜你喜欢

转载自caoshichuan.iteye.com/blog/2311856