2016-11-29 2 views
0

Ich möchte eine verschlüsselte H2-Datenbank entschlüsseln, um in einem nächsten Schritt das Wiederherstellungswerkzeug verwenden zu können. Die Entschlüsselung über "ChangeFileEncryption" scheint nicht richtig zu funktionieren. Ein kleines Beispielprogramm:Decrypt H2 Datenbank mit ChangeFileEncryption() schlägt fehl

public class H2DecryptionTest{ 
    public static void main(String[] args){ 
     try { 
      String path = "C:\\test"; 
      String dbName = "database"; 
      String dbPassword = "password"; 
      String userName = "username"; 
      String userPassword = "userpassword"; 
      JdbcDataSource datasource = new JdbcDataSource(); 
      String dbAttributes = "\\"+dbName+";USER="+userName+";PASSWORD="+userPassword+" " + dbPassword; 
      datasource.setURL("jdbc:h2:" + path + dbAttributes); 
      datasource.setUser(userName); 
      datasource.setPassword(userPassword); 

      Connection connection = datasource.getConnection(); 

      insertDefaultValues(connection); 
      connection.close(); 

      /////// the following does not work properly: ////////// 
      ChangeFileEncryption.execute(path, dbName, "AES", dbPassword.toCharArray(), null, false); 

      Recover.execute(path, dbName); // <<<<---- Exception is thrown here! 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 

    // just writing some stuff into the database to see that creation was ok, not important 
    private static void insertDefaultValues(Connection connection) throws SQLException { 
     Statement stmt = null; 
     try { 
      connection.setAutoCommit(false); 
      stmt = connection.createStatement(); 
      stmt.execute("CREATE TABLE PERSON(id int primary key, name varchar(255))"); 
      stmt.execute("INSERT INTO PERSON(id, name) VALUES(1, 'One')"); 
      stmt.execute("INSERT INTO PERSON(id, name) VALUES(2, 'Two')"); 
      stmt.execute("INSERT INTO PERSON(id, name) VALUES(3, 'Three')"); 

      ResultSet rs = stmt.executeQuery("select * from PERSON"); 
      System.out.println("H2 Database inserted through Statement"); 
      while (rs.next()) { 
       System.out.println("Id "+rs.getInt("id")+" Name "+rs.getString("name")); 
      } 
      stmt.close(); 
      connection.commit(); 
     } catch (SQLException e) { 
      System.out.println("Exception Message " + e.getLocalizedMessage()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

Die Konsolenausgabe ist:

Connected to the target VM, address: '127.0.0.1:63156', transport: 'socket' 
    H2 Database inserted through Statement 
    Id 1 Name One 
    Id 2 Name Two 
    Id 3 Name Three 
    Disconnected from the target VM, address: '127.0.0.1:63156', transport: 'socket' 
    java.lang.IllegalStateException: Store header is corrupt: nio:C:/test/database.mv.db [1.4.193/6] 
     at org.h2.mvstore.DataUtils.newIllegalStateException(DataUtils.java:765) 
     at org.h2.mvstore.MVStore.readStoreHeader(MVStore.java:609) 
     at org.h2.mvstore.MVStore.<init>(MVStore.java:359) 
     at org.h2.mvstore.MVStore$Builder.open(MVStore.java:2923) 
     at org.h2.mvstore.MVStoreTool.info(MVStoreTool.java:346) 
     at org.h2.tools.Recover.process(Recover.java:342) 
     at org.h2.tools.Recover.execute(Recover.java:320) 
     at H2DecryptionTest.main(H2DecryptionTest.java:22) 

    Process finished with exit code 0 

Also meine Frage ist: Wie kann die Datenbank korrekt entschlüsselt werden? Vielen Dank im Voraus :)

Antwort

0

fand ich den Fehler selbst - vielleicht kann es jemand anders helfen:

  • im Beispiel habe ich vergessen die Verschlüsselungsmethode in dbAttributes (cypher = AES) hinzuzufügen.

    String dbAttributes = "\\"+dbName+";CIPHER=AES;USER="+userName+";PASSWORD="+userPassword+" " + dbPassword; 
    
  • Ich habe das falsche Passwort für ChangeFileEncryption genommen. Das erste Passwort (in diesem Fall "userPassword") sollte es tun.

Verwandte Themen