[java]
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* Copyright 2007 GuangZhou Cotel Co. Ltd.
* All right reserved.
* DES加密解密类.
* @author <a href="http://www.coder55.com/mailto:xiexingxing1121@126.com" mce_href="http://www.coder55.com/mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2007-7-31 - 上午11:59:28
*/
public class Des {
/** 加密、解密key. */
private static final String PASSWORD_CRYPT_KEY = "kEHrDooxWHCWtfeSxvDvgqZq";
/** 加密算法,可用 DES,DESede,Blowfish. */
private final static String ALGORITHM = "DES";
public static void main(String[] args) throws Exception {
String md5Password = "202cb962ac59075b964b07152d234b70";
String str = Des.encrypt(md5Password);
System.out.println("str: " + str);
str = Des.decrypt(str);
System.out.println("str: " + str);
}
/**
* 对数据进行DES加密.
* @param data 待进行DES加密的数据
* @return 返回经过DES加密后的数据
* @throws Exception
* @author <a href="http://www.coder55.com/mailto:xiexingxing1121@126.com" mce_href="http://www.coder55.com/mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:06:24
*/
public final static String decrypt(String data) throws Exception {
return new String(decrypt(hex2byte(data.getBytes()),
PASSWORD_CRYPT_KEY.getBytes()));
}
/**
* 对用DES加密过的数据进行解密.
* @param data DES加密数据
* @return 返回解密后的数据
* @throws Exception
* @author <a href="http://www.coder55.com/mailto:xiexingxing1121@126.com" mce_href="http://www.coder55.com/mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-7-31 - 下午12:07:54
*/
public final static String encrypt(String data) throws Exception {
return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY
.getBytes()));
}
/**
* 用指定的key对数据进行DES加密.
* @param data 待加密的数据
* @param key DES加密的key
* @return 返回DES加密后的数据
* @throws Exception