0

現在、テキスト ドキュメントを 64 ビット暗号化で暗号化する暗号化プログラムを作成しています。それが機能する方法は、文字列を受け取り、文字列を暗号化することです。私は現在、プログラムがファイルのすべての内容を文字列に保存し、文字列を暗号化し、暗号化された文字列でファイルを上書きする方法を探しています。ただし、

while((bufferedReader.readLine()) != null) {
...
}

最初の行を読み取って暗号化するだけで、残りは変更されません。

ただし、次を使用します。

            List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                Charset.defaultCharset());
        for (String line : lines) {
        ...
        }

最後の行のみが暗号化されます。正直なところ、アイデアが不足しているので、もう何をすべきかわかりません。

これが私の現在のコードです(私が何か新しいことを試みていたので、これもファイルに追加するだけです):

    public static void Encrypt() throws Exception {

    try {

        FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile(), true);
        BufferedWriter bw = new BufferedWriter(fw);

        List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
                Charset.defaultCharset());
        for (String line : lines) {
            System.out.println(line);
            System.out.println(AESencrp.encrypt(line));
            bw.write(AESencrp.encrypt(line));
        }

        bw.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}
4

3 に答える 3

2

BufferedReader#readLineリーダーから読み取ったテキスト行を返します。あなたの例では、戻り値を無視しています。

代わりに、次のようなことを行う必要があります...

String text = null;
while((text = bufferedReader.readLine()) != null) {
    // Process the text variable
}
于 2013-06-22T00:23:52.857 に答える
1

行ごとに暗号化するのは良い考えだとは思いません。私はこのようにします

Cipher cipher = ...
Path path = Paths.get(file);
File tmp = File.createTempFile("tmp", "");
try (CipherOutputStream cout = new CipherOutputStream(new FileOutputStream(tmp), cipher)) {
    Files.copy(path, cout);
}
Files.move(tmp.toPath(), path, StandardCopyOption.REPLACE_EXISTING);

このような暗号化されたテキストを読む

Scanner sc = new Scanner(new CipherInputStream(new FileInputStream(file), cipher));
while(sc.hasNextLine()) {
    ...
于 2013-06-22T01:33:32.547 に答える