AES加密,128-192-256,方案二

与方案一对比,方案二要简洁很多。

看代码:

package com.xiao.aes.util;

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

/**
 *
 * 美国软件出口限制,JDK默认使用的AES算法最高只能支持128位。如需要更高的支持需要从oracle官网下载更换JAVA_HOME/jre/lib/
 * security目录下的: local_policy.jar和US_export_policy.jar。

 * 采用补码方式以及base64双重加密,依赖commons-codec-1.x.jar包中的base64加密
 *
 * @author xiao
 *
 */
public class AESUtil2 {

 /**
  *
  */
 private static Cipher cipher;

 /**
  * 初始化向量
  */
 private static IvParameterSpec iv;

 static {
  try {
   byte[] vi = Hex.decodeHex("12345678123456781234567812345678"
     .toCharArray());
   iv = new IvParameterSpec(vi);
   // "算法/模式/补码方式"
   cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (NoSuchPaddingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (DecoderException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 /**
  * 生成密钥
  *
  * @param type
  *            AES长度
  * @return 密钥
  */
 public static String createAESKey(AESType type) {
  try {
   KeyGenerator key = KeyGenerator.getInstance("AES");
   key.init(type.value);
   SecretKey ckey = key.generateKey();
   byte[] keyByte = ckey.getEncoded();
   return Base64.encodeBase64String(keyByte);
  } catch (NoSuchAlgorithmException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "";
 }

 /**
  * AES加密
  *
  * @param key
  *            密钥
  * @param plaintext
  *            明文
  * @return 秘文
  */
 public static String encryptAES(String key, String plaintext) {
  String ciphertext = "";
  try {
   byte[] keyByte = Base64.decodeBase64(key);
   SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
   // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
   cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
   byte[] pbyte = plaintext.getBytes("utf-8");
   byte[] result = cipher.doFinal(pbyte);
   ciphertext = Base64.encodeBase64String(result);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return ciphertext;
 }

 /**
  * 解密
  *
  * @param key
  *            密钥
  * @param ciphertext
  *            秘文
  * @return 明文
  */
 public static String decryptAES(String key, String ciphertext) {
  String plaintext = "";
  try {
   byte[] keyByte = Base64.decodeBase64(key);
   byte[] cbyte = Base64.decodeBase64(ciphertext);
   SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "AES");
   // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
   cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
   byte[] pbyte = cipher.doFinal(cbyte);
   plaintext = new String(pbyte, "utf-8");
  } catch (Exception e) {
   e.printStackTrace();
  }
  return plaintext;
 }

 public static void main(String[] args) {
  String key128 = createAESKey(AESType.AES_128);
  String key192 = createAESKey(AESType.AES_192);
  String key256 = createAESKey(AESType.AES_256);
  System.out.println("Key 128:" + key128);
  System.out.println("Key 192:" + key192);
  System.out.println("Key 256:" + key256);

  String source = "AES加密测试";

  String ciphertext128 = encryptAES(key128, source);
  String ciphertext192 = encryptAES(key192, source);
  String ciphertext256 = encryptAES(key256, source);
  System.out.println("Ciphertext 128:" + ciphertext128);
  System.out.println("Ciphertext 192:" + ciphertext192);
  System.out.println("Ciphertext 256:" + ciphertext256);

  String plaintext128 = decryptAES(key128, ciphertext128);
  String plaintext192 = decryptAES(key192, ciphertext192);
  String plaintext256 = decryptAES(key256, ciphertext256);
  System.out.println("Plaintext 128:" + plaintext128);
  System.out.println("Plaintext 192:" + plaintext192);
  System.out.println("Plaintext 256:" + plaintext256);
 }
}



你可能感兴趣的:(aes,java,加密)