[JavaSecurity] - AES Encryption
2024-09-04 13:50:00
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
If key is intercepted puzzle the encrypted data is very easy.
最新文章
- IM消息送达保证机制实现(二):保证离线消息的可靠投递
- 分布式应用下的Redis单机锁设计与实现
- thinkPHP--SQL连贯操作
- Shanghai Regional Online Contest 1004
- TLS 与 python thread local
- [Android Pro] Android性能优化典范第一季
- 关于each
- SQL数据库的基本语句
- Hbase split的三种方式和split的过程
- android 性能分析、优化
- 基于Twemproxy的Redis集群方案
- UI控件tag属性和魔法数字的处理
- Ghost版Win8.1系统企业版下载
- 教你正确地利用Netty建立连接池
- DTU软硬件方案
- 设计模式六大原则——迪米特法则(LoD)
- java断点
- Python内置函数(62)——exec
- 用Nifi 从web api 取数据到HDFS
- 解决mysqli的中文乱码问题