Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Since System.Security.Cryptography.Xml classes are not available in the .Net CF, we are writing to find if there is a method to encrypt and decrypt data of an XML element(/s).

For example, in the below XML sample, is there a method to encrypt data of <author> element (string "TSKN" in this case)?

<books>
<book>
<title>ABCD</title>
<author>TSKN</author>
</book>
</books>

Using available Cryptographic APIs for .NetCF, there is a potential issue of generating invalid XML characters in the encrypted data, which implies that the partially encrypted XML string cannot be loaded in an XML document and parsed for decrypting the <author> node.

Any help is highly appreciated.

best regards.
Posted
Comments
Keith Barrow 13-Jun-11 15:06pm    
I haven't used the .NetCF, but you should be able to write your own cryptography provider that uses the existing ones and writes writes the HEX values of the bytes rather than the Bytes directly, that way you just end up with a bunch of hex.
TSKNaidu 15-Jun-11 5:49am    
Thanks Keith for the tip to use HEX values. It worked. We used Uri.HexEscape and HexUnescape methods to store the encrypted values as XML string.

Sergey Alexandrovich Kryukov 14-Jun-11 1:29am    
"Using available Cryptographic APIs for .NetCF, there is a potential issue of generating invalid XML characters in the encrypted data" -- who tell you this? Not true!
--SA
TSKNaidu 15-Jun-11 5:51am    
If you convert encrypted byte[] array to store in an XML document as a string, it is possible that invalid XML characters could be part of the encrypted string.

1 solution

If I understand the question, you want to encrypt just the values of the XML, not the entire file?

When dealing with XML, I typically use wrapper classes. Create a BookClass with a constructor that takes the XmlElement containing the data of a single <book> node. To illustrate (and my apologies for it being VB):

VB
Public Sub New(ByVal Elem As XmlElement)
    For Each N As XmlNode In Elem.ChildNodes
        If N.NodeType = XmlNodeType.Element Then
            Select Case N.Name
                Case "Title"
                    pTitle = N.InnerText
                Case "Author"
                    pAuthor= N.InnerText
            End Select
        End If
    Next
End Sub

The variables that start with p get exposed as properties.

Override ToString, or create some other function, to write BookClass's data back out as XML. Then, add methods that will encrypt and decrypt the data, using whatever method suits your needs; my toolbox uses the RijndaelManaged library in System.Security.Cryptography, which produces a string of hex digits that won't interfere with XML. When the node is written, it will not matter whether the data is encrypted or in plain text.

Finally, write methods that will read your file into a List<BookClass> and write the list back out as a file. If you wanted to be fancy, you could encapsulate all this into a single Library class that inherits from List<BookClass>.

Now, your workflow would look like:

1. Read file and return a list or Library.
2. For each BookClass, call the class' Decrypt method.
3. Use the XML.

If you make changes and need to resave the data, just call Encrypt for each BookClass, then write the list back out.
 
Share this answer
 
v2
Comments
TSKNaidu 15-Jun-11 5:47am    
Hi!

Thank you for taking time to respond to my question.

Yes. The solution works when the XML schema is fixed.

regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900