| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 加密压缩编程 >> 实例分析正文
最新推荐 更多内容
VB.Net对XML元素进行加密解密的实例
VB.Net对XML元素进行加密解密的实例
作者:Ilu    来源:乐博网整理     更新时间:2009-8-27

VB.Net对XML元素进行加密解密的代码如下:

Imports System.Xml
Imports System.Security.Cryptography
'需要在“添加引用”对话框中添加对“System.Security”的引用
Imports System.Security.Cryptography.Xml
Imports System.IO
Public Class Form1
    Private MyKey As RijndaelManaged
    Private MyXmlDoc As XmlDocument
    '读取“MyXML.xml”文件中的内容
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.textBox1.Lines = System.IO.File.ReadAllLines("MyXML.xml")
        MyKey = New RijndaelManaged()
        MyXmlDoc = New XmlDocument()
        MyXmlDoc.PreserveWhitespace = True
    End Sub
    '对XML元素进行加密
    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        'Dim MyReader As TextReader = New StringReader(Me.textBox1.Text)
        'MyXmlDoc.Load(MyReader)
        'MyReader.Close()
        MyXmlDoc.Load("MyXML.xml")
        Dim MyEncryptElement = "客户"
        If (MyXmlDoc Is Nothing) Then
            Throw New ArgumentNullException("MyXmlDoc")
        End If
        If (MyEncryptElement Is Nothing) Then
            Throw New ArgumentNullException("ElementToEncrypt")
        End If
        If (MyKey Is Nothing) Then
            Throw New ArgumentNullException("MyKey")
        End If
        Dim MyEncrypt As XmlElement = CType(MyXmlDoc.GetElementsByTagName(MyEncryptElement)(0), XmlElement)
        If (MyEncrypt Is Nothing) Then
            Throw New XmlException("加密元素在XML文件中不存在!")
        End If
        Dim MyEncryptedXml As New EncryptedXml()
        Dim MyXmlElement As Byte() = MyEncryptedXml.EncryptData(MyEncrypt, MyKey, False)
        Dim MyEncryptedData As New EncryptedData()
        MyEncryptedData.Type = EncryptedXml.XmlEncElementUrl
        Dim MyEncryptionMethod = ""
        Select Case MyKey.KeySize
            Case 128  '更多vb.net源码和实例,请关注{乐 博 网 lob.cn}
                MyEncryptionMethod = EncryptedXml.XmlEncAES128Url
            Case 192
                MyEncryptionMethod = EncryptedXml.XmlEncAES192Url
            Case 256
                MyEncryptionMethod = EncryptedXml.XmlEncAES256Url
        End Select
        MyEncryptedData.EncryptionMethod = New EncryptionMethod(MyEncryptionMethod)
        MyEncryptedData.CipherData.CipherValue = MyXmlElement
        EncryptedXml.ReplaceElement(MyEncrypt, MyEncryptedData, False)
        Me.textBox1.Text = MyXmlDoc.InnerXml
        MyXmlDoc.Save("MyEncryptXML.dat")
    End Sub
    '对XML元素进行解密
    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        'Dim MyReader As TextReader = New StringReader(Me.textBox1.Text)
        'MyXmlDoc.Load(MyReader)
        'MyReader.Close()
        MyXmlDoc.Load("MyEncryptXML.dat")
        If (MyXmlDoc Is Nothing) Then
            Throw New ArgumentNullException("Doc")
        End If
        If (MyKey Is Nothing) Then
            Throw New ArgumentNullException("Alg")
        End If

        Dim MyXmlElement As XmlElement = CType(MyXmlDoc.GetElementsByTagName("EncryptedData")(0), XmlElement)
        If (MyXmlElement Is Nothing) Then
            Throw New XmlException("没有找到EncryptedData加密元素!")
        End If
        Dim MyEncryptedData As New EncryptedData()
        MyEncryptedData.LoadXml(MyXmlElement)
        Dim MyEncryptedXml As New EncryptedXml()
        Dim MyBytes As Byte() = MyEncryptedXml.DecryptData(MyEncryptedData, MyKey)
        MyEncryptedXml.ReplaceData(MyXmlElement, MyBytes)
        Me.textBox1.Text = MyXmlDoc.InnerXml
        MyXmlDoc.Save("MyXML.xml")
    End Sub
End Class

  • 上一篇:

  • 下一篇:
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    VB.Net获取随机密码的实例
    VB.Net用Salt哈希数据的实例
    用DPAPI加密和解密数据的实例
    储存加密的数据库连接字符串到注册表的实例
    Salt加密数据的.Net实例
    Rijndael加密解密的实例
    AES加密的实例
    Rijndael加密的实例
    SHA256 Hash加密的实例
    VB.Net文件CRC32算法的实例
    VB.Net文件SHA1算法的实例
    Blowfish加密算法的实例
    Rijndael类的实例
    TripleDES加解密的实例
    VB.Net实现des加密算法的实例
    VB.Net实现RSA加密的实例

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]