1. 程式人生 > >使用AES加密配置檔案

使用AES加密配置檔案

考慮到資料庫連線密碼直接配置在xml中,專案流轉出現密碼洩露隱患,這裡直接對密碼進行AES加密

<!-- 配置 讀取properties檔案 jdbc.properties -->
    <bean class="com.yehuishou.util.ConvertPwdPropertyConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
        <property name="fileEncoding" value="utf-8" />

    </bean>

配置bean指定資料庫檔案的位置, class屬性為重新的方法所在是全類名. 下面的

public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer {
    

    //"driverClassName", "url", "username", "password" 必須與配置檔案內的名字一致

    private String[] encryptPropNames = { "driverClassName", "url", "username", "password" };

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {

        // 如果在加密屬性名單中發現該屬性
        if (isEncryptProp(propertyName)) {
            String decryptValue;
            try {
                decryptValue = AES.Decrypt(propertyValue);
            } catch (Exception e) {
                return propertyValue;
            }
            return decryptValue;
        } else {
            return propertyValue;
        }
    }

    private boolean isEncryptProp(String propertyName) {
        for (String encryptName : encryptPropNames) {
            if (encryptName.equals(propertyName)) {
                return true;
            }
        }
        return false;
    }
}

陣列encryptPropNames裡面的值就是你jdbc.properties 配置檔案裡面 的名稱。

AES.Decrypt(propertyValue); 這個方法是呼叫AES的加密方法。

public class AES{

//AES加密的16位key

    private static String sKey = "1234567887654321";

    
    public static void main(String[] args) throws Exception {
        
        System.out.println(AHEEHES.Encrypt(""));
        
        System.out.println(AHEEHES.Decrypt(""));
    }

    // 加密
    public static String Encrypt(String sSrc) throws Exception {
      
        // 判斷Key是否為16位
        if (sKey.length() != 16) {
            System.out.print("Key長度不是16位");
            return null;
        }
        // 8866952612241802
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// "演算法/模式/補碼方式"
        IvParameterSpec iv = new IvParameterSpec("9876543223456789".getBytes());// 使用CBC模式,需要一個向量iv,可增加加密演算法的強度
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());

        return org.apache.commons.codec.binary.Base64.encodeBase64String(encrypted);// 此處使用BAES64做轉碼功能,同時能起到2次加密的作用。
    }

    // 解密
    public static String Decrypt(String sSrc) throws Exception {
     
            // 判斷Key是否為16位
            if (sKey.length() != 16) {
                System.out.print("Key長度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec("9876543223456789".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = org.apache.commons.codec.binary.Base64.decodeBase64(sSrc);// 先用bAES64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }
}
 

Key的長度必須是16,24,32位 , 這裡用的是AES-128-CBC加密模式 , key需要16

使用AES加密需要的pom坐

<dependency>

       <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
</dependency>