Java Symmetric Data encryption AES

Source: Internet
Author: User
Tags base64 decrypt ming asymmetric encryption

Yesterday wrote an asymmetric data encryption, today to write symmetric data encryption AES. Symmetric data encryption is the use of only one key for encryption and decryption, AES can use 128,192, and 256-bit keys.

And then there's my tool class:

public class Aesutil {public static byte[] Getkeys (String data) {try {//create AES key producer Ke            Ygenerator KGen = keygenerator.getinstance ("AES"); Using the user password as a random number to initialize the 128-bit key producer//securerandom is to generate a safe random number sequence, password.getbytes () is a seed, as long as the seed is the same, the sequence is the same, the key is the same k            Gen.init (+, New SecureRandom (Data.getbytes ()));            According to the user password, generate a key Secretkey Secretkey = Kgen.generatekey ();            byte[] key = secretkey.getencoded ();        Return key;            } catch (NoSuchAlgorithmException e) {e.printstacktrace ();        System.out.println ("Without this algorithm");    } return null; } public static byte[] Getkeys () {try {//create AES key producer Keygenerator KGen = KEYGENERATOR.G            Etinstance ("AES");            Using the user password as a random number to initialize the 128-bit key producer//securerandom is to generate a safe random number sequence, no seed kgen.init (128);            Secretkey Secretkey = Kgen.generatekey (); byte[] key = Secretkey.geteNcoded ();        Return key;            } catch (NoSuchAlgorithmException e) {e.printstacktrace ();        System.out.println ("Without this algorithm");    } return null; }/** * @param content * @param secretkey * @return */public static byte[] Encrypt (String content, Byte[] Secretkey) {try {//convert to AES private key Secretkeyspec key = new Secretkeyspec (Secretkey, "AES            ");            Cipher Cipher = cipher.getinstance ("AES");//Create cipher byte[] bytecontent = content.getbytes ("Utf-8");            Cipher.init (Cipher.encrypt_mode, key);//cipher initialized to cryptographic mode byte[] result = cipher.dofinal (bytecontent);//encryption        return result;        } catch (Exception e) {e.printstacktrace ();    } return null; } public static byte[] Decrypt (byte[] content, Secretkey Secretkey) {try {byte[] Encodeformat = sec            Retkey.getencoded (); Convert to AES private key Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES");            Create a cipher Cipher Cipher = cipher.getinstance ("AES");            Cipher cipher.init initialized to decryption mode (Cipher.decrypt_mode, key);            Byte[] result = cipher.dofinal (content);        Clear text return result;        } catch (Exception e) {e.printstacktrace ();    } return null; } public static byte[] Decrypt (byte[] content, byte[] secretkey) {try {//convert to AES private key Sec            Retkeyspec key = new Secretkeyspec (Secretkey, "AES");            Create a cipher Cipher Cipher = cipher.getinstance ("AES");            Cipher cipher.init initialized to decryption mode (Cipher.decrypt_mode, key);            Byte[] result = cipher.dofinal (content);        Clear text return result;        } catch (Exception e) {e.printstacktrace ();    } return null;        public static void Main (string[] args) {String content = "I am-winter bamboo"; String Password = "767 ";        System.out.println ("Before encryption:" + content);        byte[] key = Getkeys ();        Encryption byte[] Encrypt = aesutil.encrypt (content, key);        SYSTEM.OUT.PRINTLN ("Encrypted content:" + base64.encodebase64urlsafestring (encrypt));        Decrypt byte[] Decrypt = Aesutil.decrypt (encrypt, key);    SYSTEM.OUT.PRINTLN ("Decrypted content:" + new String (decrypt)); }}

  

The process is to randomly generate a key (can have a seed, or not), and then use this key to encrypt and decrypt, or very simple (ˇ∀ˇ)

And then for security, often RSA and AES are used together, generally speaking:

Client-side encryption:

1. Generate AES key randomly;

2. AES encryption of important information

3. Use RSA for Public key encryption of AES keys

Service-Side decryption:

1. Decrypt the AES key using the RSA private key

2. Decryption of important information with AES key

So I simulated the process again:

public class Aes_rsautil {public    static void Main (string[] args) throws exception{/        * Impersonation Client */        String msg = "he Llo Winter Bamboo ";        byte[] key = Aesutil.getkeys ();//Gets the encoding of the key        byte[] bytes = Aesutil.encrypt (msg,key);        String sekey = base64.encodebase64urlsafestring (key);//Convert to string after re        -encrypt rsautil.init ();        AES key after RSA public key is encrypted        String Encryptkey = Rsautil.encryptbypublickey (Sekey,rsautil.getpublickey (RSAUtil.keyMap.get ("Public_key"));        /* Analog service side        ///decode AES key        String Aeskey = Rsautil.decryptbyprivatekey (Encryptkey,rsautil.getprivatekey ( RSAUtil.keyMap.get ("Private_key"));        Restore Aeskey        byte[] Secretkey = base64.decodebase64 (Aeskey);        String ming = new String (Aesutil.decrypt (Bytes,secretkey));        System.out.println (Ming);    }

  

  The dependency on this is what I did yesterday, okay, because the Java JDK has helped us to write the details of these cryptographic algorithms, so it is convenient

Finally, recommend:

    1. Detailed introduction and implementation of AES encryption algorithm

2. Data transmission Encryption--Asymmetric encryption algorithm rsa+ symmetric encryption algorithm AES

Https://github.com/MoisAbby/XZUtils

Java Symmetric Data encryption AES

Related Article

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.