AES 加密解密的JAVA实现方式【一】已调通

使用环境  JDK 1.8

这种方法密钥必须是16位的,偏移量也是16位。

 

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

 

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *<b>AES 加密/解密 </b><br>
 * <B>注意:</B><br>
 * 调用方法参见main方法<br>
 *
 */
public class AESUtil {

	public static  String key = "ThisIsMytestKey1";
	public static  String iv  = "1234567890123456"; 

	/**
	 * 偏移量获取
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private static IvParameterSpec getIV() throws UnsupportedEncodingException {
		return new IvParameterSpec(iv.getBytes());
	}

	/**
	 * 密钥生成
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws NoSuchAlgorithmException
	 */
	public static SecretKeySpec getKey()   {
 		SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
		return keySpec;
	}

	/**
	 * 解密
	 * 
	 * @param content
	 * @return
	 */
	public static String decrypt(String src) {
		try {

			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, getKey(), getIV());
			byte[] decodedByte = new BASE64Decoder().decodeBuffer(src);
			byte[] original = cipher.doFinal(decodedByte);
			String result = new String(original);
			return result;
		} catch(IOException e){
			System.out.println(e.toString());
		} catch (IllegalBlockSizeException e) {
			System.out.println(e.toString());
		} catch (BadPaddingException e) {
			System.out.println(e.toString());
		} catch (InvalidKeyException e) {
			System.out.println(e.toString());
		} catch (InvalidAlgorithmParameterException e) {
			System.out.println(e.toString());
		} catch (NoSuchAlgorithmException e) {
			System.out.println(e.toString());
		} catch (NoSuchPaddingException e) {
			System.out.println(e.toString());
		};

		return null;
	}

	/**
	 * 加密
	 * 
	 * @param content
	 * @return
	 */
	public static String encrypt(String content) {

		try {

			Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, getKey(), getIV());
			byte[] contentByte = content.getBytes();
			byte[] result = cipher.doFinal(contentByte);
			return new BASE64Encoder().encode(result);
		} catch (IllegalBlockSizeException e) {
			System.out.println(e.toString());
		} catch (BadPaddingException e) {
			System.out.println(e.toString());
		} catch (InvalidKeyException e) {
			System.out.println(e.toString());
		} catch (InvalidAlgorithmParameterException e) {
			System.out.println(e.toString());
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.toString());
		} catch (NoSuchAlgorithmException e) {
			System.out.println(e.toString());
		} catch (NoSuchPaddingException e) {
			System.out.println(e.toString());
		};

		return null;
	}

	 
	/**
	 * 调用的测试主方法
	 * @param args
	 * @throws UnsupportedEncodingException
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		String content = "欲穷千里目,更上一层楼";
		// 加密
		System.out.println("加密前:" + content);
		String resultString = AESUtil.encrypt(content);
		System.out.println("加密后:" + resultString);
		// 解密
		String decryptResult = AESUtil.decrypt(resultString);
		System.out.println("解密后:" + decryptResult);
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安优小青和他的程序生活

我的文档对您有很大的帮助吗?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值