I want to encrypt & decrypt an XML file. The following is the code I tried.
I have used a file one.xml It is encrypted & two.xml is created two.xml is decrypted & creates three.xml which should be like one.xml But Iam getting many junk characters in three.xml and also many Alphabets are replaced by junk characters.
The content of the file one.xml file was:
Sometimes Iam also getting the following error javax.crypto.BadPaddingException: Given final block not properly padded Exception in thread "main" java.lang.NullPointerException at java.lang.String.<init>(Unknown Source) at Enc.doCipher(Enc.java:121) at Enc.decrypt(Enc.java:91) at Enc.main(Enc.java:68)
Waiting for reply. Please reply ASAP. Thanks in Advance.
[Edited to include code tags - Paul Sturrock] [ August 22, 2006: Message edited by: Paul Sturrock ]
My guess would be that the problem is that the encrypted text is stored in a String, both on writing and reading. You need to use byte instead, to avoid issues arising from encoding/decoding between byte and String.
Originally posted by Ulf Dittmer: My guess would be that the problem is that the encrypted text is stored in a String, both on writing and reading. You need to use byte instead, to avoid issues arising from encoding/decoding between byte and String.
Yes, indeed. That code is full of FileReaders and String.getBytes() that use the default encoding, for a start, and that could mess up an XML document. But the more serious problem is the conversion of an enciphered byte array -- which is not text -- into a String, which is pretty much guaranteed to produce garbage. I would agree with your guess; the code should be rewritten to use InputStreams rather than Readers and byte arrays rather than Strings throughout.