henryhuang / winzipaes

Automatically exported from code.google.com/p/winzipaes
0 stars 0 forks source link

AESUtilsJCA(String password, int keySize, byte[] salt) masks NoSuchAlgorithmException message #45

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Compile and run the test program below with a SecurityManager installed with an 
empty policy (use policy==empty.policy)

Result: Test program stops with an Error with no error message at all.

Expected: Exception in thread "main" java.lang.Error: PBKDF2WithHmacSHA1 
SecretKeyFactory not available

It would be nice to know that it was a SecurityException under the covers but 
it appears that SecretKeyFactory doesn't wrap the underlying SecurityException.

The fix is simple: change this:

 /* 
  * XXX(mdempsky): Could happen if the user's JRE doesn't support PBKDF2,
  * AES, and/or HMAC-SHA1.  Throw a better exception?
  */
 throw new Error();

to this:

 /* 
  * XXX(mdempsky): Could happen if the user's JRE doesn't support PBKDF2,
  * AES, and/or HMAC-SHA1.  Throw a better exception?
  */
 throw new Error(e.getMessage(), e);

Honestly, you could probably just allow that method to throw 
GeneralSecurityException (or even a list of explicit exceptions that can be 
thrown), but that would, of course, change the API.

Original issue reported on code.google.com by schultz....@gmail.com on 15 Aug 2012 at 4:39

GoogleCodeExporter commented 9 years ago
Whoops, I forgot to attach the test driver:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

import de.idyl.winzipaes.AesZipFileEncrypter;
import de.idyl.winzipaes.impl.AESEncrypterJCA;

public class AESZipTest
{
    public static void main(String[] args)
        throws Exception
    {
        //int size = 1024 * 1024 * 1024; // 1GiB
        int size = 1024 * 1024; // 1MiB
        ByteBuffer bb = ByteBuffer.allocate(size);
        OutputStream out = System.out;
        InputStream in = new ByteArrayInputStream(bb.array());
        AesZipFileEncrypter encrypter = new AesZipFileEncrypter(out, new AESEncrypterJCA());
        encrypter.add("test.txt", in, "password");
        encrypter.close();
        out.flush();
        out.close();
    }
}

Original comment by schultz....@gmail.com on 15 Aug 2012 at 4:42

GoogleCodeExporter commented 9 years ago
thanks for your input

Original comment by olaf.merkert on 19 Aug 2012 at 7:05

GoogleCodeExporter commented 9 years ago

Original comment by olaf.merkert on 19 Aug 2012 at 7:08