AES-128使用java实现加解密

AES的介绍

AES:高级加密标准(Advanced Encryption Standard),是一种常见的对称加解密技术,加密和解密用的是同一个密钥,因此这个密钥必须保存好,不要泄漏。一般是线下与调用方约定好固定的密钥串,或者服务器端用RSA来加密AES的密钥后返回给前端使用,前端施一公RSA公钥来解密获取AES的密钥。

AES的原理

请求发起方,通过密钥key+明文content,使用加密函数加密出密文secreContent,把密文发送给接收方,接收方用密钥key和密文传入解密函数解密得出原始明文content。大致流程如下:

下面是基于AES-128实现的java AES加解密,上代码:

package com.lsk.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.UUID;

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; 

public class AesUtil {
     
	private final static String AES = "AES";
	private final static String UTF8 = "UTF-8";
	//定义一个16byte的初始向量
	private static final String IV_STRING = "Linsk110011ksniL";
	/**
	 * 	产生一个16位的密钥字符串
	 * @return
	 */
	public static String generateSecreKey() {
     
		String uuid = UUID.randomUUID().toString();
		uuid = uuid.replaceAll("-", "");
		return uuid.substring(0, 16);
	}
	public static String aesEncry(String content,String key) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
     
		byte[] contentByte = content.getBytes(UTF8);
		byte[] keyByte = key.getBytes();
		//初始化一个密钥对象
		SecretKeySpec keySpec = new SecretKeySpec(keyByte ,AES);
		//初始化一个初始向量,不传入的话,则默认用全0的初始向量
		byte[] initParam = IV_STRING.getBytes();
		IvParameterSpec ivSpec = new IvParameterSpec(initParam);
		// 指定加密的算法、工作模式和填充方式
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);
		byte[] encryptedBytes = cipher.doFinal(contentByte);
		String encodedString = Base64.getEncoder().encodeToString(encryptedBytes);
		return encodedString;
	}
	
	public static String aesDecry(String content,String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
     
		byte[] contentByte = Base64.getDecoder().decode(content);
		byte[] keyByte = key.getBytes();
		//初始化一个密钥对象
		SecretKeySpec keySpec = new SecretKeySpec(keyByte ,AES);
		//初始化一个初始向量,不传入的话,则默认用全0的初始向量
		byte[] initParam = IV_STRING.getBytes();
		IvParameterSpec ivSpec = new IvParameterSpec(initParam);
		// 指定加密的算法、工作模式和填充方式
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, keySpec,ivSpec);
		byte[] result  = cipher.doFinal(contentByte);
		return new String(result,UTF8);
	}

	public static void main(String[] args) throws Exception{
     
		// TODO Auto-generated method stub
		String secrekey = generateSecreKey();
		System.out.println("secrekey="+secrekey);
		String sourceStr = "Linsk123456";
		String secreStr = aesEncry(sourceStr,secrekey);
		System.out.println("secreStr="+secreStr);
		String decodeStr = aesDecry(secreStr,secrekey);
		System.out.println("decodeStr="+decodeStr);
	}
}

输出结果:
secrekey=2983ce249a2a4e5b
secreStr=cqRG3WhpsGbV7oOT8wSnfw==
decodeStr=Linsk123456

你可能感兴趣的:(AES-128加解密,java)