java - lucene 中的加密工具

标签 java encryption lucene

最近在我的项目中,我使用 lucene 在我们的数据库中进行重复搜索,它工作得很好。 但现在需要加密 lucene 索引,我被要求寻找 lucene 本身提供的加密工具,而不是使用外部库。

我刚刚找到了 LUCENE-2228 AES 加密目录并制作了小型 POC。 问题是,当我重新索引时,出现以下错误:

java.lang.RuntimeException: File already Exists
    at org.apache.lucene.util.AESWriter.<init>(AESWriter.java:117)
    at org.apache.lucene.store.AESDirectory$AESIndexOutput.<init>
        (AESDirectory.java:187)
    at org.apache.lucene.store.AESDirectory.createOutput(AESDirectory.java:72)
    at org.apache.lucene.index.SegmentInfos.finishCommit(SegmentInfos.java:939)
    at org.apache.lucene.index.IndexWriter.finishCommit(IndexWriter.java:3539)
    at org.apache.lucene.index.IndexWriter.commitInternal(IndexWriter.java:3529)
    at org.apache.lucene.index.IndexWriter.closeInternal(IndexWriter.java:1879)
    at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1822)
    at org.apache.lucene.index.IndexWriter.close(IndexWriter.java:1786)
    at org.apache.lucene.test.indexing.main(indexing.java:45)

这是我的代码:

public class indexing 
{
    private static final byte[] KEY = 
        new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't',
'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
    public static void main(String[] args) throws Exception
    {

        Directory INDEX_DIR = new AESDirectory(new File("index1"),KEY);

        Connection conn=null;

        SnowballAnalyzer analyzer=new SnowballAnalyzer(Version.LUCENE_30,"English");
        try
        {
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           conn = DriverManager.getConnection("jdbc:mysql:///lucene", "abcd", "abcd");
           IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_34, analyzer);
           IndexWriter writer = new IndexWriter(INDEX_DIR, config);
           writer.deleteAll();
           //writer.flush();
           System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
           long starttime=System.currentTimeMillis();
           indexDocs(writer, conn);
           writer.optimize();
           writer.close();
           long endtime=System.currentTimeMillis();
           long timetaken=TimeUnit.MILLISECONDS.convert(endtime - starttime,TimeUnit.MILLISECONDS);
           System.out.println("Time taken to do indexing is "+timetaken+"ms");
        } 
        catch (Exception e)
        {
           e.printStackTrace();
        }
    }
    static void indexDocs(IndexWriter writer, Connection conn) throws Exception 
    {
        //String sql = "select qid,question from tblquestions";
        String sql = "select qid,question from tblquestions";
        Statement stmt = conn.createStatement();
        stmt.setFetchSize(Integer.MIN_VALUE);
        ResultSet rs = stmt.executeQuery(sql);
        Integer count = 0;
        while (rs.next())
        {
            count ++;
            Document d = new Document();
            d.add(new Field("qid", rs.getString("qid"), Field.Store.YES, Field.Index.NOT_ANALYZED));
            d.add(new Field("question", rs.getString("question"), Field.Store.YES,  Field.Index.ANALYZED));
            writer.addDocument(d);
        }
        System.out.println("count: " + count);
    }
}

谁能帮我解决这个问题吗? 或者 给出一些关于 lucene 索引加密的想法。

最佳答案

该补丁适用于 3.1 版本,您似乎正在使用其他版本的 Lucene。选择以下替代方案之一,直到您的版本的补丁发布(或编写您自己的补丁!)

  1. 切换到 Lucene 3.1

  2. 使用 Windows NTFS 加密。应该是安全的,除非未经授权的人知道如何以创建索引的用户身份登录。

  3. 继续使用 TrueCrypt 或其他外部加密。这应该非常安全,但需要安装 TrueCrypt 和管理权限才能安装加密驱动器。

关于java - lucene 中的加密工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11758619/

相关文章:

java - 滚动不起作用时工具栏自动隐藏和显示

python - 关于 Diffie-Hellman key 交换

encryption - 使用 ColdFusion 对数据进行签名以进行单点登录

lucene - 在 ElasticSearch 中匹配缺失的空格

java - 将/assets/image.png 转换为 byte[]

java - 如何构建只有一处不同的 3 个类

java - LinkedLists 和其中的对象

c++ - C++中的简单加密/解密

java - Lucene搜寻器(需要建立Lucene索引)

java - 如何在lucene中获取文档总数