在没有System.Security.Cryptography.Xml类的情况下加密XML数据 [英] Encrypting XML data without System.Security.Cryptography.Xml classes

查看:96
本文介绍了在没有System.Security.Cryptography.Xml类的情况下加密XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于.Net CF中没有System.Security.Cryptography.Xml类,因此我们正在寻找是否有一种方法可以加密和解密XML元素的数据.

例如,在下面的XML示例中,有一种方法可以对< author>的数据进行加密.元素(在这种情况下为字符串"TSKN")?

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

使用适用于.NetCF的加密API,存在一个潜在问题,即在加密数据中生成无效的XML字符,这意味着部分加密的XML字符串无法加载到XML文档中,并且无法解析以解密< author>节点.

非常感谢您的帮助.

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.

推荐答案

如果我理解这个问题,您只想加密XML的,而不是整个文件?

在处理XML时,我通常使用包装器类.使用构造函数创建一个BookClass,该构造函数采用包含单个<book>节点数据的XmlElement.为了说明(对我的歉意是VB):

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):

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


p开头的变量作为属性公开.

覆盖ToString或创建其他功能,以将BookClass的数据写回XML.然后,使用适合您需要的任何方法,添加将对数据进行加密和解密的方法.我的工具箱使用了System.Security.Cryptography中的RijndaelManaged库,该库生成了一个不会干扰XML的十六进制数字字符串.写入节点时,数据是加密的还是纯文本的都没有关系.

最后,编写将文件读入List<BookClass>并将列表写回作为文件的方法.如果想花哨的话,可以将所有这些封装到一个从List<BookClass>继承的Library类中.
现在,您的工作流程将如下所示:

1.读取文件并返回列表或Library.
2.对于每个BookClass,调用类的Decrypt方法.
3.使用XML.

如果进行更改并需要重新保存数据,只需为每个BookClass调用Encrypt,然后将列表写回即可.


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.


这篇关于在没有System.Security.Cryptography.Xml类的情况下加密XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆