云医院(HIS)Fhir数据如何完成签名认证 – 云医院

云医院(HIS)Fhir数据如何完成签名认证

私钥签名,公钥验证 

 public static void SignXmlDoc(ref XmlDocument xmlDoc, string ThumbPrint,bool AddKeyInfo)
        {
            SignedXml signedXml = new SignedXml(xmlDoc);

            //获取签名证书
            X509Certificate2 Certificate = Wdpc.Security.Certificate.Store.GetX509Certificate2FromStore( ThumbPrint);
            signedXml.SigningKey = Certificate.PrivateKey;

            //引用
            //指定了在哈希运算之前应当如何对将要签名的数据进行处理。
            //URI属性标识要签名的数据,而Transforms元素指定如何处理数据。
            Reference reference = new Reference();
            reference.Uri = ""; //空字符串,它指定对整个文档进行签名并且包含签名,需要特别注意的是文档中如果已经存在<Signature>节点,在签名前将先被移除。
            XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();  //使用包封式签名转换
            reference.AddTransform(env);
            signedXml.AddReference(reference);

            //向签名的接收方提供签名证书的信息,在验证签名的同时可以验证签名证书
            if (AddKeyInfo)
            {
                KeyInfoX509Data keyInfoX509 = new KeyInfoX509Data(Certificate, X509IncludeOption.EndCertOnly);
                signedXml.KeyInfo.AddClause(keyInfoX509);
            }
         

            //签名
            signedXml.ComputeSignature();

            //将签名加入XML中
            XmlElement xmlDigitalSignature = signedXml.GetXml();
            xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
        }

        public static Boolean VerifySignXmlDoc(XmlDocument xmlDoc, string ThumbPrint)
        {
            //XML有效性验证
            XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
            if (nodeList.Count <= 0)
            {
                throw new CryptographicException("缺失signature节点");
            }
            if (nodeList.Count >= 2)
            {
                throw new CryptographicException("signature节点多于一个");
            }

            SignedXml signedXml = new SignedXml(xmlDoc);

            signedXml.LoadXml((XmlElement)nodeList[0]);

            //获取证书
            X509Certificate2 Certificate = Wdpc.Security.Certificate.Store.GetX509Certificate2FromStore(ThumbPrint);

            //验证签名以及证书,verifySignatureOnly设置为flase则不验证证书
            return signedXml.CheckSignature(Certificate, true);
        }

 

 

数字签名后:

 

<?xml version="1.0" encoding="utf-8"?>
<!–平台程序集–>
<flatFormAssembly>
  <ver value="1000" />
  <!–计算机访问的名字–>
  <name value="publicComponent" />
  <!–人可读的标题–>
  <title value="公共组件" />
  <!–枚举–>
  <status value="active/retired" />
  <!–创建时间–>
  <createdate value="20010101T235959" />
  <!–发布者–>
  <publisher value="wdpc" />
  <!–阐述–>
  <description value="" />
  <!–目的–>
  <purpose value="" />
  <!–有效期–>
  <effectivePeriod>
    <start value="2001-1-1">
    </start>
    <end value="2001-1-3" />
  </effectivePeriod>
  <!–参照这个类型(联系方式)–>
  <contact />
  <!–参照这个类型(版权)–>
  <copyright />
  <fileGroupType>
    <system value="valuest/PublicCloudFileSystemGroup" />
    <code value="0001.0001" />
    <version value="1000" />
    <display value="云平台/服务程序集" />
  </fileGroupType>
  <fileName value="aaa.dll" />
  <operatingSystem>
    <!–操作系统类别(windows,linux,unix,macosx,andriod,os/2,ios)–>
    <type value="windows" />
    <!–操作系统位数(x86,64,any)–>
    <bit value="any" />
    <supportLowestVer value="6.1" />
    <supportHighestVer value="*" />
  </operatingSystem>
  <!–本组件需要的依赖的项目–>
  <dependOn>
    <reference value="flatFormAssembly/1323" />
  </dependOn>
  <!–需要注册–>
  <regsvr value="true" />
  <!–开发语言(dotnet,pb,java,delphi,c,c++)–>
  <developLanguage value="" />
  <dotnet>
    <!–版本–>
    <frameWorkVer value="4.0" />
    <!–参照这个类型(相关文档)–>
    <relatedArtifact />
    <!–隶属于服务平台域名称–>
    <partOfPlatFormDomain value="WdpcCommon" />
    <!–模拟windows用户身份信息–>
    <runAsIdentity>
      <userName value="administrator" />
      <passWord value="a" />
      <domain value="domain" />
      <logonType value="2" />
      <logonProvider value="0" />
    </runAsIdentity>
    <!–资源访问用户–>
    <runAsResourceRoll value="aaa" />
  </dotnet>
  <!–支持热插拔–>
  <hotPlug value="true" />
  <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
    <SignedInfo>
      <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
      <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
      <Reference URI="">
        <Transforms>
          <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
        </Transforms>
        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
        <DigestValue>iyY0TLjT9Yu07tt0QzioF/Re2KM=</DigestValue>
      </Reference>
    </SignedInfo>
    <SignatureValue>WI1WlqaXCoEMzr4heMerl/Rt5NqQxtctkcUSg5egho5lXJFK8ksP/SY7Anu88tPlwntiKcEmACe8vFVRpNnsms2ML9AmxQLW6anudzw1OVAxWcdxhZYf/5vGEeBGs355NS9x1Kwh1KV8iti9qviUNpGc6v/fEiTc8mG6XLJH0X8=</SignatureValue>
    <KeyInfo>
      <X509Data>
        <X509Certificate>MIICXTCCAcagAwIBAgIQTcPjTDTfXKetWnkFBeg02DANBgkqhkiG9w0BAQsFADBKMQswCQYDVQQGEwJDTjEVMBMGA1UECgwM5Lyf6L6+6bmP56iLMRUwEwYDVQQLDAzor4HkuabkuK3lv4MxDTALBgNVBAMMBFdEUEMwHhcNMTgwODA0MTAxMTE3WhcNMjAwMzI3MTAxMTE3WjA7MRUwEwYDVQQDDAzkuKrkurror4HkuaYxFTATBgNVBAoMDOetvuWQjeWKoOWvhjELMAkGA1UEBhMCQ04wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMxYO0hg/6Z0Qhl2TOSAuWy/nBFTl4Ts2Hl6qMZZKkfGrhhkl9mbX/wsaJ1F0fJb1ugZ+z/+RikoqFwvIZmg6VPicLIBoDSUt4LWcWXQA425HGQKKS/HuUwDrZ5KCgL9rkth/rsmiBz+xxGfep2zNqIWaAGaOCfuRj6iU8tQ7id1AgMBAAGjUzBRMB8GA1UdIwQYMBaAFOdTC8/aFf5NIt6Cg/aBE/pRI1RoMB0GA1UdDgQWBBQ9arlmw+3L7pYfBhuUuKTX1rEezzAPBgNVHSUECDAGBgRVHSUAMA0GCSqGSIb3DQEBCwUAA4GBAGyfeEHLGpXgHJJDif1dJIRgZyLAhIroDJzYP2BxtQ6OSDWGbdo1Nle5P7omOf9OAwNQfZCh8BwtkRowB7vx4jwvytnaDi0Fj5eOYxVjxS3lw+E+510p7W4hcufj/o9+k8CQzy0HeqtyBF0/zVTQWSB3jUxylTCO4F+vTInjTn2c</X509Certificate>
      </X509Data>
    </KeyInfo>
  </Signature>
</flatFormAssembly>

要发表评论,您必须先登录