How to?
Jasypt的EncryptableProperties類別,提供讀取加密過後的properties檔案,其中密文以ENC()包住。
# #Sat Jan 16 00:52:05 CST 2016 password=ENC(xWWu6PWYHwfVmNMTXCJG5A\=\=) account=root
以下為測試範例,存檔是透過一般的Properties類別,需要加密的部分透過PropertyValueEncryptionUtils類別去做轉換;讀檔則使用EncryptableProperties類別:
@Test public void test() throws IOException{ StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword("jasypt"); Properties p = new Properties(); p.setProperty("account", "root"); p.setProperty("password", PropertyValueEncryptionUtils.encrypt("123456", encryptor)); File file = new File("test.properties"); file.createNewFile(); FileOutputStream fos = null; try { fos = new FileOutputStream(file); p.store(fos, "gg"); } finally { close(fos); } EncryptableProperties ep = new EncryptableProperties(encryptor); FileInputStream fis = null; try { fis = new FileInputStream(file); ep.load(fis); } finally { close(fis); } Assert.assertEquals("root", ep.getProperty("account")); Assert.assertEquals("123456", ep.getProperty("password")); }
留言
張貼留言