云医院(HIS)FHIR数据如何加解密

   公钥加密,私钥解密

public static void EncryptXml(ref XmlDocument xmlDoc, string targetElementName, string ThumbPrint)
        {
            try
            {
                //XML有效性验证
                //XmlElement targetElement = Doc.GetElementsByTagName(targetElementName)[0] as XmlElement;
               XmlNodeList  targetElements = xmlDoc.GetElementsByTagName(targetElementName);
                
                if (targetElements == null || targetElements.Count==0)
                {
                    throw new CryptographicException("待加密节点[" + targetElementName+"]不存在");
                }
                if (targetElements.Count >1)
                {
                    throw new CryptographicException("待加密节点[" + targetElementName + "]数量超过一个了!");
                }
                XmlElement targetElement = targetElements[0] as XmlElement ;
                //获取加密证书
                X509Certificate2 Certificate = Wdpc.Security.Certificate.Store.GetX509Certificate2FromStore(ThumbPrint);
                EncryptedXml eXml = new EncryptedXml();
                //加密
                EncryptedData encryptElement = eXml.Encrypt(targetElement, Certificate);
                //替换加密节点
                EncryptedXml.ReplaceElement(targetElement, encryptElement, false);
            }
            catch (Exception ex)
            {
                throw new Exception("Wdpc.Security.Certificate.EncryptionAndDecryption.EncryptXml" + ex.Message.ToString());
            }
         
        }

public static void DecryptXml(ref XmlDocument xmlDoc, string ThumbPrint)
        {
          
            try
            {
                //获取加密证书
                X509Certificate2 Certificate = Wdpc.Security.Certificate.Store.GetX509Certificate2FromStore(ThumbPrint);
                EncryptedXml exml = new EncryptedXml(xmlDoc);
                //设置密钥-名称映射,可以随意设置一个名称,我们使用证书私钥解密
                exml.AddKeyNameMapping("rsaKey", Certificate.PrivateKey);
                //解密
                //此时会寻找所有<EncryptedData>,查看<KeyInfo>节点,解密<CipherData>节点。用解密<CipherData>的结果来替换<EncryptedData>元素。
                exml.DecryptDocument();
            }
            catch (Exception ex)
            {
                throw new Exception("Wdpc.Security.Certificate.EncryptionAndDecryption.DecryptXml" + ex.Message.ToString());
            }
        }

例子:

加密前数据

<?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"/>
</flatFormAssembly>

加密后: 

<?xml version="1.0" encoding="utf-8"?>
<!--平台程序集-->
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
  <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
  <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
    <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <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>
      <CipherData>
        <CipherValue>iLcUBbQFyA4o/Yv3tvO+UUmjLmFKRGlymliXk46QrTO3N0TdH7qy6p99npOjl4BXke1869aLFk0Y0Q7FL00CVETdC1W9fPzgQVUDnYBXmGgUtdpiONxRow2jBnnP9QVDBBnUE1iOYhoj9SCd9h05j7Epds/FN5d6SGXXs6o15x0=</CipherValue>
      </CipherData>
    </EncryptedKey>
  </KeyInfo>
  <CipherData>
    <CipherValue>Y7mhFq115YvKi+sKDaBn3jmmBmG17sjY8ra76c0h8XTWYVyWHWfp3yv69p587vWfWGO5QcohUd5vPNyAGhgnQx8Qva7VD2lIK1ebiRc2Fy5Y+73T9kapO1ohqvlENzFApyVkoijIPMa9pIKzzO4F6hW4VMfCAv8WSM2KxnTyplUXGcnHdpFbfuK9ZmCzRMjBkmpswhjz0MlNPOCi4nGOx6Ba4/L2zzEtMcKNQAthuf9zmsJS262xElkXQpeyfmc1kmEiLruQdUuFKXFXp2WujjfjvmrS7F9z/439EnLJU6edtRZ6eDWzdYQkpo3or3w9r4EuzX6Chc/HJtKhVrtvX7jZuccLJR1n5dWIU2pvwFDrdl8ZKhJWI2K/O4loZDTQmSuJixxXL/BKlKsUxGmiHRr+bjt/KOqVo58K55lwfQwx5f/z/VE8Ms7UVMxEjW7asPhEPYvFpCyUHuIdc4eJxwCfM/ZZPcoaX8JVTlPtyFOeN6MnfeMGQvsceClO+FedeZLsrP3g6UcoWjuOoxwbZaF4lvwHKxmHWQamy5wKIZN/Bn1gjqOa67+CRmvtfLTAlj/42S8CSjTzpHXaaRBGPUFme2wbOis/CfVjgghzc7nzeO2sc8aOsjUZASN9+xoV7e/du5lFV1DKLlQk9GfxEF/p8EKrwwvuRqufzccAdKUvBpzT26z7fLZNrOjttEKeU9dq7CSkSqp0wc1OBuPASSnTirkF0fsfoEnMAkRI8IXhoACl2Cw95abjixzTxycXfP3RZwAHsyytgbdVNpBUIIqy9VfW4GOZIczWG2kxU8R87/kP7cQlfeHX4Bxsjg9A0QJcFC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nd4N6pzvTneVMR6dLk+czFKZdF+hI2egf6deVCfrmmHSuAhexAOAXygh66H1F5uF23xlTH4r4PudUgRTyjbiw8ws+wexcGQXRQQQ3Is6fu4YVXQQstbQQakCxkjCOtztmMym7gL2MWqmsIIfR3cELLkokXLfXtOFKIK+8q9Wdgg6qTYqsMzrlFfQh2qAcK9dgr6tNeibrO7be5vBZBt8wcVG3PUzqVuxPaTbqa0sSAe8fpS6eWunlJyQPDJd5O4hz5JAswcOqtpSmr2ObXJgf+PRuhIjHaciTuL8cLdgsJHGbSE3PUQYXDW678gLg6YtEnYdSPZr0BfN0dB7wBrfdiA38XzD9yi9IFBx6sD+XWbfl3OETOD3B0HXyW/KNcK9PEnq13ZH/q5tOlWPA+NnhqJxMqOSMRomJo9noOanKHLmE+jmD2/gbnkzdbu/6Hye/wyF8Tp7vKtpy/psNNJmQgE696sg0MJ0qycUZujRetj32ocDTlA=</CipherValue>
  </CipherData>
</EncryptedData>

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