BaseCodecUtils

1673 단어 java codec

/**
 * 
 */
package com.baidu.maven.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

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

/**
 * @author Roger Zhou 
 * email:[email protected] 
 * 2013-11-6  10:08:59
 * 
 */

public class BaseCodecUtils {

	public static byte[] encryptMode(String keyword, String src)
			throws Exception {
		SecretKey deskey = new SecretKeySpec(keyword.getBytes(), "DESede");
		Cipher c1 = Cipher.getInstance("DESede");
		c1.init(1, deskey);
		return Base64.encodeBase64(c1.doFinal(src.getBytes()));
	}

	public static String md5Encrypt(String src) {
		return DigestUtils.md5Hex(src);
	}

	public static String base64Encrypt(byte[] src) {
		byte[] b = Base64.encodeBase64(src, true);
		return new String(b).trim();
	}

	public static String base64Decrypt(String desc) {
		byte[] b = Base64.decodeBase64(desc.getBytes());
		return new String(b).trim();
	}

	public static void main(String[] args) {
		try {
			byte[] b = BaseCodecUtils.encryptMode("123456789456123321654987",
					"1234567890");
			System.out.println(base64Encrypt(b));
			System.out.println(new String(b));
			System.out.println(md5Encrypt(""));
		} catch (Exception e) {
			
		}
	}

}




좋은 웹페이지 즐겨찾기