數據庫賬號密碼加密


簡單的加密,就是把連接數據的賬號密碼在配置文件中使用密文,在連接數據庫的時候解密。

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對象的屬性 ,不會更改配置文件的值。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM