change the driver which is compatible with sqlcipher and modified the way to connect the encrypted sqlite db file

This commit is contained in:
ChessNineeee committed 2021-04-06 23:16:50 +08:00
1 parent aa2761b10e
commit f6da0c30ba
5 files changed
+61 -8

No files matched your search

+8 -3
View File
@@ -94,12 +94,17 @@
<version>3.0.8</version>
</dependency>
<!-- Sqlite Driver-->
<!-- Sqlite Driver compatible with sqlcipher-->
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<!-- <dependency>-->
<!-- <groupId>org.xerial</groupId>-->
<!-- <artifactId>sqlite-jdbc</artifactId>-->
<!-- <version>3.28.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.xerial</groupId>
<groupId>io.github.willena</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.28.0</version>
<version>3.35.4</version>
</dependency>
@@ -1,5 +1,8 @@
package minsecurity.identity.persist.sqlite;
import org.sqlite.mc.SQLiteMCConfig;
import org.sqlite.mc.SQLiteMCSqlCipherConfig;
import java.io.File;
import java.sql.*;
@@ -34,6 +37,10 @@ public class Sqlite {
}
private String passwd2HexKey(){
return String.format("x'%s'", passwd);
}
/**
* 获取Sqlite object
* @return Sqlite object
@@ -70,8 +77,8 @@ public class Sqlite {
new File(dbPath).mkdirs();
}
String real_db_file = dbPath + db_file;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
// 兼容sqlcipher
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
stmt = c.createStatement();
stmt.executeUpdate(table_create);
stmt.close();
@@ -92,8 +99,8 @@ public class Sqlite {
new File(filePath).mkdirs();
}
String real_db_file = filePath + db_file;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
// 兼容sqlcipher
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
stmt = c.createStatement();
stmt.executeUpdate(table_create);
stmt.close();
@@ -111,7 +118,8 @@ public class Sqlite {
try{
String real_db_file = db_path + db_file;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file);
// 兼容sqlcipher
c = DriverManager.getConnection("jdbc:sqlite:" + real_db_file, SQLiteMCSqlCipherConfig.getV4Defaults().withKey(passwd2HexKey()).toProperties());
// TODO 设置数据库最大连接数
return c;
}catch (Exception ex){
@@ -210,6 +210,7 @@ public final class Db {
Connection c = Sqlite.getInstance().getConn();
String pubStr = "", priStr = "", certStr, prikeyRawByte = "";
int algo, sign;
if (identity.hasPrivateKey()){
byte[] priByte = identity.getPrikey().getBytes();
priStr = Base64.getEncoder().encodeToString(priByte);
@@ -1,5 +1,6 @@
package minsecurity.identity.sqlite;
import minsecurity.identity.persist.sqlite.Sqlite;
import minsecurity.identity.persist.sqlite.SqliteUtil;
import org.junit.Test;
@@ -11,4 +12,38 @@ public class SqliteTest {
System.out.println(SqliteUtil.home());
assertFalse(SqliteUtil.pathExists("/aaa/1.txt"));
}
@Test
public void TestSqliteOpen(){
try{
Sqlite.getInstance().openDefault();
boolean res = SqliteUtil.pathExists("/home/zhengqi/min/identity/identity.db");
if (!res){
throw new Exception("no target db file!!");
}
String homePath = SqliteUtil.home();
String filePath = homePath + "/test/";
Sqlite.getInstance().open(filePath);
boolean res1 = SqliteUtil.pathExists("/home/zhengqi/test/identity.db");
if (!res1){
throw new Exception("no target db file!!");
}
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
@Test
public void TestSqliteSetPasswd(){
try{
String newPasswd = "1DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99";
Sqlite.getInstance().setPasswd(newPasswd);
Sqlite.getInstance().open("/home/zhengqi/min/identity/");
// Sqlite.getInstance().getConn();
}catch (Exception ex){
System.out.println(ex.getMessage());
}
}
}
@@ -0,0 +1,4 @@
package minsecurity.identity.sqlite.db;
public class DBTest {
}