Java 如何正确使用CipherOutputStream加密和解密使用log4j(RollingFileAppender)创建的日志

Java 如何正确使用CipherOutputStream加密和解密使用log4j(RollingFileAppender)创建的日志,java,file,log4j,encryption,Java,File,Log4j,Encryption,我在加密/解密log4j的RollingFileAppender生成的日志文件时遇到问题。对于我尝试扩展RollingFileAppender的加密,只需将其称为EncryptedRollingFileAppender。我重写了这个方法 setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize) 基本上,我使用CipherOutputStream和Base64OutputStream对写入输出流的所有

我在加密/解密log4j的RollingFileAppender生成的日志文件时遇到问题。对于我尝试扩展RollingFileAppender的加密,只需将其称为EncryptedRollingFileAppender。我重写了这个方法

setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
基本上,我使用CipherOutputStream和Base64OutputStream对写入输出流的所有内容进行加密和编码。以下是部分代码:

...
setImmediateFlush(true);

FileOutputStream ostream = null;
CipherOutputStream cstream = null;
Base64OutputStream b64stream = null;
try {        
    byte[] keyBytes = "1234123412341234".getBytes();  //example
    final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 
         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; //example

    final SecretKey key = new SecretKeySpec(keyBytes, "AES");
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key, IV);

    ostream = new FileOutputStream(fileName, true);
    b64stream = new Base64OutputStream(ostream);
    cstream = new CipherOutputStream(b64stream, cipher);

    } catch(Exception ex) {
        ex.printStackTrace();
    }

Writer cw = createWriter(cstream);
...
然后我用以下代码解密文件:

private static void decryptFile(String filename) throws Exception {
    FileInputStream fis = null;
    BufferedReader br = new BufferedReader(new FileReader(filename));

    File file = new File(filename + "-decrypted");
    file.createNewFile();
    Writer out = new OutputStreamWriter(new FileOutputStream(filename + "-decrypted"), "UTF-8");

    String line = null;
    try {
         while (( line = br.readLine()) != null){
             line = decrypt(Base64.decodeBase64(line));
             out.write(line);
         }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            br.close();
        }
        if (fis != null) {
            fis.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

public static String decrypt(byte[] line) throws Exception {
    byte[] keyBytes = "1234123412341234".getBytes();
    final byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
                0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };

    final SecretKey secretkey = new SecretKeySpec(keyBytes, "AES");
    final IvParameterSpec IV = new IvParameterSpec(ivBytes);
    final Cipher decipher = Cipher.getInstance("AES/CFB8/NoPadding");
    decipher.init(Cipher.DECRYPT_MODE, secretkey, IV);
    final byte[] plainText = decipher.doFinal(line);

    return new String(plainText, "UTF-8").trim();
}
它奏效了,但只是部分奏效。结果文件中的某些文本已正确解密,但其他一些文本未正确解密。如果你好奇,这就是我所说的部分:

07 Jul 11 13:13:13, DEBUG  MrBean.java:checkUserVal���̥V;��ƃ�˨�� - username: squall,password: 4GROmr95Qcf����v�M�7�y�5�@CGO09 ,active: true 
我也尝试过将算法改为“DESede”,但它仍然被部分解密。然后我尝试在两端使用“CBC/PKCS5Padding”,但我得到了一个例外

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
我假设加密没有正确地填充输入,但我想知道为什么。。。因为当我使用相同的加密和解密算法而不使用CipherOutputStream时,填充效果很好。 有人能帮我把这个做好吗?任何帮助都将不胜感激


对不起,我的英语不是我的母语。

看起来不错。是否为每条消息重置/初始化密码对象?您可能不想这样做,但如果这样做,则需要仔细考虑密码文件的结构,因为解密程序需要知道消息边界的位置。

不是每个消息都要这样做,但我认为在某些情况下密码会重新初始化。如果密钥、IV、算法、分组密码模式和填充没有更改,应该不会有问题,对吗?(就像上面的示例代码一样)这是一个问题。为了大大简化,您需要使用的“IV”对于密码流中的不同位置是不同的。有关详细信息,请参阅。嗯,我想如果我用同样的静脉注射会有用的,反正只是测试用的。但如果一定要有所不同,我想这会让一切变得更复杂。我必须在文件中写入IV,添加消息分隔符,等等。不起作用的填充怎么办?你有什么线索吗?@ordinarydot:我同意,使用单独的消息实体要复杂得多。NoPadding工作正常,几乎可以肯定问题完全是由于重置了密码实例。它现在已经在工作了。不过我采取了不同的方法。现在,我将所有类扩展到log4j的WriteAppender,然后将所有加密和编码直接放在writer中。我想,在不知道发生了什么的情况下处理CipherOutputStream太难了。现在我使用AES/ECB/PKCS5P添加,但我计划将其更改为CBC。对每条消息使用不同的IV,并添加必要的更改。嗯,非常感谢Greg,谢谢你的帮助。只是好奇:你为什么要使用Base64OutputStream?通常加密的文件是二进制格式的,将其设为ascii码没有任何好处。只是想一想,解密时不需要
CipherInputStream