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

ord*_*dot 10 java encryption log4j file

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

setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
Run Code Online (Sandbox Code Playgroud)

基本上我使用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);
...
Run Code Online (Sandbox Code Playgroud)

然后我用这段代码解密文件:

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();
}
Run Code Online (Sandbox Code Playgroud)

它工作但只是部分.结果文件中的某些文本已正确解密,但其他文本未正确解密.如果你很好奇,这就是我的意思:

07 Jul 11 13:13:13, DEBUG  MrBean.java:checkUserVal????V;??????? - username: squall,password: 4GROmr95Qcf????v?M?7?y?5?@CGO09 ,active: true 
Run Code Online (Sandbox Code Playgroud)

我也尝试将算法更改为"DESede",但它仍然被部分解密.然后我尝试在两端使用"CBC/PKCS5Padding",但我得到了一个例外

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
Run Code Online (Sandbox Code Playgroud)

我假设加密没有正确填充输入,但我想知道为什么......因为当我使用相同的加密和解密算法而不使用CipherOutputStream时,填充工作正常.任何人都可以帮助我做这个工作?任何帮助将不胜感激.

PS:对不起我的英语,这不是我的母语.

Jam*_*olk 4

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

  • 这是一个问题。为了大大简化,对于密码流中的不同位置,您需要使用的“IV”是不同的。有关详细信息,请参阅[维基百科页面](https://secure.wikimedia.org/wikipedia/en/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29) (2认同)