[Javasecurity]-AES encryption

Source: Internet
Author: User
Tags decrypt

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
    1. Generate a key
    2. Share This key with the 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, 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
    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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.