从小就学习不好

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

简单的加密,就是把连接数据的账号密码在配置文件中使用密文,在连接数据库的时候解密。

1.加密工具类

public class DESUtil
{  
    private static Key key;  
    private static String KEY_STR="tian";  
      
    static{  
        try  
        {  
            KeyGenerator generator = KeyGenerator.getInstance("DES");  
            SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");  
            secureRandom.setSeed(KEY_STR.getBytes());
            generator.init(secureRandom);  
            key = generator.generateKey();  
            generator=null;  
        }  
        catch (Exception e)  
        {  
            throw new RuntimeException(e);  
        }  
    }  
      
    /** 
     * 对字符串进行加密,返回BASE64的加密字符串 
     * <功能详细描述> 
     * @param str 
     * @return 
     * @see [类、类#方法、类#成员] 
     */  
    
    public static String getEncryptString(String str){  
        BASE64Encoder base64Encoder = new BASE64Encoder();  
        try  
        {  
            byte[] strBytes = str.getBytes("UTF-8");  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.ENCRYPT_MODE, key);  
            byte[] encryptStrBytes = cipher.doFinal(strBytes);  
            return base64Encoder.encode(encryptStrBytes);  
        }  
        catch (Exception e)  
        {  
            throw new RuntimeException(e);  
        }  
          
    }  
      
    /** 
     * 对BASE64加密字符串进行解密 
     * <功能详细描述> 
     * @param str 
     * @return 
     * @see [类、类#方法、类#成员] 
     */  
    public static String getDecryptString(String str){  
        BASE64Decoder base64Decoder = new BASE64Decoder();  
        try  
        {  
            byte[] strBytes = base64Decoder.decodeBuffer(str);  
            Cipher cipher = Cipher.getInstance("DES");  
            cipher.init(Cipher.DECRYPT_MODE, key);  
            byte[] encryptStrBytes = cipher.doFinal(strBytes);  
            return new String(encryptStrBytes,"UTF-8");  
        }  
        catch (Exception e)  
        {  
            throw new RuntimeException(e);  
        }  
          
    }  

通过上边的工具类对连接数据库的账号密码进行加密。该数据库的账号和密码分别是 “postgres” 和 “postgres”。

经过加密后得到 “P4jPscryZFIx/IjAWZ6/Dw==” 和 “P4jPscryZFIx/IjAWZ6/Dw==”

2.配置文件属性加密

通过 DES 算法加密连接数据库的账号和密码并将加密后的密文写到 db 配置文件中。

 

jdbc.properties  配置文件完整内容如下:

jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://10.10.46.104:5432/postgres
jdbc.username=P4jPscryZFIx/IjAWZ6/Dw==
jdbc.password=P4jPscryZFIx/IjAWZ6/Dw==

jdbc.username :加密后的值

 

jdbc.password :加密后的值

3、加密生成方法

public static void main(String[] args)  
    {  
        String name ="postgres";  
        String password="postgres";  
        String encryname = getEncryptString(name);  
        String encrypassword = getEncryptString(password);  
        System.out.println(encryname);  
        System.out.println(encrypassword);  
    }

用以上的方法可以得到加密后的秘钥。

4、初始化解密

private static Properties ppt = null;

    static {
        try {
            ppt = Resources.getResourceAsProperties("jdbc.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取配置文件的属性值
        String username = ppt.getProperty("jdbc.username");
        String password = ppt.getProperty("jdbc.password");
        //把解密的username存放至Properties对象中
        ppt.setProperty("jdbc.username", DESUtil.getDecryptString(username));
        //把解密的password存放至Properties对象中
        ppt.setProperty("jdbc.password", DESUtil.getDecryptString(password));
    }

使用Properties实现解密,对象存在内存中,更改Properties对象的属性 ,不会更改配置文件的值。

posted on 2020-06-16 15:15  从小就学习不好  阅读(1453)  评论(0编辑  收藏  举报