package com.lot.common.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* <p>
* Title:
* </p>
* <p>
* Description: 采用AES负责字符串的加密与解密
* </p>
* <p>
* Copyright: Copyright (c) 2011 版权
* </p>
* <p>
* Company:
* </p>
*
* @author kevin
* @version V1.0
* @date 2011-5-27上午10:57:33
*/
public class SecUtil {
private static final Log logger = LogFactory.getLog(SecUtil.class);
/**
* key 可自定义
*/
private static byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x50,
0x37, 0x38, 0x39, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46 };
public static void main(String[] args) throws Exception {
BufferedReader reader;
String input;
int i = 0;
try {
String st = "";
System.out.println("AES加密与解密操作:");
System.out.println("\"E\":加密 \t\"D\":解密\t\t\"Q\":退出");
do{
if("".equals(st)) {
System.out.println("请输入操作代码:");
}
reader = new BufferedReader(new InputStreamReader(System.in));
st = reader.readLine();
if("E".equalsIgnoreCase(st)) {
System.out.println("请输入待加密字符串:");
st = reader.readLine();
if(!"".equals(st.trim())) {
System.out.println("加密前:" + st.trim());
System.out.println("加密后:" + encrypt(st.trim()) + "\n\n");
}
st = "";
}else if("D".equalsIgnoreCase(st)) {
System.out.println("请输入待解密字符串:");
st = reader.readLine();
if(!"".equals(st.trim())) {
System.out.println("解密前:" + st.trim());
System.out.println("解密后:" + decrypt(st.trim()) + "\n\n");
}
st = "";
}
} while(!st.equalsIgnoreCase("Q"));
} catch (Exception e) {
logger.error(StringHandleUtils.getExceptionInfo(e));
}
}
/**
*
* @author: kevin
* @Title encrypt
* @Time: 2011-5-27上午10:57:56
* @Description: 加密
* @return: String
* @throws:
* @param value
* @return
*/
public static String encrypt(String value) {
String s = null;
int mode = Cipher.ENCRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher.doFinal(value.getBytes());
s = String.valueOf(Hex.encodeHex(outBytes));
} catch (Exception e) {
logger.error(StringHandleUtils.getExceptionInfo(e));
}
return s;
}
/**
*
* @author: kevin
* @Title decrypt
* @Time: 2011-5-27上午10:58:09
* @Description: 解密
* @return: String
* @throws:
* @param value
* @return
*/
public static String decrypt(String value) {
String s = null;
int mode = Cipher.DECRYPT_MODE;
try {
Cipher cipher = initCipher(mode);
byte[] outBytes = cipher
.doFinal(Hex.decodeHex(value.toCharArray()));
s = new String(outBytes);
} catch (Exception e) {
logger.error(StringHandleUtils.getExceptionInfo(e));
}
return s;
}
/**
*
* @author: kevin
* @Title initCipher
* @Time: 2011-5-27上午10:58:47
* @Description: 初始化密码
* @return: Cipher
* @throws:
* @param mode
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
*/
private static Cipher initCipher(int mode) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Key key = new SecretKeySpec(keybytes, "AES");
cipher.init(mode, key);
return cipher;
}
}