views:

593

answers:

1

I am decrypting an XML file from the file system using Bouncy Castle. I output the decrypted text and get a fatal error SAXParseException on the very last byte of data. Below is my decryption method and the setup of the cipher object.

I was initially using cipher streams, and everything worked perfect (commented out code was my stream). Due to policy files and end users not having the 256 bit unlimited versions I need to use bouncy castle.

Any ideas why the final byte is not coming through?

From Constructor:

keyParam = new KeyParameter(key);
engine = new AESEngine();
paddedBufferedBlockCipher = 
    new PaddedBufferedBlockCipher(new CBCBlockCipher(engine));

Decrypt Method:

public void decrypt(InputStream in, OutputStream out) {
    try
    {
        paddedBufferedBlockCipher.init(false, 
            new ParametersWithIV(keyParam, _defaultIv));
//    cipher.init(Cipher.DECRYPT_MODE, secretKey, ivs);
//    CipherInputStream cipherInputStream 
//                      = new CipherInputStream(in, cipher);

        byte[] buffer = new byte[4096];
        byte[] outBuffer = new byte[4096];

        for (int count = 0; (count = in.read(buffer)) != -1;) {
            paddedBufferedBlockCipher.processBytes(buffer, 0, 
                count, outBuffer, 0);
            out.write(outBuffer, 0, count);   
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

[Fatal Error] :40:23: Element type "Publi" must be followed by either attribute specifications, ">" or "/>".
org.xml.sax.SAXParseException: Element type "Publi" must be followed by either attribute specifications, ">" or "/>".
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:264)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292)
+1  A: 

Do you call doFinal() with the final chunk of data?

public void decrypt(InputStream in, OutputStream out) {
    try
    {
        paddedBufferedBlockCipher.init(false, 
            new ParametersWithIV(keyParam, _defaultIv));
        byte[] buffer = new byte[4096];
        byte[] outBuffer = new byte[4096];

        for (int count = 0; (count = in.read(buffer)) != -1;) {
            int c2 = paddedBufferedBlockCipher.processBytes(buffer, 0, 
                count, outBuffer, 0);
            out.write(outBuffer, 0, c2);                     
        }
        count = paddedBufferedBlockCipher.doFinal(outBuffer, 0);
        out.write(outBuffer, 0, count);                     
   }
    catch(Exception e) {
        e.printStackTrace();
    }
}
kd304
Adding those two lines at the end does make the output correct. I get the entire XML file. But still get the same SAXParseException as above even though the file looks good
I am trying to deserialize the XML to an object right after the decrypt is finished
I actually have two XML files I tried. One gives the exception above. The other file gives "An invalid XML character (Unicode: 0x0) was found in the element content of the document."
how about this version above with c2. Can you show us a runnable test program (with the things initialized) so we can test it too?
kd304
I dont have a runnable test program at this time I would have make one pulling functions from a few different classes. The code bombs on the line "Document doc = db.parse(new InputSource(new StringReader(xml)));". There are no exceptions in the decryption or errors that I can see in the display to system out.
Could you save the decrypted output and check the file size equals to the original file size? I'm 99% certain that your encryption does not correctly encrypt the entire input file. Have a look at this example: http://osdir.com/ml/encryption.bouncy-castle.devel/2006-12/msg00129.html
kd304
The encryption is done in C#. The same exact file decrypts and deserializes perfect on Windows using the cipher streams (with the unlimited policy files installed). The only variables that have changed are me being on a Mac, original policy files, and using BouncyCastle. I never saw any parsing errors like this until BouncyCastle was introduced. I will keep investigating and make a runnable test program