package cn;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
/**
* 本程序生成一个AES密钥,并且转换它为RAW字节,然后根据这个密钥重新创建一个AES密钥,这个新构建的密钥用于初始化一个加密解密的AES密码
*/
public class AES {
public static String aes_encrypt(String password, String strKey) {
try {
byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] cleartext = password.getBytes("UTF-8");
byte[] ciphertextBytes = cipher.doFinal(cleartext);
return new String(Hex.encodeHex(ciphertextBytes)).toUpperCase();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} return null;
}
public static String aes_decrypt(String encryptResult, String strKey) {
try {
byte[] keyBytes = Arrays.copyOf(strKey.getBytes("ASCII"), 16);
SecretKey key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] cleartext = Hex.decodeHex(encryptResult.toCharArray());
byte[] ciphertextBytes = cipher.doFinal(cleartext);
return new String(ciphertextBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (DecoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return null;
}
public static void main(String[] args) {
String content = "password";
String password = "0123456789012345";
// 加密
System.out.println("加密前:" + content);
String encryptResult = aes_encrypt(content, password);
System.out.println("加密后:" + encryptResult);
// 解密
String decryptResult = aes_decrypt(encryptResult, password);
System.out.println("解密后:" + decryptResult);
//SELECT HEX(aes_encrypt("password", "0123456789012345"));
//SELECT aes_decrypt(UNHEX("E75C7C56AFB3EA4360A9856456F1C8A2"), "0123456789012345");
}
}
mysql的
//SELECT HEX(aes_encrypt("password", "0123456789012345"));
//SELECT aes_decrypt(UNHEX("E75C7C56AFB3EA4360A9856456F1C8A2"), "0123456789012345");
参考 http://stackoverflow.com/questions/4888176/java-function-for-encrypt-decrypt-like-mysqls-aes-encrypt-and-aes-decrypt
2012年9月04日 19:14