在java开发过程中,很多时候我们都需要加密数据,例如声音、敏感信息等。我们通常使用的是 MD5加密、SHA加密、DES 加密、AES 加密等。今天我们就看看AES 加密。

问题出处

在项目中,代码写的好好的,本地测试什么都没问题,打包发布,高高兴兴的回家,第二天到公司,发现加密的数据,下载时解密失败。这什么情况,哪出了问题,汗直接流了出来。不经意间的一个想法:windows和linux 有什么差别呢?于是开始调查,有了如下的总结。

解决方法

方法一

代码如下:

    private void encryptAES1(String key, ByteArrayInputStream inputStream, ByteArrayOutputStream outputStream) throws IOException {
CipherInputStream cipherInputStream = null;
try{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom(key.getBytes(StandardCharsets.UTF_8)));
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyEncoded = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(keyEncoded, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// 解密 Cipher.DECRYPT_MODE
cipherInputStream = new CipherInputStream(inputStream, cipher);
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = cipherInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, numBytes);
}
}catch (NoSuchPaddingException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
e.printStackTrace();
} finally {
outputStream.flush();
outputStream.close();
inputStream.close();
if (null != cipherInputStream){
cipherInputStream.close();
}
}
}

此方法在windows 系统是能够加密和解密。但是在linux 系统中,加密解密会失败。原因是因为windows 和linux 内核不同,产生的随机数不同,所以导致了linux 系统加密解密失效。解决方法参照 方法二

方法二

代码如下:

    private void encryptAES2(String key, ByteArrayInputStream inputStream, ByteArrayOutputStream outputStream) throws IOException {
CipherInputStream cipherInputStream = null;
try{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes(StandardCharsets.UTF_8));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyEncoded = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(keyEncoded, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);//解密 Cipher.DECRYPT_MODE
cipherInputStream = new CipherInputStream(inputStream, cipher);
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = cipherInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, numBytes);
}
}catch (NoSuchPaddingException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
e.printStackTrace();
} finally {
outputStream.flush();
outputStream.close();
inputStream.close();
if (null != cipherInputStream){
cipherInputStream.close();
}
}
}

此方法是能够解决不同系统加密算法的差异,能够成功的实现windows、linux 系统加密解密。但是经过代码检测,发现【SecureRandom.getInstance("SHA1PRNG");】中的 SHA1PRNG 是有代码风险的。通过查找资料,发现可以替换成 NativePRNG ,但是代码书写完成后,运行失败。经过请教公司前辈们,的到 方法三 的方案。

方法三

代码如下:

    private void encryptAES3(String key, ByteArrayInputStream inputStream, ByteArrayOutputStream outputStream) throws IOException {
CipherInputStream cipherInputStream = null;
try{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(key.getBytes(StandardCharsets.UTF_8));
byte[] keyBytes = new byte[16];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);// Cipher.DECRYPT_MODE
cipherInputStream = new CipherInputStream(inputStream, cipher);
byte[] bytes = new byte[64];
int numBytes;
while ((numBytes = cipherInputStream.read(bytes)) != -1){
outputStream.write(bytes, 0, numBytes);
}
}catch (NoSuchPaddingException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
e.printStackTrace();
} finally {
outputStream.flush();
outputStream.close();
inputStream.close();
if (null != cipherInputStream){
cipherInputStream.close();
}
}
}

此方法去除了随机数,使用MessageDigest 类,可以生成和 KeyGenerator 生成相同位数的的密匙。有没有代码风险或着缺陷还没有发现,路过的朋友要是知道的化请留言告知。

补充

  • 以上三种方法中 keyGenerator.init(param1, param2) 方法中,第一个参数是产生随机数的位数,他可以是128(16字节)、192(24字节)、256(32字节)。需要加密的数据,不满16字节的,加密后会生成16字节的加密数据;等于16字节的数据,加密后会生成32字节的加密数据。所以方法三中 【byte[] keyBytes =newbyte[16];】是16。
  • 建议:若需要对加密后数据拼接的,且整个文件解密的,例如实时的声音数据、大篇幅的文本消息(分段加密)等,最好加密的数据是16的倍数,加密后减去末尾的16字节再拼接(最后一段数据加密不需要减16)。

总结

这里只是简单的介绍了AES 加密,为什么没有详细的说明DES、AES、MD5 等加密的原理和差别呢,因为我还不够了解他们的原理。感兴趣的小伙伴可以网上搜索下他们的原理和差别。

Java AES 加密小试牛刀的更多相关文章

  1. Java AES加密

    Java AES 加密 加密 /** * * @description 加密 * * @param content 需要加密的内容 * @param password 加密密码 * @return * ...

  2. C# 实现 JAVA AES加密解密[原创]

    以下是网上普遍能收到的JAVA AES加密解密方法. 因为里面用到了KeyGenerator 和 SecureRandom,但是.NET 里面没有这2个类.无法使用安全随机数生成KEY. 我们在接收J ...

  3. java AES加密、解密(兼容windows和linux)

      java AES加密.解密 CreationTime--2018年7月14日10点06分 Author:Marydon 1.准备工作 updateTime--2018年8月10日15点28分 up ...

  4. 你真的了解字典(Dictionary)吗? C# Memory Cache 踩坑记录 .net 泛型 结构化CSS设计思维 WinForm POST上传与后台接收 高效实用的.NET开源项目 .net 笔试面试总结(3) .net 笔试面试总结(2) 依赖注入 C# RSA 加密 C#与Java AES 加密解密

    你真的了解字典(Dictionary)吗?   从一道亲身经历的面试题说起 半年前,我参加我现在所在公司的面试,面试官给了一道题,说有一个Y形的链表,知道起始节点,找出交叉节点.为了便于描述,我把上面 ...

  5. Java aes加密C#解密的取巧方法

    摘要: 项目开发过程中遇到一个棘手的问题:A系统使用java开发,通过AES加密数据,B系统使用C#开发,需要从A系统获取数据,但在AES解密的时候遇到麻烦.Java的代码和C#的代码无法互通. Ja ...

  6. Java AES加密案例

    AES加密原理 http://www.blogjava.net/amigoxie/archive/2014/07/06/415503.html PHP 加密 https://segmentfault. ...

  7. C#与Java AES 加密解密

    参考文档:https://www.cnblogs.com/xbzhu/p/7064642.html 前几天对接Java接口,需要C#加密参数,Java解密.奈何网上找了一堆大同小异的加解密方法都跟Ja ...

  8. JAVA AES加密解密

    import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java ...

  9. Java AES 加密工具类

    package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.S ...

随机推荐

  1. 获取本地请求的真实IP地址

    1 /** 2 * 自定义访问对象工具类 3 * 4 * 获取对象的IP地址等信息 5 * 6 */ 7 public class CusAccessObjectUtil { 8 9 /** 10 * ...

  2. spring动态切换数据源(一)

    介绍下spring数据源连接的源码类:| 1 spring动态切换连接池需要类AbstractRoutingDataSource的源码 2 /* 3 * Copyright 2002-2017 the ...

  3. 登录用户出现‘’-bash-4.2$‘’的问题解决

    Linux系统切换用户时如显示的是-bash-4.2# 而不是user@主机名 + 路径的显示方式,以往一直用的脚本也不能执行起来: 原因是在用useradd添加普通用户时,有时会丢失家目录下的环境变 ...

  4. MySQL-SQL基础-查询2

    mysql> create table customer(mid char(5) primary key,th date,sex char(1) default '0'); Query OK, ...

  5. Spring Security进阶

    Spring Security进阶 1.连接数据库进行数据的验证 Spring Security进行身份验证或者权限控制时,用户名和密码应该要和数据库的进行比较才行,用户的各种信息我们从数据库中去获取 ...

  6. WebService学习总结(五)--CXF的拦截器

    拦截器是Cxf的基础,Cxf中很多的功能都是由内置的拦截器来实现的,拦截器在Cxf中由Interceptor表示.拦截器的作用类似axis2中handle.Cxf的拦截器包括入拦截器和出拦截器,所有的 ...

  7. 缩减Centos7xfs磁盘空间

    问题描述:df -h查看 root目录仅有20G空间,其余300G空间全在home目录下.xfs不可以直接缩减,所以只能删除xfs盘然后重新添加. 解决办法: 1. 注释想要删除的磁盘,此处以cent ...

  8. Python脚本运行出现语法错误:IndentationError:unexpected indent

    对于py来说典型错误就是缩进,,烦不胜烦,整理一下解决方法:一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: Indentation ...

  9. 在 vue-cli 项目中 使用elementUI 的“自定义主题”功能

    1.安装elementUI $ npm i element-ui -S 2.安装主题工具 npm i element-theme -g 3.安装chalk主题 npm 安装 npm i element ...

  10. 痞子衡嵌入式:嵌入式Cortex-M中断向量表对齐原则的深入研究

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是Cortex-M中断向量表对齐原则. 今天这篇文章的内容主要来自于五年前做 Kinetis K32W 系列双核启动时的发现,最近正好有同 ...