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