Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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: 43026
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.
 
Sheriff
Posts: 26798
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