• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encoding XML file using Base64Encoder

 
shiva kumar
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20166
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic