博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA AES加密算法实现代码
阅读量:4493 次
发布时间:2019-06-08

本文共 2735 字,大约阅读时间需要 9 分钟。

1、代码

package com.zhaochao.utill;import java.io.UnsupportedEncodingException;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;import javax.crypto.spec.SecretKeySpec;public class AESUitl {	public static byte[] encrypt(String content, String password) {		try {			KeyGenerator kgen = KeyGenerator.getInstance("AES");			kgen.init(128, new SecureRandom(password.getBytes()));			SecretKey secretKey = kgen.generateKey();			byte[] enCodeFormat = secretKey.getEncoded();			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");			Cipher cipher = Cipher.getInstance("AES");// 创建密码器			byte[] byteContent = content.getBytes("utf-8");			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化			byte[] result = cipher.doFinal(byteContent);			return result; // 加密		} catch (NoSuchAlgorithmException e) {			e.printStackTrace();		} catch (NoSuchPaddingException e) {			e.printStackTrace();		} catch (InvalidKeyException e) {			e.printStackTrace();		} catch (UnsupportedEncodingException e) {			e.printStackTrace();		} catch (IllegalBlockSizeException e) {			e.printStackTrace();		} catch (BadPaddingException e) {			e.printStackTrace();		}		return null;	}	public static byte[] decrypt(byte[] content, String password) {		try {			KeyGenerator kgen = KeyGenerator.getInstance("AES");			kgen.init(128, new SecureRandom(password.getBytes()));			SecretKey secretKey = kgen.generateKey();			byte[] enCodeFormat = secretKey.getEncoded();			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");			Cipher cipher = Cipher.getInstance("AES");// 创建密码器			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化			byte[] result = cipher.doFinal(content);			return result; // 加密		} catch (NoSuchAlgorithmException e) {			e.printStackTrace();		} catch (NoSuchPaddingException e) {			e.printStackTrace();		} catch (InvalidKeyException e) {			e.printStackTrace();		} catch (IllegalBlockSizeException e) {			e.printStackTrace();		} catch (BadPaddingException e) {			e.printStackTrace();		}		return null;	}	public static void main(String[] rags) {		String content = "test2222";		String password = "12345678";		// 加密		System.out.println("加密前:" + content);		byte[] encryptResult = encrypt(content, password);		System.out.println("加密后:"+new String(encryptResult));		// 解密		byte[] decryptResult = decrypt(encryptResult, password);		System.out.println("解密后:" + new String(decryptResult));	}}

2、输出结果

加密前:test2222加密后:��»�4�s-�?�D�H�解密后:test2222

转载于:https://www.cnblogs.com/whzhaochao/p/5023416.html

你可能感兴趣的文章
《程序是怎样跑起来的》第三章
查看>>
Jquery回到顶部效果
查看>>
开园第一笔
查看>>
Spark项目之电商用户行为分析大数据平台之(七)数据调研--基本数据结构介绍...
查看>>
Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列
查看>>
Delphi APP 開發入門(十)REST Client 開發
查看>>
用包来组织模型
查看>>
ORA-29857: 表空间中存在域索引和/或次级对象
查看>>
LeetCode58 Length of Last Word
查看>>
Python基础语法 系统学习
查看>>
ios开发之数据的持久化存储机制
查看>>
poj 3264
查看>>
图标跟着摄像机(Camera)orthographicSize的值改变大小
查看>>
LeetCode 386——字典序排数
查看>>
linux如何挂载windows下的共享文件
查看>>
常用正则表达式
查看>>
Houdini 节点参数读取输入节点的数据列表
查看>>
初识Linq to Entity
查看>>
Linux vmstat命令实战详解
查看>>
FastDFS在centos上的安装配置与使用
查看>>