2014-02-11 135 views
0

舊數據我在我的應用程序上下文文件加密數據庫

<!-- Added to encrypt user identification fields using jasypt --> 
    <bean id="stringEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor" lazy-init="false"> 
    <property name="algorithm" value="PBEWithMD5AndDES" /> 
    <property name="password" value="contactKey" /> 
    </bean> 

    <bean id="hibernateEncryptor" class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor" lazy-init="false"> 
    <!-- This property value must match "encryptorRegisteredName" used when defining hibernate user types --> 
    <property name="registeredName" value="jasyptHibernateEncryptor" /> 
    <property name="encryptor" ref="stringEncryptor" /> 
    </bean>` 

This below coded added in hibernate mapping file 
`<typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType"> 
     <param name="encryptorRegisteredName">jasyptHibernateEncryptor</param> 
    </typedef> 

我們正在使用與Hibernate春天到我的應用程序添加了這個,但我們要在implenting jasyptHibernateEncryptorin我的應用程序。

將數據庫中的新條目存入數據庫並獲取相同條目時工作正常,但問題在於如何加密舊數據。

回答

1

您創建了一個新的應用程序,該應用程序連接到數據庫,獲取所有現有行並在使用加密器加密字段後逐一更新它們。完成此更新後,您可以使用新的typedef來處理這些加密字段。

+0

感謝您的答覆.. – user3296747

+0

我想知道我使用哪種加密技術。這與jasypt加密技術相同/我如何獲得未加密的舊數據 – user3296747

1

好,更詳細地說明:

  1. 目前你映射你的實體/類與proterties數據庫不加密的,看起來是這樣的:

    @Entity

    公共類Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    私人字符串名稱; }

如果您打算SWITH到加密類型(jasypt),您需要先在數據庫中,看起來像這樣的代碼加密所有當前 值:

public class Exec { 
    public static void main(String[] args) { 

     SessionFactory sf = HibernateUtil.getSessionFactory(true); 
     Session session = null; 
     try { 
      session = sf.openSession();   


      //configure the jasypt string encryptor - different type use different encryptors 
      StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
      encryptor.setAlgorithm("PBEWithMD5AndDES"); 
      encryptor.setPassword("123456"); 
      encryptor.setKeyObtentionIterations(1000);   
      encryptor.initialize(); 
//      get all unencrypted data from db and encrypt them - here just the name property of the Person is encrypted. 
      session.beginTransaction(); 
       List<Person> persons = session.createQuery("select p from Person p").list(); 
      for(Person pers : persons){ 
       pers.setName(encryptor.encrypt(pers.getName())); 
       session.save(pers);    
      } 

      session.getTransaction().commit(); 
     } catch (Exception ex) { 
      try { 
       ex.printStackTrace(); 
       session.getTransaction().rollback(); 
      } catch (Exception ex2) { 
       ex2.printStackTrace(); 
      } 
     } finally { 
      session.close(); 
      HibernateUtil.shutdown(); 
     } 

    } 
} 

後你加密你需要的值,開關的人員實體使用這樣的加密類型:

@org.hibernate.annotations.TypeDefs({ 
    @org.hibernate.annotations.TypeDef(name="EncryptedString", 
      typeClass=EncryptedStringType.class, 
      parameters={@Parameter(name="algorithm",value="PBEWithMD5AndDES"),@Parameter(name="password",value="123456"),@Parameter(name="keyObtentionIterations",value="1000")}) 
}) 
@Entity 
public class Person { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private long id; 

    @Type(type="EncryptedString") 
    private String name; 

    public long getId() { 
     return id; 
    } 
// ... getters and setters 
} 

確保ENC的參數在定義和代碼中ryptors是相同的: 相同的算法,相同的密碼,相同的密鑰描述迭代參數。 之後,您可以照常使用Person實體,因爲加密對於您使用的java持久性代碼是透明的。

這段代碼的工作比如你可以從SVN結帳用這個命令:

svn籤http://hibernate-jasypt-database-encryption.googlecode.com/svn/trunk/休眠-jasypt數據庫加密,只讀

祝你好運!