Problem in Xml Encryption and Decryption

Hi Experts

I have problem in decryption an encrypted Xml file. because my Xml file contains utf-8 characters, this problem occurs. after many proccess on Xml data the following code throws an Exception:

try {
        // Works if the content is a single child element.
       byte [] a = decbit.getBytes("UTF-8");
       ByteArrayInputStream bais2 = new ByteArrayInputStream(a);
       Document decdoc = docBuilder.parse(bais2);
       Node decNode =
                                          encDoc.importNode(decdoc.getFirstChild(), true);
}catch(org.xml.sax.SAXParseException spe) {
      //In case the content is plain text
      //or a group of child elements
      Text decText = encDoc.createTextNode(decbit);
      decString = (getString((XmlDocument)encDoc));
at the line with code "Document decdoc = docBuilder.parse(bais2); " an Exception occurs with message :
org.xml.sax.SAXParseException: Illegal character at end of document, <.

whereas when I debug the project the filed "decbit", contains correct data. any way, in order to the exception, the control move to "catch" block and create a Text Node but the problem is the replaced data is like this:

<id root="588588588" extension="" displayable="false" /><beneficiaryOf typeCode="BEN">
            <policyOrAccount classCode="COV" moodCode="EVN">
               <id xsi:type="II" root="855855855" extension="" displayable="false" />
               <author typeCode="AUT">
                  <carrierRole classCode="UNDWRT">
                     <id root="6548888888" extension="" displayable="false" />
as you can see, tha "<" characters are &lt; and the ">" characters are &gt;
this really exhausted me, please help me out.

thank you, any help will be appreciated

Who is Participating?
ee_ai_constructConnect With a Mentor Commented:
Closed, 400 points refunded.
Community Support Moderator
replacement part #xm34
>>after many proccess on Xml data

This is your problem. The code you have is correct when handling a file containing XML data in UTF-8 character set. Your file looks as if it has been "html"ed, ie processed so that it can be displayed on an HTML page. Where does the file come from and what did it look like originally?
mnrzAuthor Commented:
No, the file created by myself and everything in the Xml file is Ok. when I am using English data in that Xml it works properly whereas I put Strings in my language which is created in utf-8 encoding this problem happen.

the Xml file content :
<?xml version="1.0" encoding="utf-8"?>
<PRPA_IN201101UV01.MCCI_MT000100UV01.Message xmlns:xsi="" xmlns:xsd="">
  <controlActProcess xmlns="urn:hl7-org:v3">
    <subject typeCode="SUBJ">
      <registrationEvent classCode="REG" moodCode="EVN">
          <patient classCode="PAT">
            <id root="100214" />
              <city />
              <streetAddressLine>&#1605;&#1576;&#1583;&#1575;&#1606; &#1607;&#1601;&#1578; &#1578;&#1576;&#1585; &#1582;&#1576;&#1575;&#1576;&#1575;&#1606; &#1587;&#1604;&#1576;&#1605;&#1575;&#1606; &#1582;&#1575;&#1591;&#1585; &#1705;&#1608;&#1670;&#1607; &#1662;&#1606;&#1580;&#1605; &#1588;&#1585;&#1602;&#1576; &#1662;&#1604;&#1575;&#1705; 7</streetAddressLine>
            <telecom value="88741145" use="H" />
            <telecom value="09122544147" use="MC" />
            <telecom value="554122387" use="WP" />
            <patientPerson classCode="PSN" determinerCode="INSTANCE">
              <id root="1378554107" />
                <given>&#1605;&#1581;&#1605;&#1583; &#1585;&#1590;&#1575;</given>
              <administrativeGenderCode displayName="&#1605;&#1585;&#1583;" />
              <asCoveredParty classCode="COVPTY">
                <id root="87445127412" />
                <beneficiaryOf typeCode="BEN">
                  <policyOrAccount classCode="COV" moodCode="EVN">
                    <holder xsi:nil="true" />
                    <author typeCode="AUT">
                      <carrierRole classCode="UNDWRT">
                        <underwritingCarrierOrganization determinerCode="INSTANCE">
                          <name>&#1578;&#1575;&#1605;&#1576;&#1606; &#1575;&#1580;&#1578;&#1605;&#1575;&#1593;&#1576;</name>
                    <limitation xsi:nil="true" />
              <birthPlace xsi:nil="true" />
            <providerOrganization determinerCode="INSTANCE">
              <name />
        <author xsi:nil="true" />
        <custodian xsi:nil="true" />
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

>>whereas I put Strings in my language which is created in utf-8 encoding this problem happen


What am I seeing here? OK, so the characters are Arabic. If you open the file in Notepad (on at least Windows 2000 NOT Windows 98) should see the arabic characters properly. If in Notepad you see these sequences of ampersands(&) and numbers then the encoding is incorrect. Encoding like &#1575; is an HTML entity.

Now assuming that the file is OK, why did you not parse it like :-

Document doc = docBuilder.parse (new File("decbit.xml"));

is it because you have it as a string already? I ask because the getBytes method returns ONLY the bottom 8-bits of each Unicode character and that is bound to go wrong!

mnrzAuthor Commented:

the content in notepad or other editor is correct, here in this site ,when I copy and paste the content here the data has been changed.
about the parsing like :
Document doc = docBuilder.parse (new File("decbit.xml"));

I cant do that, because the data is encrypted and first of all I should decrypt it and then add that decrypted section to the main content.

>>I cant do that, because the data is encrypted

What exactly do you mean by "encrypted"?

It is perfectly normal for XML files to be *encoded* in UTF-8 and all XML parsers MUST handle UFT-8 format. Packing a UTF-8 file into a byte array is NOT going to work, since the getBytes() method looses the top 8 bytes of each 16-bit Unicode character.
mnrzAuthor Commented:
ok let me tell you exactly what I am doing.

there is some way to encrypt Xml data, encrypting all document, encrypting an element, encrypting an element content. for example, I am encrypting an element content of my Xml document for security reasons and then apply a digital signature on that encrypted data and send this document to another client. over side, it has its own private key to decrypt the received Xml document.

encrypting document is working properly but when I want to decrypt the Xml data this problem occurs. first I load the encrypted data as an InputStream and after decrypting process and change the data back to human readable format. after that, it's going to  build a new Xml document with no encrypted data.
when I debug the codes, I can see that the data decrypted correctly and no weird character or anything wrong else exists but when I try to append that decrypted section to whole document this happen.
as you can see in above code, I put a try-catch block so if the parser could not to parse my data, I append that data as a text node to Xml document and always this exception occurs and cause to create a text node, but this text node made data weird and (html)ed.
I mean always the following line is  running:

Text decText = encDoc.createTextNode(decbit);

here is all the story.

OK, so that means that decbit is a Java string containing the human readable XML. If so it contains Arabic characters which are full 16-bit characters and you may NOT convert them into a byte array with getBytes(). I suggest you just parse the string with :-

    Document decdoc = docBuilder.parse(decbit);

since I believe parse is overloaded to take string. (Which parser are you using?)
mnrzAuthor Commented:

but this parser has no overloaded method with String.
I am using DocumentBuilderFactory.

I fail to understand why they (Sun) don't provide a simple parse(String) method, instead of having to do this sort of thing :-

StringReader sr = new StringReader(decbit);

InputSource is = new InputSource(sr);

Document decdoc = docBuilder.parse(is);

Try that.
mnrzAuthor Commented:


excuse me for taking long my reply, because I am very busy at work and I didnt had time to check this, but now, I used "StringReader" as you told me, but unfortunaely it still doesnt work.

I really confused about this!!

thank you for your respond and excuse me again.

mnrzAuthor Commented:


after hours of toiling, at last I decided to replace all "&lt;" with "<" and "&gt;" with ">" in the final string that going to be written to file as a Xml document
and this resolved my problem.

thank you so much BigRat for spending your valuable times and trying to help me.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.