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

  1. Generate a key
  2. Share this key with B
  3. Encrypt data with this key
  4. 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

  1. Get and restore the key
  2. 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

If key is intercepted puzzle the encrypted data is very easy.

[JavaSecurity] - AES Encryption的更多相关文章

  1. 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 ...

  2. [转](.NET Core C#) AES Encryption

    本文转自:https://www.example-code.com/dotnet-core/crypt2_aes.asp Chilkat.Crypt2 crypt = new Chilkat.Cryp ...

  3. AES加密 C++调用Crypto++加密库 样例

    这阵子写了一些数据加密的小程序,对照了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES).听这名字就非常厉害的样子 预计会搜索到这 ...

  4. PHP的AES加密类

    PHP的AES加密类 aes.php <?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ...

  5. C++的AES加解密

    最近公司项目要做个WPF程序,但是底层加密部分要用C++来实现.通过网上搜索各种资料,地址已经记不下了,没发贴出来了! 下面看看如何加解密的~!先贴代码.... string tKey(sKey); ...

  6. Crypto++入门学习笔记(DES、AES、RSA、SHA-256)

    最先附上 下载地址 背景(只是个人感想,技术上不对后面的内容构成知识性障碍,可以skip): 最近,基于某些原因和需要,笔者需要去了解一下Crypto++库,然后对一些数据进行一些加密解密的操作. 笔 ...

  7. Windows10 VS2017 C++使用crypto++库加密解密(AES)

    参考文章: https://blog.csdn.net/tangcaijun/article/details/42110319 首先下载库: https://www.cryptopp.com/#dow ...

  8. AES Test vectors

    Table of content List of test vectors for AES/ECB encryption mode AES ECB 128-bit encryption mode AE ...

  9. aes加密/解密(转载)

    这篇文章是转载的康奈尔大学ece5760课程里边的一个final project,讲的比较通俗易懂,所以转载过来.附件里边是工程文件,需要注意一点,在用modelsim仿真过程中会出现错误,提示非法引 ...

随机推荐

  1. Spring Struts里用到的设计模式

    Bean工厂的Factory模式 AOP的Proxy模式

  2. 【分治】计算概论(A) / 函数递归练习(1)多边形游戏

    #include<cstdio> #include<algorithm> using namespace std; ],c[],s[]; int work(int L,int ...

  3. Exercise02_11

    import javax.swing.JOptionPane; public class Population{ public static void main(String[] args){ int ...

  4. linux-磁盘目录使用情况-df/du

    1.  df -h   查看磁盘使用情况 2. du -h --max-depth=1  查看各文件夹大小 3.  sudo du -k --max-depth=1 | sort -k 1 -n -r ...

  5. Telnet技术白皮书

    转:http://www.cnpaf.net/Class/Telnet/200705/19978.html Telnet的应用不仅方便了我们进行远程登录,也给hacker们提供了又一种入侵手段和后门, ...

  6. LiDAR Textbook & Automated Road Network Extraction

    Original article published here, Posted on March 18, 2009 by lidar A positive feedback loop is begin ...

  7. 【java】JDK安装后,没有配置环境变量,也可以java -version查看到版本信息

    JDK安装后,没有配置环境变量,也可以java -version查看到版本信息 原因是:jdk安装过程,java.javaw.javaws三个命令被复制到C:\windows\system32目录下 ...

  8. html5:localStorage储存

    实例:刷新值会增长,关掉浏览器,再打开,值会在原基础上增长 if(localStorage.pagecount){ localStorage.pagecount=Number(localStorage ...

  9. 防止木马利用iframe框架来调用外域JS代码

    <!--防止木马利用iframe框架来调用外域JS代码,不过滤自己网站的域名的框架网页开始--><SCRIPT LANGUAGE="JavaScript"> ...

  10. 构建高性能web站点-1

    以下为阅读<构建高性能web站点>郭欣 著 这本书的适合读者: 1.编写web程序.关心站点性能,并且希望自己做的更加出色的开发人员 2.关心性能和可用性的web架构师 3.希望构建高性能 ...