outputstream的Flush方法什么都不做

时间:2012-03-27 18:22:21

标签: java outputstream

尝试包含部分加密文件的内容。一切都很好,除了这个小问题。由于某些原因。 flush方法确实有效,直到n> 52其中n是循环计数。您可以在解密方法中看到它。如果我改变n fomr< 10至< 53它冲了过来。我通过查看文件来测试它。直到53年才添加新内容。但应该有。

public class DesEncrypter {
Cipher ecipher;
Cipher dcipher;

DesEncrypter(SecretKey key) {
    // Create an 8-byte initialization vector
    byte[] iv = new byte[]{
        (byte)0x8E, 0x12, 0x39, (byte)0x9C,
        0x07, 0x72, 0x6F, 0x5A
    };
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    try {
        ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

        // CBC requires an initialization vector
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (java.security.InvalidAlgorithmParameterException e) {
    } catch (javax.crypto.NoSuchPaddingException e) {
    } catch (java.security.NoSuchAlgorithmException e) {
    } catch (java.security.InvalidKeyException e) {
    }
}

// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out) {
    try {
        // Bytes written to out will be encrypted
       AppendableOutputStream out_append = new AppendableOutputStream(out);

       OutputStream out_c = new CipherOutputStream(out_append, ecipher);

        // Read in the cleartext bytes and write to out to encrypt
        int numRead = 0;
        int count = 0;
        int max = 1024;
        boolean first = true;

        while ((numRead = in.read(buf, 0, max)) > 0) {                 
            System.out.println("running Total: " + count);
            count += numRead;
            // if this read puts as at less than a meg, encrypt
            if(count <= 1024*1024){
                System.out.println("encrypted " + numRead + " of " + max +" bytes : total " + count);
                out_c.write(buf, 0, numRead);
                // last encryption pass, close buffer and fix max
                if(count == 1024*1024){
                    // fix reading 1k in case max was decreased
                    max = 1024;
                    out_c.close();
                }
                // if next read will go over a meg, read less than 1k
                else if(count + max > 1024*1024)
                    max = 1024*1024 - count;
            }
            // past the first meg, don't encrypt
            else{
                System.out.println("processed " + numRead + " of " + max +" bytes : total " + count);
                out.write(buf, 0, numRead);
            }

        }
        out.close();

    } catch (java.io.IOException e) {}

}

// Movies encrypt only 1 MB 128 passes.

public void decrypt(InputStream in, OutputStream out) {
    try {
        // Bytes read from in will be decrypted
        InputStream in_c = new CipherInputStream(in, dcipher);

        // Read in the decrypted bytes and write the cleartext to out
        int numRead = 0;
        int count = 0;
        int max = 1024;

        while ((numRead = in_c.read(buf, 0, max)) > 0) {
            count += numRead;
            System.out.println("decrypted " + numRead + " of " + max +" bytes : total " + count);
            out.write(buf, 0, numRead);
            if(count + max > 1024*1024){
                max = 1024*1024 - count;
            }
            if(count == 1024*1024)
                max = 0;
        }

        //in.skip(count);
        int n = 0;
        while((numRead = in.read(buf)) > 0 && n < 10){
        count += numRead;
        System.out.println("processed " + numRead + " of 1024 bytes : total " + count);
            out.write(buf,0,numRead);
            //System.out.println("buf"+buf.length  +" numered" + numRead+ " n"+n);
            // If i look at the file after anything under n < 51 the file  doesn't change.
            n++;

        }
        out.flush();
        out.close();
    } catch (java.io.IOException e) {
        System.out.println("AHHHHHHHH!!!!!!");
    }
}

2 个答案:

答案 0 :(得分:1)

根据给定的信息,我只能推测。如果您在调用OutputStream方法时指定了out decrypt的类型,那么至少会非常有用。 flush()方法的行为因具体实现而异。

例如,如果输出流恰好是CipherOutputStream,或者是连接到该类型流的其他流,那么其flush()方法的文档说明了以下内容(重要部分)我强调):

  

通过强制写出已由封装的密码对象处理的任何缓冲的输出字节来刷新此输出流。   由封装的密码缓冲并等待由它处理的任何字节都不会被写出。例如,如果封装的密码是块密码,并且使用其中一种写入方法写入的总字节数小于密码的块大小,则不会写出任何字节。

这当然听起来可能是你问题的原因。但是,正如我所说,了解有关您的特定流类型的更多详细信息将非常有用。

答案 1 :(得分:0)

好吧,我正在做的事情似乎解密方法中的输入流正在弄乱数据。当它被回读时,我没有解密它,而是在它被处理回来时解密它。填充物也有点搞乱了。必须在解密方法中为1024 * 1024代码添加额外的8个字节。 如果有人关心这里是修改后的方法 谢谢大家的帮助。

    public void encrypt(InputStream in, OutputStream out) {
    try {
        // Bytes written to out will be encrypted
       AppendableOutputStream out_append = new AppendableOutputStream(out);

       OutputStream out_c = new CipherOutputStream(out_append, ecipher);

        // Read in the cleartext bytes and write to out to encrypt
        int numRead = 0;
        int count = 0;
        int max = 1024;
        boolean first = true;

        while ((numRead = in.read(buf, 0, max)) > 0) {                 
            //System.out.println("running Total: " + count);
            count += numRead;
            // if this read puts as at less than a meg, encrypt
            if(count <= 1024*1024){
                //System.out.println("encrypted " + numRead + " of " + max +" bytes : total " + count);
                out_c.write(buf, 0, numRead);
                // last encryption pass, close buffer and fix max
                if(count == 1024*1024){
                    // fix reading 1k in case max was decreased
                    max = 1024;
                    out_c.close();
                }
                // if next read will go over a meg, read less than 1k
                else if(count + max > 1024*1024)
                    max = 1024*1024 - count;
            }
            // past the first meg, don't encrypt
            else{
                //System.out.println("processed " + numRead + " of " + max +" bytes : total " + count);
                out.write(buf, 0, numRead);
            }

        }
        out.flush();

        out.close();

    } catch (java.io.IOException e) {

        System.out.println("AHHHHHHHH!!!!!!111");

    }

}

// Movies encrypt only 1 MB 128 passes.

public void decrypt(InputStream in, OutputStream out) {
    try {
        // Bytes written to out will be decrypted
        AppendableOutputStream out_append = new AppendableOutputStream(out);
        System.out.println(ecipher.getOutputSize(1024*1024));
        OutputStream out_d = new CipherOutputStream(out_append, dcipher);

        // Read in the decrypted bytes and write the cleartext to out
        int numRead = 0;
        int count = 0;
        int max = 1024;

        while ((numRead = in.read(buf, 0, max)) > 0) {
            count += numRead;
            if(count <= ecipher.getOutputSize(1024*1024)){
                out_d.write(buf, 0, numRead);
                // last encryption pass, close buffer and fix max
                if(count == ecipher.getOutputSize(1024*1024)){
                    // fix reading 1k in case max was decreased
                    max = 1024;
                    out_d.close();
                }
                // if next read will go over a meg, read less than 1k
                else if(count + max > ecipher.getOutputSize(1024*1024))
                    max = ecipher.getOutputSize(1024*1024) - count;
            }
            // past the first meg, don't decrypt
            else{
                out.write(buf, 0, numRead);
            }

        }
        out.close();
    } catch (java.io.IOException e) {
    }
}