【发布时间】:2020-11-04 09:32:17
【问题描述】:
我想弄清楚如何在非安卓 java 中加密一个 sqlite 数据库。
这似乎不是很直接,但我 Willena jdbc crypt 似乎确实能够创建加密数据库,但我根本不知道如何使用它访问 SQLCipher 4 加密数据库。
这是我的代码。
String path = "jdbc:sqlite:C:\\Users\\User1\\Desktop\\testServer232.db";
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection(path+"?cipher=sqlcipher&key=a");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(3, 'leo1')");
statement.executeUpdate("insert into person values(4, 'yui1')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
{
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e.getMessage());
}
}
此代码确实有效,但我认为它不会生成 SqlCipher 4 加密数据库。当我尝试使用 Sqlite 的 DB 浏览器打开它时,当我输入密码 = a 时,它不允许我访问。
我哪里错了?
【问题讨论】:
标签: java sqlite encryption sqlcipher