AES加密算法的实现

AES简介

高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法。对称加密算法也就是加密和解密需要使用相同的密钥。

应用场景:接口之间的数据传输

需要用到的包:commons-codec-1.5.jar(其他版本的也可以)

代码实现

package com.common.utils.aes;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
/**
 * AES加密工具类
 * @author zhh
 * 2018-12-20
 */
public class AesEncryptUtil {
	
	public static void main(String[] args) throws Exception {
		// 秘钥
		String encodeRules = "8866";
		String content = "[中文名:漠然、英文名:moran、电话:18801258888]";
		
		String ciphertext = AESEncode(encodeRules, content);
		String plaintext = AESDncode(encodeRules, ciphertext);
		
		System.out.println("明文:"+content);
		System.out.println("密文:"+ciphertext);
		System.out.println("解密后的明文:"+plaintext);
	}

    /**
	 * AES根据密钥加密
	 * @param encodeRules	密钥
	 * @param content		需要加密的内容
	 * @return
     * @throws NoSuchAlgorithmException 
     * @throws NoSuchPaddingException 
     * @throws NoSuchProviderException 
     * @throws InvalidKeyException 
     * @throws UnsupportedEncodingException 
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
	 */
	public static String AESEncode(String encodeRules,String content) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
		//1.构造密钥生成器,指定为AES算法,不区分大小写
		KeyGenerator keygen=KeyGenerator.getInstance("AES");
		//2.根据ecnodeRules规则初始化密钥生成器
		//生成一个128位的随机源,根据传入的字节数组
		SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
		random.setSeed(encodeRules.getBytes());
		keygen.init(128, random);
		//3.产生原始对称密钥
		SecretKey original_key=keygen.generateKey();
		//4.获得原始对称密钥的字节数组
		byte [] raw=original_key.getEncoded();
		//5.根据字节数组生成AES密钥
		SecretKey key=new SecretKeySpec(raw, "AES");
		//6.根据指定算法AES自成密码器
	    Cipher cipher=Cipher.getInstance("AES");
		//7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY
		cipher.init(Cipher.ENCRYPT_MODE, key);
		//8.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
		byte [] byte_encode=content.getBytes("utf-8");
		//9.根据密码器的初始化方式--加密:将数据加密
		byte [] byte_AES=cipher.doFinal(byte_encode);
		//10.将加密后的数据转换为字符串
		//解决办法:
		return new String(Base64.encodeBase64(byte_AES));
	}
	
	/**
	 * AES根据密钥解密
	 * @param encodeRules	密钥
	 * @param content		密文
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 * @throws NoSuchPaddingException 
	 * @throws NoSuchProviderException 
	 * @throws BadPaddingException 
	 * @throws IllegalBlockSizeException 
	 * @throws InvalidKeyException 
	 */
	public  static String AESDncode(String encodeRules,String content) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
		//1.构造密钥生成器,指定为AES算法,不区分大小写
		KeyGenerator keygen=KeyGenerator.getInstance("AES");
		//2.根据ecnodeRules规则初始化密钥生成器
		//生成一个128位的随机源,根据传入的字节数组
		SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
		random.setSeed(encodeRules.getBytes());
		keygen.init(128, random);
		//3.产生原始对称密钥
		SecretKey original_key=keygen.generateKey();
		//4.获得原始对称密钥的字节数组
		byte [] raw=original_key.getEncoded();
		//5.根据字节数组生成AES密钥
		SecretKey key=new SecretKeySpec(raw, "AES");
		//6.根据指定算法AES自成密码器
		Cipher cipher=Cipher.getInstance("AES");
		//7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作,第二个参数为使用的KEY
		cipher.init(Cipher.DECRYPT_MODE, key);
		//8.将加密并编码后的内容解码成字节数组
		byte [] byte_content= Base64.decodeBase64(content.getBytes("utf-8"));
		// 解密
		byte [] byte_decode=cipher.doFinal(byte_content);
		return new String(byte_decode,"utf-8");  
	}
}

运行结果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值