JAVA实现AES机密解密算法

本文详细介绍了一种使用Java实现AES加密和解密的方法。通过具体代码示例,展示了如何生成密钥、加密字符串以及解密过程。适用于需要在Java环境中进行数据安全保护的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.cn.ssm.test;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
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;

public class AESTest {
	
	static final String ALGORITHM = "AES";
	static final String CHARSET_NAME = "utf-8";
	static Charset charset = Charset.forName(CHARSET_NAME);
	
	public static SecretKey generateKay() throws NoSuchAlgorithmException {
		
		KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom secureRandom = new SecureRandom();
		keyGenerator.init(secureRandom);
		SecretKey secretKey = keyGenerator.generateKey();
		
		return secretKey;
		
	}
	
	public static byte[] encrypt(String content, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
		return aes(content.getBytes(charset), Cipher.ENCRYPT_MODE, secretKey);
		
	}
	
	public static String decrypt(byte[] contentArray, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		
		byte[] result = aes(contentArray, Cipher.DECRYPT_MODE, secretKey);
		return new String(result,CHARSET_NAME);
		
	}
	
	public static byte[] aes(byte[] contentArray, int mode, SecretKey secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(mode, secretKey);
		byte[] result = cipher.doFinal(contentArray);
		
		return result;
		
	}
	
	public static void main(String[] args) {
		
		String content = "hshj加密解密你好啊377843843";
		try {
			
			SecretKey secretKey = generateKay();
			byte[] encryptResult = encrypt(content, secretKey);
			String decryptResult = decrypt(encryptResult, secretKey);
			
			System.out.println("加密后的结果==》"+encryptResult);
			System.out.println("解密后的结果==》"+decryptResult);
			
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值