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. 数字 function

    SELECT TRUNC(15.79), TRUNC(), ROUND(15.79), ), ), FLOOR(26.983), CEIL(26.123), ), ), ), MOD(,) FROM ...

  2. 记录(Record)

    记录有可以被称为行(Row),可以通俗的认为它是数据表中的一行数据.以员工表为例,一个公司的员工表中的数据是这样的: 这里每一行数据就代表一个员工的资料,这样的一行数据就叫做一条记录.表是由行和列组成 ...

  3. 分享Kali Linux 2017年第29周镜像文件

     分享Kali Linux 2017年第29周镜像文件 Kali Linux官方于7月16日发布2017年的第29周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE ...

  4. [Codeforces 35E] Parade

    Link: Codeforces 35E 传送门 Brief Intro: 给定$n$个矩形,求出轮廓线的所有顶点 Solution: 对于此类可拆分成多个事件点的题目,使用扫描线的方式 将每个矩形分 ...

  5. mac下更新自带的PHP版本到5.6或7.0

    下载和安装PHP 5.6 打开终端并且运行如下命令: curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6 然后,PHP 5.6的版本会被安装 ...

  6. mysql 初始化报错 /usr/local/mysql/bin/mysqld:error while loading shared libraries :libaio.so.1

    安装mysql在初始化的时候,出现/usr/local/mysql/bin/mysqld:error while loading shared libraries:libaio.so.1 :canno ...

  7. Hibernate 参数匹配查询

    第一种: public User validate(String userName, String password) { String hql = "from User where use ...

  8. Spark createDirectStream 维护 Kafka offset(Scala)

    createDirectStream方式需要自己维护offset,使程序可以实现中断后从中断处继续消费数据. KafkaManager.scala import kafka.common.TopicA ...

  9. Python测试Kafka集群(pykafka)

    生产者代码: # -* coding:utf8 *- from pykafka import KafkaClient host = 'IP:9092, IP:9092, IP:9092' client ...

  10. Ruby中map, collect,each,select,reject,reduce的区别

    # map 针对每个element进行变换并返回整个修改后的数组 def map_method arr1 = ["name2", "class2"] arr1. ...