• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

设置和验证XML文档的数字签名_xml

xml 搞代码 7年前 (2018-06-15) 111次浏览 已收录 0个评论

Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.xml
Imports System.Xml

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ‘ Create a new CspParameters object to specify
        ‘ a key container.

http://www.gaodaima.com/33895.html设置和验证XML文档的数字签名_xml

        Dim cspParams As New CspParameters()
        cspParams.KeyContainerName = “XML_DSIG_RSA_KEY”
        ‘ Create a new RSA signing key and save it in the container.
        Dim rsaKey As New RSACryptoServiceProvider(cspParams)
        ‘ Create a new XML document.
        Dim xmlDoc As New XmlDocument()

        ‘ Load an XML file into the XmlDocument object.
        xmlDoc.PreserveWhitespace = True
        xmlDoc.LoadXml(“<text>可以使用 System.Security.Cryptography.Xml 命名空间中的类通过数字签名对XML   “` 文档或 XML 文档的部分进行签名</text>”)
        ‘ Sign the XML document.
        SignXml(xmlDoc, rsaKey)

        MsgBox(“XML file signed.”)

        ‘ Save the document.
        xmlDoc.Save(“c:/ok.xml”)
        MsgBox(VerifyXml(xmlDoc, rsaKey))
    End Sub

    ‘ Sign an XML file.
    ‘ This document cannot be verified unless the verifying
    ‘ code has the key with which it was signed.
    Sub SignXml(ByVal Doc As XmlDocument, ByVal Key As RSA)
        ‘ Check arguments.
        If Doc Is Nothing Then
            Throw New ArgumentException(“Doc”)
        End If
        If Key Is Nothing Then
            Throw New ArgumentException(“Key”)
        End If
        ‘ Create a SignedXml object.
        Dim signedXml As New SignedXml(Doc)
        ‘ Add the key to the SignedXml document.
        signedXml.SigningKey = Key
        ‘ Create a reference to be signed.
        Dim reference As New Reference()
        reference.Uri = “”
        ‘ Add an enveloped transformation to the reference.
        Dim env As New XmlDsigEnvelopedSignatureTransform()
        reference.AddTransform(env)
        ‘ Add the reference to the SignedXml object.
        signedXml.AddReference(reference)
        ‘ Compute the signature.
        signedXml.ComputeSignature()
        ‘ Get the XML representation of the signature and save
        ‘ it to an XmlElement object.
        Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
        ‘ Append the element to the XML document.
        Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, True))
    End Sub

    Function VerifyXml(ByVal Doc As XmlDocument, ByVal Key As RSA) As [Boolean]
        ‘ Check arguments.
        If Doc Is Nothing Then
            Throw New ArgumentException(“Doc”)
        End If
        If Key Is Nothing Then
            Throw New ArgumentException(“Key”)
        End If
        ‘ Create a new SignedXml object and pass it
        ‘ the XML document class.
        Dim signedXml As New SignedXml(Doc)
        ‘ Find the “Signature” node and create a new
        ‘ XmlNodeList object.
        Dim nodeList As XmlNodeList = Doc.GetElementsByTagName(“Signature”)
        ‘ Throw an exception if no signature was found.
        If nodeList.Count <= 0 Then
            MsgBox(“Verification failed: No Signature was found in the document.”)
        End If

        ‘ This example only supports one signature for
        ‘ the entire XML document.  Throw an exception
 

欢迎大家阅读《设置和验证XML文档的数字签名_xml》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:设置和验证XML文档的数字签名_xml

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址