File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Encoding XML file using Base64Encoder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encoding XML file using Base64Encoder " Watch "Encoding XML file using Base64Encoder " New topic
Author

Encoding XML file using Base64Encoder

shiva kumar
Greenhorn

Joined: Aug 22, 2006
Posts: 7
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.


Code:


[Edited to include code tags - Paul Sturrock]
[ August 22, 2006: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"sivaabc sivaabc"

Welcome to JavaRanch! Can you please update your display name so it complies with our Naming Policy. You can do this here.

Thanks!


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42594
    
  65
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.


Ping & DNS - my free Android networking tools app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18880
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encoding XML file using Base64Encoder