Symmetric encryption-java Implementation

Source: Internet
Author: User

The main steps are as follows:

1. Use Secretkeyfactory.getinstance ("encryption algorithm") to create the key factory, encryption algorithm such as "DES", "AES" and so on

2. Generate the Key object with the new Deskeyspec (original key), the original key is the byte[] type

3. Converting a key object into a canonical key object using the Generatesecret (Key object) method of the key factory

4. Generate a trustworthy random number source with new SecureRandom () or with new Ivparameterspec (vector)

5. Generate encrypted decrypted objects with Cipher.getinstance ("encryption algorithm/encryption mode/fill mode")

6. Initialize cryptographic decryption objects with Cipher.init (cryptographic decryption model, canonical key, random number)

7. Perform cryptographic decryption operations with cipher.dofinal (data)

The code is as follows:

1    //Encrypt2    Private  byte[] Desencrypt (byte[] Data,byte[] key)throwsException {3SecureRandom sr =Newsecurerandom ();4Deskeyspec DKs =NewDeskeyspec (key);5Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");6Secretkey SecureKey =Keyfactory.generatesecret (DKS);7Cipher Cipher = cipher.getinstance ("DES");8Cipher.init (1, SecureKey, SR);9         returncipher.dofinal (data);Ten     } One     //decryption A     Private  byte[] Desdecrypt (byte[] Data,byte[] key)throwsException { -SecureRandom sr =Newsecurerandom (); -Deskeyspec DKs =NewDeskeyspec (key); theSecretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES"); -Secretkey SecureKey =Keyfactory.generatesecret (DKS); -Cipher Cipher = cipher.getinstance ("DES"); -Cipher.init (2, SecureKey, SR); +         returncipher.dofinal (data); -}

Symmetric encryption-java Implementation

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.