• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1002
  • Last Modified:

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);
       edata.getParentNode().replaceChild(decNode,edata);
}catch(org.xml.sax.SAXParseException spe) {
      //In case the content is plain text
      //or a group of child elements
      Text decText = encDoc.createTextNode(decbit);
      edata.getParentNode().replaceChild(decText,edata);
}
      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" />
                  </carrierRole>
               </author>
            </policyOrAccount>
         </beneficiaryOf>
---
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

regards
Mohammad
   
0
mnrz
Asked:
mnrz
  • 6
  • 5
1 Solution
 
BigRatCommented:
>>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?
0
 
mnrzAuthor Commented:
Hi
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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <controlActProcess xmlns="urn:hl7-org:v3">
    <subject typeCode="SUBJ">
      <registrationEvent classCode="REG" moodCode="EVN">
        <subject1>
          <patient classCode="PAT">
            <id root="100214" />
            <addr>
              <country>&#1575;&#1576;&#1585;&#1575;&#1606;</country>
              <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>
              <postalCode>555411123</postalCode>
            </addr>
            <telecom value="88741145" use="H" />
            <telecom value="09122544147" use="MC" />
            <telecom value="554122387" use="WP" />
            <patientPerson classCode="PSN" determinerCode="INSTANCE">
              <id root="1378554107" />
              <name>
                <given>&#1605;&#1581;&#1605;&#1583; &#1585;&#1590;&#1575;</given>
                <family>&#1575;&#1581;&#1605;&#1583;&#1576;</family>
              </name>
              <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>
                        </underwritingCarrierOrganization>
                      </carrierRole>
                    </author>
                    <limitation xsi:nil="true" />
                  </policyOrAccount>
                </beneficiaryOf>
              </asCoveredParty>
              <birthPlace xsi:nil="true" />
            </patientPerson>
            <providerOrganization determinerCode="INSTANCE">
              <name />
            </providerOrganization>
          </patient>
        </subject1>
        <author xsi:nil="true" />
        <custodian xsi:nil="true" />
      </registrationEvent>
    </subject>
  </controlActProcess>
</PRPA_IN201101UV01.MCCI_MT000100UV01.Message>
0
 
BigRatCommented:
>>whereas I put Strings in my language which is created in utf-8 encoding this problem happen

>>>&#1605;&#1576;&#1583;&#1575;&#1606;

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!

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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.

thanks
0
 
BigRatCommented:
>>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.
0
 
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);
edata.getParentNode().replaceChild(decText,edata);

here is all the story.

thanks
0
 
BigRatCommented:
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?)
0
 
mnrzAuthor Commented:

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

0
 
BigRatCommented:
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.
0
 
mnrzAuthor Commented:

BigRat

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.

regards
mohammad
0
 
mnrzAuthor Commented:

BigRat

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.

Regards
mohammad
0
 
ee_ai_constructCommented:
Closed, 400 points refunded.
ee_ai_construct
Community Support Moderator
replacement part #xm34
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now