[JavaSecurity] - AES Encryption
1. AES Algorithm
- The Advanced Encryption Standard (AES), also as known as Rijndael (its original name), is a specification for encryption of electronic data established by the U.S. National Institute of Standard and Technology (NIST) in 2001.
- It uses a fixed long key to encrypt and decrypt data, available key size, 128, 192 and 256 bits.
- Use case: A want to send a message to friend B, and A does not want anyone else to see it. So A use a key to encrypt his message and share this key with B, tell B he need decrypt the message with this key later.
2. Encryption
- Generate a key
- Share this key with B
- Encrypt data with this key
- Transmit encrypted data to B
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.ShortBufferException; /**
*
*/
public class AESEncrypt { public static void main(String[] args) throws NoSuchAlgorithmException, IOException,
NoSuchPaddingException, InvalidKeyException, ShortBufferException,
IllegalBlockSizeException, BadPaddingException { // Generate key and store into file
SecureRandom random = new SecureRandom(); // see below
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(random);
SecretKey secretKey = keyGen.generateKey(); FileOutputStream secretKeyOut = new FileOutputStream(Util.PATH_SECRETKEY);
secretKeyOut.write(secretKey.getEncoded());
secretKeyOut.close(); // Cipher
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey); // Encrypt
BufferedInputStream dataIn = new BufferedInputStream(new FileInputStream(Util.PATH_DATA));
BufferedOutputStream encryptedDataOut = new BufferedOutputStream(new FileOutputStream(Util.PATH_DATA_ENCRYPTED)); byte[] inBytes = new byte[aesCipher.getBlockSize()];
byte[] outByte;
int len;
while ((len = dataIn.read(inBytes)) >= 0) {
outByte = aesCipher.update(inBytes, 0, len);
encryptedDataOut.write(outByte);
}
outByte = aesCipher.doFinal();
encryptedDataOut.write(outByte); dataIn.close();
encryptedDataOut.close();
} }
3. Decryption
- Get and restore the key
- Decrypt data with key
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; /**
* Class documentation to be filled TODO
*/
public class AESDecrypt { public static void main(String[] args) throws IOException, ClassNotFoundException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException { // Get key
FileInputStream secretKeyIn = new FileInputStream(Util.PATH_SECRETKEY);
byte[] secretKeyBytes = new byte[secretKeyIn.available()];
secretKeyIn.read(secretKeyBytes);
secretKeyIn.close();
SecretKey secretKey = new SecretKeySpec(secretKeyBytes, "AES"); // Cipher
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secretKey); // Decrypt
BufferedInputStream encryptedDataIn = new BufferedInputStream(new FileInputStream(Util.PATH_DATA_ENCRYPTED));
BufferedOutputStream decryptedDataOut = new BufferedOutputStream(new FileOutputStream(Util.PATH_DATA_DECRYPTED));
byte[] inBytes = new byte[aesCipher.getBlockSize()];
byte[] outBytes;
int len;
while ((len = encryptedDataIn.read(inBytes)) >= 0) {
outBytes = aesCipher.update(inBytes, 0, len);
decryptedDataOut.write(outBytes);
}
outBytes = aesCipher.doFinal();
decryptedDataOut.write(outBytes); encryptedDataIn.close();
decryptedDataOut.close();
}
}
Defect
[JavaSecurity] - AES Encryption的更多相关文章
- AES encryption of files (and strings) in java with randomization of IV (initialization vector)
http://siberean.livejournal.com/14788.html Java encryption-decryption examples, I've seen so far in ...
- [转](.NET Core C#) AES Encryption
本文转自:https://www.example-code.com/dotnet-core/crypt2_aes.asp Chilkat.Crypt2 crypt = new Chilkat.Cryp ...
- AES加密 C++调用Crypto++加密库 样例
这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES).听这名字就非常厉害的样子 预计会搜索到这 ...
- PHP的AES加密类
PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...
- C++的AES加解密
最近公司项目要做个WPF程序,但是底层加密部分要用C++来实现.通过网上搜索各种资料,地址已经记不下了,没发贴出来了! 下面看看如何加解密的~!先贴代码.... string tKey(sKey); ...
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
最先附上 下载地址 背景(只是个人感想,技术上不对后面的内容构成知识性障碍,可以skip): 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后对一些数据进行一些加密解密的操作. 笔 ...
- Windows10 VS2017 C++使用crypto++库加密解密(AES)
参考文章: https://blog.csdn.net/tangcaijun/article/details/42110319 首先下载库: https://www.cryptopp.com/#dow ...
- AES Test vectors
Table of content List of test vectors for AES/ECB encryption mode AES ECB 128-bit encryption mode AE ...
- aes加密/解密(转载)
这篇文章是转载的康奈尔大学ece5760课程里边的一个final project,讲的比较通俗易懂,所以转载过来.附件里边是工程文件,需要注意一点,在用modelsim仿真过程中会出现错误,提示非法引 ...
随机推荐
- sed replace HEX sequence in your binary file:
Here is how to replace a HEX sequence in your binary file: $ sed 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x5 ...
- Linux CentOS下Python+robot framework环境搭建
转载自:http://blog.sina.com.cn/s/blog_13cc013b50102vof1.html 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...
- APIO2017伪题解
题目质量还是比较高的,只是当时澳大利亚方面出了一点问题?最后造成了区分度非常迷的局面. 纵观三道题,T1是披着交互外衣的提答题,T2是披着交互外衣的传统题,T3是一道据说近年来APIO最水的一道传统题 ...
- HDOJ 4903 The only survival
Discription: There is an old country and the king fell in love with a devil. The devil always ask th ...
- RxJava 1.x 理解-2
给RxJava 加入线程控制 -- Scheduler 在 RxJava 1.x 理解-1 中,我们说到了RxJava的简单用法,但是这还远远不够,因为这简单用法是在同一个线程中使用的.比如我们需要在 ...
- 谁说 JavaScript 很简单了?
转载请注明出处,保留原文链接以及作者信息 本文介绍了 JavaScript 初学者应该知道的一些技巧和陷阱.如果你是老司机,就当做回顾了,哪里有写的不好的地方欢迎指出. 1. 你是否尝试过对一个数字数 ...
- JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
原文:http://www.cnblogs.com/liuxianan/p/pinyinjs.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多 ...
- mongodb_性能监控
一.使用mongostat.exe cd C:\Program Files\MongoDB\Server\3.0\bin\ --> mongostat.exe --> mongostat ...
- 大数据的开始:安装hadoop
为实现全栈,从今天开始研究Hadoop,个人体会是成为某方面的专家需要从三个方面着手 系统化的知识(需要看书或者比较系统的培训) 碎片化的知识(需要根据关注点具体的深入的了解) 经验的积累(需要遇到问 ...
- ubuntu16.04新服务器上配置selenium+firefox
ubuntu16.041安装pythonsudo apt-get install python默认2.7.122更新apt-getsudo apt-get update更新下apt-get库否则下载p ...