簡體   English   中英

加密數據庫中的舊數據

[英]encrypting old data in database

我在我的應用程序上下文文件中添加了這個

<!-- 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的spring,但我們想在我的應用程序中實現jasyptHibernateEncryptorin。

將新條目存儲到數據庫表並獲取相同條目時,它工作正常,但問題是如何加密我的舊數據。

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

好的,詳細說明:

  1. 目前您已將實體/類映射到未加密的proterties數據庫,如下所示:

    @實體

    公共類人{
    @ID
    @GeneratedValue(策略= GenerationType.AUTO)
    私人長身份;
    私有字符串名稱; }

如果您計划切換到加密類型(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();
        }

    }
}

加密所需的值后,切換Person實體以使用加密類型,如下所示:

@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
}

確保加密器的參數在定義和代碼中是相同的:相同的算法,相同的密碼,相同的密鑰比例迭代參數。 之后,您可以像往常一樣使用Person實體,因為加密對您使用的java持久性代碼是透明的。

您可以使用以下命令從svn簽出此代碼的工作示例:

svn checkout http://hibernate-jasypt-database-encryption.googlecode.com/svn/trunk/ hibernate-jasypt-database-encryption-read-only

祝好運 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM