justinludwig / jpgpj

Java Pretty Good Privacy Jig
MIT License
75 stars 20 forks source link

Buffer size of zero passed to BufferedInputStream in Encryptor.java line 343 results in exception. #23

Closed bclark76 closed 5 years ago

bclark76 commented 5 years ago

Upgrading from 0.1.1 to 0.7 I got this during testing my code:

java.lang.IllegalArgumentException: Buffer size <= 0 at java.io.BufferedInputStream.(BufferedInputStream.java:201) at org.c02e.jpgpj.Encryptor.encrypt(Encryptor.java:343)

Encryptor.class has:

        int bestFileBufferSize =
            Util.bestFileBufferSize(plaintext.length(), maxFileBufferSize);
        input = new BufferedInputStream(
            new FileInputStream(plaintext), bestFileBufferSize);

It's passing zero as the buffer size for zero byte plaintexts.

Seems BufferedInputStream has:

public BufferedInputStream(InputStream in, int size) {
    super(in);
    if (size <= 0) {
        throw new IllegalArgumentException("Buffer size <= 0"); // this is being thrown
    }
    buf = new byte[size];
}

Is it possible to pass a buffer size of 1 if the buffer size would be zero?

justinludwig commented 5 years ago

Thanks for the bug report! You're right, it should use a buffer size of 1 instead of 0. I'll release a fix for it tonight.

justinludwig commented 5 years ago

I fixed this, and released it as version 0.7.1. Thanks again for the detailed bug report!

bclark76 commented 5 years ago

Wow, that was quick! Thanks! Fixed my issue!