Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Encoding XML file using Base64Encoder

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic