Use of DES encryption and decryption methods

DES encryption key information

First, write a DESUtils class (key information encrypted in the database):

import java.security.Key;

import java.security.SecureRandom;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

 

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

 * Is an encryption algorithm

 * @author Administrator

 *

 */

Public Class DESUtils {

 

   private static Key key;

   private static String KEY_STR = "myKey";

   private static String CHARSETNAME = "UTF-8";

   private static String ALGORITHM = "DES";

 

   static {

      try {

         // generate the DES algorithm objects

         KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);

         // use SHA1 Security Policy

         SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");

         // Set the keys seed

         secureRandom.setSeed(KEY_STR.getBytes());

         // initialize based on SHA1 algorithm objects

         generator.init(secureRandom);

         // generate a secret key objects

         key = generator.generateKey();

         generator = null;

      } catch (Exception e) {

         throw new RuntimeException(e);

      }

   }

   /**

    * Information after obtaining encrypted

    * @Param Cycle

    * @return

    */

   public static String getEncryptString(String str) {

      BASE64Encoder base64encoder = new BASE64Encoder();

      try {

         byte[] bytes = str.getBytes(CHARSETNAME);

         Cipher cipher = Cipher.getInstance(ALGORITHM);

         cipher.init(Cipher.ENCRYPT_MODE, key);

         byte [] doFinal = cipher .doFinal ( bytes );

         return base64encoder.encode(doFinal);

      } catch (Exception e) {

         // TODO: handle exception

         throw new RuntimeException(e);

      }

   }

 

   /**

    * Information after acquiring decryption

    * @Param Cycle

    * @return

    */

   public static String getDecryptString(String str) {

      // based BASE64 encoded, received byte [] and converted into String

      BASE64Decoder base64decoder = new BASE64Decoder();

      try {

         // string decode into byte []

         byte[] bytes = base64decoder.decodeBuffer(str);

         // get the object to decrypt

         Cipher cipher = Cipher.getInstance(ALGORITHM);

         // initialize the decryption information

         cipher.init(Cipher.DECRYPT_MODE, key);

         // decryption

         byte [] doFinal = cipher .doFinal ( bytes );

         // return information after decryption

         return new String(doFinal, CHARSETNAME);

      } catch (Exception e) {

         // TODO: handle exception

         throw new RuntimeException(e);

      }

   }

  

   public static void main(String[] args) {

      System.out.println(getEncryptString("work1111"));

      System.out.println(getEncryptString("Pengliang11111111"));

     

   }

 

}

Write a EncryptPropertyPlaceholderConfigurer class (decryption key information in the database):

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

 

public class EncryptPropertyPlaceholderConfigurer extends

      PropertyPlaceholderConfigurer {

   private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };

 

   @Override

   protected String convertProperty(String propertyName, String propertyValue) {

      if (isEncryptProp(propertyName)) {

         String decryptValue = DESUtils.getDecryptString(propertyValue);

         return decryptValue;

      } else {

         return propertyValue;

      }

   }

 

   private boolean isEncryptProp(String propertyName) {

      for (String encryptpropertyName : encryptPropNames) {

         if (encryptpropertyName.equals(propertyName))

            return true;

      }

      return false;

   }

}

In the spring-dao.xml file configuration

Guess you like

Origin blog.csdn.net/qq_41479464/article/details/89951138