1. AES algorithm
- The Advanced Encryption Standard (AES), also as known as Rijndael (its original name), was a specification for encryption O F Electronic data established by the U.S. National Institute, and Technology (NIST) in 2001.
- It uses a fixed long key to encrypt and decrypt data, available key size, $ A, 192, and bits.
- Use case:a want to send a message to friend B, and A does not want anyone else to see it. So a is 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 the 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, BADPADDINGEXC eption {//Generate key and store into file securerandom random = new SecureRandom ();//See below K Eygenerator 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 being filled TODO */public class Aesdecrypt {public S tatic void Main (string[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, Nosuchpadd Ingexception, InvalidKeyException, illegalblocksizeexception, badpaddingexception {//Get key Fi Leinputstream 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_encrypt ED)); 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 (); }}
Defectif key is intercepted puzzle the encrypted data are very easy.
[Javasecurity]-AES encryption