`

BaseCodecUtils

阅读更多
/**
 * 
 */
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:lineagezd@163.com 
 * 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) {
			
		}
	}

}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics