Java AES加密
Java AES 加密
- 加密
/**
*
* @description 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @return
*/
public static byte[] encrypt(String content, String password) {
try {
//创建AES密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥长度
keyGenerator.init(128, new SecureRandom(password.getBytes()));
//生成一个密钥
SecretKey secretKey = keyGenerator.generateKey();
//编码的密钥,如果此密钥不支持编码,则返回 null
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;
}
- 解密
/**
*
* @description 解密
*
* @param content 待解密内容
* @param password 解密密钥
* @return
*/
public static byte[] decryptFrom(byte[] content, String password) {
try {
//创建AES密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
//使用用户提供的随机源初始化此密钥生成器,使其具有确定的密钥长度
keyGenerator.init(128, new SecureRandom(password.getBytes()));
//生成一个密钥
SecretKey secretKey = keyGenerator.generateKey();
//编码的密钥,如果此密钥不支持编码,则返回 null
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[] args) {
String content = "7654321 ";
String password = "a.7?6#5@4!3^1%2";
byte[] encryptResult = encrypt(content,pa
System.out.println(encryptResult);
byte[] decryptResult = decryptFrom(ecryptResult, password);
System.out.println("解密后:" + new String(decryptResult));
}
Java AES加密的更多相关文章
- C# 实现 JAVA AES加密解密[原创]
以下是网上普遍能收到的JAVA AES加密解密方法. 因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类.无法使用安全随机数生成KEY. 我们在接收J ...
- java AES加密、解密(兼容windows和linux)
java AES加密.解密 CreationTime--2018年7月14日10点06分 Author:Marydon 1.准备工作 updateTime--2018年8月10日15点28分 up ...
- 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密
你真的了解字典(Dictionary)吗? 从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...
- Java aes加密C#解密的取巧方法
摘要: 项目开发过程中遇到一个棘手的问题:A系统使用java开发,通过AES加密数据,B系统使用C#开发,需要从A系统获取数据,但在AES解密的时候遇到麻烦.Java的代码和C#的代码无法互通. Ja ...
- Java AES加密案例
AES加密原理 http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html PHP 加密 https://segmentfault. ...
- C#与Java AES 加密解密
参考文档:https://www.cnblogs.com/xbzhu/p/7064642.html 前几天对接Java接口,需要C#加密参数,Java解密.奈何网上找了一堆大同小异的加解密方法都跟Ja ...
- JAVA AES加密解密
import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java ...
- Java AES 加密工具类
package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...
- Java AES加密解密工具 -- GUI 、在线传输文件
原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下: (1)将128位AES ...
随机推荐
- Mysql 下 Insert、Update、Delete、Order By、Group By注入
Insert: 语法:INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) 报错注入: insert into test(id,name,p ...
- Quailty and Binary Operation
Quailty and Binary Operation 题意 分别给\(N,M(N,M \le 50000)\)两个数组\(A\)和\(B\),满足\(0 \le A_i,B_i \le 50000 ...
- URAL(timus)1709 Penguin-Avia(并查集)
Penguin-Avia Time limit: 1.0 secondMemory limit: 64 MB The Penguin-Avia airline, along with other An ...
- HMTL—表单
<body> <form> 账号:<input type="text" value="123" /> <br /> ...
- 移动端动画使用transform提升性能
在移动端做动画,对性能要求较高而通常的改变margin属性是性能极低的,即使使用绝对定位改变top,left这些属性性能也很差因此应该使用transform来进行动画效果,如transform:tra ...
- Git入门指南十一:Git branch 分支与合并分支
十五. Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master ...
- WebForm 中的页面重定向和传值(转自 MSDN)
——原文地址:https://msdn.microsoft.com/zh-cn/library/6c3yckfw(v=vs.100).aspx 在开发 ASP.NET 网站时,您经常需要从一 ...
- git checkout 和 git checkout --merge <branch_name>使用
一.git checkout //查看当前分支$ git branch master *t2 testing //checkout会覆盖当前工作区文件和覆盖暂存区内容,所以发现分支有未提交的警告,执行 ...
- java DecimalFormat
public class Test{ public static void main(String[] args) throws Exception{ /*DecimalFormat参数,如果是0则会 ...
- perform-two-phase-commits/
https://docs.mongodb.com/manual/tutorial/perform-two-phase-commits/