Encryption of users and passwords in Hibernate configuration files

Source: Internet
Author: User
Tags decrypt

In Hibernate.cfg.xml, users and passwords are stored in plaintext, storing certain security issues and thinking of a way to encrypt information about users and passwords.

First create a connection provider, the parameter interpretation in the configuration file is responsible for this class, so as long as the ciphertext decryption in this class. Public class customdrivermanagerconnectionprovider extends          provider {    public customdrivermanagerconnectionprovider ()  {         super ();          /*      *  (Non-javadoc)       *       *  @see   Org.hibernate.connection.drivermanagerconnectionprovider#configure (java.util.Properties)       */     @Override     public void configure ( Properties props)  throws hibernateexception {         string user = props.getproperty (Environment.user);         string password = props.getproPerty (Environment.pass);         props.setproperty (Environment.user, secutil.decrypt (USER) );         props.setproperty (Environment.pass, secutil.decrypt ( password));         super.configure (props);     }}

 

Write a class that uses AES to encrypt and decrypt strings/**  * aes encryption tools  *   *  @author  bany  *   *   @version   creation time:2008-8-5  am 10:58:16  *   */public class secutil {     private static byte[] keybytes = { 0x31, 0x32,  ...... };     public static void main (String[] args)  throws Exception  {        string e1 = encrypt ("NewPassword");         SYSTEM.OUT.PRINTLN (E1);         string e2 = decrypt (E1);         SYSTEM.OUT.PRINTLN (E2);     }     /**      *  Encryption       *  @param  value      *  @return       */    public static string encrypt (String value)  {                 string  s=null;         int mode = Cipher.ENCRYPT_MODE;         try {             cipher cipher = initcipher (mode);             byte[] outBytes =  Cipher.dofinal (Value.getbytes ());             s = string.valueof ( Hex.encodehex (outbytes));         } catch  (exception e)  {             e.printstacktrace ();          }         return s;     }     /**      *  decryption       *  @param  value      *  @return       */    public static string decrypt (string value)  {         String s = null;         int mode = Cipher.DECRYPT_MODE;         try {             cipher cipher = initcipher (mode);             byte[] outBytes =  Cipher.dofinal (Hex.decodehex (Value.tochararray ()));             s = new String (outbytes);         } catch  (exception e)  {             e.printstacktrace ();         }         return s ;     }          private static Cipher  Initcipher (Int mode)  throws NoSuchAlgorithmException, NoSuchPaddingException,  invalidkeyexception{        Cipher cipher =  Cipher.getinstance ("aes/ecb/pkcs5padding");         key key = new secretkeyspec (Keybytes,   "AES");         cipher.init (Mode, key);         return cipher;     }}

Call Secutil.encrypt method, the user password encryption to generate ciphertext, and then modify the Hibernate.cfg.xml file according to ciphertext

<property name= "Connection.username" >c59cd98</property> <property name= "Connection.password" > 68e32593ea5943a6a</property> <property name= "Connection.provider_class" >bany. Customdrivermanagerconnectionprovider</property>

Line 12th is the ciphertext after encryption, and the third line uses the custom connector

PS: If you use a third-party connector, the Customdrivermanagerconnectionprovider needs to inherit from the appropriate connector, such as C3p0connectionprovider

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.