加密properties文件

来源:互联网 发布:易企秀模板源码 编辑:程序博客网 时间:2024/06/14 03:42

加密properties文件

在做web项目时,我们经常都会使用jdbc.properties文件来保存我们使用的数据库的路径,用户名,及密码,在一般的教程上,我们看到的都是使用的明文来存储。如果我们部署到生产环境上,那天真心倒霉了,被人登陆上生产机,那么我们的数据库就完全暴露,为了解决这一问题,我们想到把明文转换为密文。笔者做web项目使用的是hibernate框架,所以接下来的说明都是基本hibernate。项目中有个文件beans.xml,在这里我们配置了数据库的一些参数,如下:```<bean id="dataSource"    class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="${jdbc.driverClassName}" />    <property name="url" value="${jdbc.url}" />    <property name="username" value="${jdbc.username}" />    <property name="password" value="${jdbc.password}" /></bean><bean    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <value>classpath:jdbc.properties</value>    </property></bean>
    其中PropertyPlaceholderConfigurer表示的是解晰这个属性文件的类,看源码看到,这个类的processProperties是对读取记录在属性文件的属性值。于是方法有了:在属性文件中我们存放的是密文,然后在这个方法体里,把密码解晰成明文,即可。方法体如下:        ```    @Override      protected void processProperties(              ConfigurableListableBeanFactory beanFactory, Properties props)              throws BeansException {          ThreeDes des = new ThreeDes(); // 实例化一个对像        des.getKey(ThreeDes.GK_STR);// 生成密匙 (同一个key)            //密码        String password = props.getProperty("jdbc.password");        if (password != null) {            props.setProperty("jdbc.password", des.getDesString(password));          }          //用户名        String username = props.getProperty("jdbc.username");        if (username != null) {            props.setProperty("jdbc.username", des.getDesString(username));          }        //路径         String url = props.getProperty("jdbc.url");        if (url != null) {            props.setProperty("jdbc.url", des.getDesString(url));          }        super.processProperties(beanFactory, props);//调用父方法      }
其中ThreeDes类是笔者写的一个加密解密类    ```

public class ThreeDes {

private Key key;//加密算法private static String EA = "DES";//加密密钥public static String GK_STR = "abcdesc";/**根据参数生成key*/public void getKey(String strkey){    try{        KeyGenerator generator = KeyGenerator.getInstance(EA);        generator.init(new SecureRandom(strkey.getBytes()));        this.key = generator.generateKey();        generator = null;    }catch(Exception e)    {        e.printStackTrace();    }}/**加密*/public String getEncString(String str){    byte[] mi = null;    byte[] ming =null;    String strMi = "";    BASE64Encoder base64en = new BASE64Encoder();    try{        ming = str.getBytes("UTF-8");        mi = this.getEncCode(ming);        strMi = base64en.encode(mi);    }catch(Exception e)    {        e.printStackTrace();    }finally{        base64en = null;        mi = null;        ming = null;    }    return strMi;}/**解密*/public String getDesString(String str){    BASE64Decoder base64De = new BASE64Decoder();    byte[] ming = null;    byte[] mi = null;    String strMing = "";    try{        mi = base64De.decodeBuffer(str);        ming = this.getDesCode(mi);        strMing = new String(ming, "UTF-8");    }catch(Exception e)    {        e.printStackTrace();    }finally{        base64De = null;        ming = null;        mi = null;    }    return strMing;}/**EA加密*/private byte[] getEncCode(byte[] bytes){    byte[] byteFina = null;    Cipher cipher = null;    try{        cipher = Cipher.getInstance(EA);        cipher.init(Cipher.ENCRYPT_MODE, key);        byteFina = cipher.doFinal(bytes);    }catch(Exception e)    {        e.printStackTrace();    }finally{        cipher = null;    }    return byteFina;}/**EA解密*/private byte[] getDesCode(byte[] bytes){    Cipher cipher;    byte[] byteFina = null;    try{        cipher = Cipher.getInstance(EA);        cipher.init(Cipher.DECRYPT_MODE, key);        byteFina = cipher.doFinal(bytes);    }catch(Exception e)    {        e.printStackTrace();    }finally{        cipher = null;    }    return byteFina;}public static void main(String[] args){    //转换路径时,要注意格式,不要把明文写在属性文件的值放进来,否则会连接不上数据库    String encryStr = "jdbc:mysql://localhost:3306/test";    ThreeDes des = new ThreeDes();    des.getKey(GK_STR);    String mi = des.getEncString(encryStr);    System.out.println(mi);    String ming = des.getDesString(mi);    System.out.println(ming);}

}
“`

0 0
原创粉丝点击