【问题标题】:Willena sqlite jdbc cannot open SqlCipher dbWillena sqlite jdbc 无法打开 SqlCipher db
【发布时间】: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


    【解决方案1】:

    好的,所以我最终找到了存储库的创建者。而且他很容易就解决了,而且回答得非常快。

    解决方法如下: 以下是一些可以测试的东西:

    1. 使用版本 3.31.1
    2. 尝试使用“jdbc:sqlite:file:C:\Users\User1\Desktop\test.db?cipher=sqlcipher&key=password123”作为 URI 进行数据库连接(注意添加的“file:”)。李>
    3. 尝试在此处添加 SQLCipher 的旧参数 (https://github.com/Willena/sqlite-jdbc-crypt#aes-256-bit-cbc---sha1sha256sha512-hmac-sqlcipher)。 URI 会变成这样:“cipher=sqlcipher&key=password123&legacy=4”

    这现在对我有用。我建议其他人如果对使用 sqlcipher 版本 4 的简单方法感兴趣,则可以使用它,类似于在 android 项目中的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多