commons-codec的一些应用

TestCodecDemo.java代码:
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

public class TestCodecDemo {

	/**
	 * 把密码进行MD5加密
	 * */
	private static String getMd5Pwd(String password){
		String returnStr = DigestUtils.md5Hex(password);
		System.out.println(returnStr);
		return DigestUtils.md5Hex(returnStr);
	}
	
	/**
	 * 把密码进行SHA1加密
	 * */
	private static String getSha1Pwd(String password){
		String returnStr = DigestUtils.shaHex(password);
		System.out.println(returnStr);
		return returnStr;
	}
	
	/**
	 * 把密码进行BASE64加密
	 * */
	private static String getBase64Pwd(String password){
		byte[] b = Base64.encodeBase64(password.getBytes(), true);
		String returnStr = new String(b);
		System.out.println(returnStr);
		return returnStr;
	}
	
	/**
	 * 把密码进行BASE64解密
	 * */
	private static String getUnBase64Pwd(String password){
		byte[] b = Base64.decodeBase64(password.getBytes());
		String returnStr = new String(b);
		System.out.println(returnStr);
		return returnStr;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String password = "abc";
		TestCodecDemo.getMd5Pwd(password);
		TestCodecDemo.getSha1Pwd(password);
		TestCodecDemo.getBase64Pwd(password);
		TestCodecDemo.getUnBase64Pwd("YWJj");
	}

}

猜你喜欢

转载自shihuan830619.iteye.com/blog/1160733