javax.crypto not working properly; on linux ;-)

Marco Trudel mtrudel@gmx.ch
Thu Sep 28 22:38:00 GMT 2006


Hello list

Is there some active development on javax.crypto? Is this question 
better placed at the gnu classpath list (is there a list at all? I don't 
really understand the page)?

On windows it doesn't work at all. On linux I have problems as well. 
These two method bodies do exactly the same:

{
    File inFile = new File("test.mpg");
    File outFile = new File("test.enc");
    Cipher cipher = createCipher("helloWorld", Cipher.ENCRYPT_MODE);
		
    FileInputStream inputStream = new FileInputStream(inFile);
    FileOutputStream outputStream = new FileOutputStream(outFile);
    byte[] ba = new byte[1024 * 1024]; // 1mb
		
    while(true)
    {
       int len = inputStream.read(ba);
       if(len < 0) break;
       outputStream.write(cipher.update(ba, 0, len));
    }
    outputStream.write(cipher.doFinal());
		
    outputStream.flush();
    outputStream.close();
    inputStream.close();
}

{
    File inFile = new File("test.mpg");
    File outFile = new File("test.enc");
    Cipher cipher = createCipher("helloWorld", Cipher.ENCRYPT_MODE);
		
    FileInputStream inputStream = new FileInputStream(inFile);
    CipherOutputStream outputStream = new CipherOutputStream(new 
FileOutputStream(outFile), cipher);
    byte[] ba = new byte[1024 * 1024]; // 1mb
		
    while(true)
    {
       int len = inputStream.read(ba);
       if(len < 0) break;
       outputStream.write(ba, 0, len);
    }
		
    outputStream.flush();
    outputStream.close();
    inputStream.close();
}

private static Cipher createCipher(String password, int mode) throws 
Exception
{
    byte[] baKey = 
MessageDigest.getInstance("MD5").digest(password.getBytes());
    SecretKey secretKey = new SecretKeySpec(baKey, "AES");

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(mode, secretKey);

    return cipher;
}


Only the first one works with GNU classpath. The second gives:

Exception in thread "main" java.lang.NullPointerException
    at gnu.javax.crypto.jce.cipher.CipherAdapter.engineUpdate(CryptTest-lin)
    at javax.crypto.Cipher.update(CryptTest-lin)
    at javax.crypto.Cipher.update(CryptTest-lin)
    at javax.crypto.CipherOutputStream.process(CryptTest-lin)
    at javax.crypto.CipherOutputStream.write(CryptTest-lin)
    at CryptTest.main(CryptTest-lin)


According decrypting doesn't work at all. Using CipherInputStream 
results in:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at gnu.javax.crypto.pad.PKCS7.unpad(CryptTest-lin)
    at 
gnu.javax.crypto.jce.cipher.CipherAdapter.engineDoFinal(CryptTest-lin)
    at javax.crypto.Cipher.doFinal(CryptTest-lin)
    at javax.crypto.Cipher.doFinal(CryptTest-lin)
    at javax.crypto.CipherInputStream.nextBlock(CryptTest-lin)
    at javax.crypto.CipherInputStream.read(CryptTest-lin)
    at javax.crypto.CipherInputStream.read(CryptTest-lin)
    at CryptTest.main(CryptTest-lin)

Working direct with the cipher results in:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at gnu.javax.crypto.pad.PKCS7.unpad(CryptTest-lin)
    at 
gnu.javax.crypto.jce.cipher.CipherAdapter.engineDoFinal(CryptTest-lin)
    at javax.crypto.Cipher.doFinal(CryptTest-lin)
    at javax.crypto.Cipher.doFinal(CryptTest-lin)
    at CryptTest.main(CryptTest-lin)


I checked the GNU classpath CVS. CipherInputStream, CipherOutputStream 
and Cipher are all pretty old so I guess they're all up to date in the 
latest GCJ source.

So, my questions:
- Is there someone actively maintaining javax.crypto?
- Is there someone who could guide me fixing it?
- Are there already patches/fixex I could backport for GCJ?


thanks
Marco



More information about the Java mailing list