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.
时间: 2024-10-26 23:39:12