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.

最新文章

  1. IM消息送达保证机制实现(二):保证离线消息的可靠投递
  2. 分布式应用下的Redis单机锁设计与实现
  3. thinkPHP--SQL连贯操作
  4. Shanghai Regional Online Contest 1004
  5. TLS 与 python thread local
  6. [Android Pro] Android性能优化典范第一季
  7. 关于each
  8. SQL数据库的基本语句
  9. Hbase split的三种方式和split的过程
  10. android 性能分析、优化
  11. 基于Twemproxy的Redis集群方案
  12. UI控件tag属性和魔法数字的处理
  13. Ghost版Win8.1系统企业版下载
  14. 教你正确地利用Netty建立连接池
  15. DTU软硬件方案
  16. 设计模式六大原则——迪米特法则(LoD)
  17. java断点
  18. Python内置函数(62)——exec
  19. 用Nifi 从web api 取数据到HDFS
  20. 解决mysqli的中文乱码问题

热门文章

  1. Maven与Nexus OSS
  2. 传统路径导出 VS 直接路径导出(oracle exp direct=y)
  3. 进程注入后门工具Cymothoa
  4. 【数位dp】hdu2089 不要62
  5. 【树链剖分/线段树】BZOJ1036-[ZJOI2008]树的统计Count
  6. 权限管理-RBAC
  7. SpringMVC实现操作的第二种方式
  8. Android闪闪发光字体效果
  9. Android Studio 生成aar包,并在其他项目中引用
  10. Remote procedure call (RPC)