AES加密解密 Java中运用
2024-09-01 03:37:42
AES全称 Advanced Encryption Standard, 高级加密算法,更加安全,可取代DES。
Aes:
package com.blog.d201706.encrypt; import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key; public class Aes { /**
* 加解密key
*/
private final Key keySpec; /**
* 构造函数
* @param key
*/
public Aes(String key){
keySpec = new SecretKeySpec(key.getBytes(), "AES");
} /**
* 加密
* @param str
* @return
*/
public String encryt(String str) {
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
try {
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
return parseByte2HexStr(cipherByte);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 解密
* @param deCodeStr
* @return
*/
public String decrypt(String deCodeStr) {
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
try {
if (null == deCodeStr) return null;
byte[] buff = parseHexStr2Byte(deCodeStr);
Cipher c;
c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, keySpec);
byte[] cipherByte = c.doFinal(buff);
return new String(cipherByte);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} /**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1) return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
MainAes:
package com.blog.d201706.encrypt; public class MainAes {
public static void main(String[] args) throws Exception {
Aes aes = new Aes("1234567890123456");
String msg = "测试文本:今天周5" + System.currentTimeMillis();
String encontent = aes.encryt(msg);
String decontent = aes.decrypt(encontent);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + encontent);
System.out.println("解密后:" + decontent);
} }
最新文章
- QQ邮箱发送邮件,出现mail from address must be same as authorization user错误
- sdk manager更新超时 time out
- hibernate 配置文件
- [Swift]LeetCode134. 加油站 | Gas Station
- centos6.7 配置外网端口映射
- python note 16 re模块的使用
- MATLAB一元线性回归分析
- HTML 5 Web 音频
- 419. Battleships in a Board 棋盘上的战舰数量
- 使用python爬虫爬取股票数据
- ChinaCock界面控件介绍-TCCImageViewerForm
- Jmeter使用流程及简单分析监控
- tensorflow进阶篇-4(损失函数2)
- ICC2 常用命令
- os模块学习+open行数
- POJ 2823 Sliding Window(单调队列 || 线段树)题解
- 使用kubernetes的deployment进行RollingUpdate
- swift中UITextView的使用
- Linux fdisk命令详解[主分区/逻辑分区创建]
- 关于Cooperation.GTST
热门文章
- git远程删除分支后,本地执行git branch -a依然能看到删除分支到底该咋整?
- Session技术入门代码案例
- 日志框架之Logger
- [Err] ORA-00942: table or view does not exist
- 阶段3 3.SpringMVC&#183;_02.参数绑定及自定义类型转换_7 获取Servlet原生的API
- springboot中静态属性/静态方法从YAML(yml)读取配置属性
- 如何从项目中移除CocoaPods
- 让nginx 的ssi支持include相对路径
- jQuery I
- PJzhang:从js文件中寻找子域名的SubDomainizer