美文网首页
第三十六章 结合加密和签名 - 使用对称密钥签名并加密

第三十六章 结合加密和签名 - 使用对称密钥签名并加密

作者: Cache技术分享 | 来源:发表于2024-09-30 09:52 被阅读0次

第三十六章 结合加密和签名 - 使用对称密钥签名并加密

使用对称密钥签名并加密

签名然后加密(使用对称密钥时):

  1. 按照使用<DerivedKeyToken> 进行加密中的步骤进行操作。
  2. 按照使用<DerivedKeyToken> 进行签名中的步骤进行操作。

使用<DerivedKeyToken>元素

以下示例使用对称密钥进行签名和加密。它使用消息接收者的公钥创建一个 <EncryptedKey>元素,然后使用该元素生成两个 <DerivedKeyToken> 元素,一个用于签名,一个用于加密:

 // create UsernameToken
  set userToken=##class(%SOAP.Security.UsernameToken).Create("_SYSTEM","SYS")

  //get credentials of message recipient
  set x509alias = "servernopassword"
  set cred = ##class(%SYS.X509Credentials).GetByAlias(x509alias) 

  //get EncryptedKey element
  set enc=##class(%XML.Security.EncryptedKey).CreateX509(cred,$$$SOAPWSEncryptNone)
  do client.SecurityOut.AddSecurityElement(enc)

  // get derived keys
  set dkenc=##class(%SOAP.WSSC.DerivedKeyToken).Create(enc,$$$SOAPWSReferenceEncryptedKey)
  do client.SecurityOut.AddSecurityElement(dkenc)
  set dksig=##class(%SOAP.WSSC.DerivedKeyToken).Create(enc,$$$SOAPWSReferenceEncryptedKey)
  do client.SecurityOut.AddSecurityElement(dksig)

  // create and add signature
  set sig=##class(%XML.Security.Signature).Create(dksig,,$$$SOAPWSReferenceDerivedKey)
  do client.SecurityOut.AddSecurityElement(sig) 
 
  // ReferenceList to encrypt Body and Username. Add after signing
  set reflist=##class(%XML.Security.ReferenceList).%New()
  set refopt=$$$SOAPWSReferenceDerivedKey
  set encryptedData=##class(%XML.Security.EncryptedData).Create(dkenc,userToken,refopt)
  set dataref=##class(%XML.Security.DataReference).Create(encryptedData)
  do reflist.AddReference(dataref)
  set encryptedData=##class(%XML.Security.EncryptedData).Create(dkenc,"",refopt)
  set dataref=##class(%XML.Security.DataReference).Create(encryptedData)
  do reflist.AddReference(dataref)
  do client.SecurityOut.AddSecurityElement(reflist)
  
  // Add UsernameToken; force after ReferenceList so that it can decrypt properly
  do client.SecurityOut.AddSecurityElement(userToken,reflist)

该客户端发送如下消息:

<SOAP-ENV:Envelope [parts omitted]'>  
   <SOAP-ENV:Header>
      <Security xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
         <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#" 
                       Id="Id-A0CBB4B7-18A8-40C1-A2CD-C0C383BF9531">
            <EncryptionMethod Algorithm="[parts omitted]#rsa-oaep-mgf1p">
               <DigestMethod xmlns="http://www.w3.org/2000/09/xmldsig#" 
                             Algorithm="[parts omitted]#sha1"></DigestMethod>
            </EncryptionMethod>
            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
               <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
                  <KeyIdentifier EncodingType="[parts omitted]#Base64Binary" 
                                 ValueType="[parts omitted]#ThumbprintSHA1">
              5afOHv1w7WSXwDyz6F3WdM1r6cM=</KeyIdentifier>
               </SecurityTokenReference>
            </KeyInfo>
            <CipherData>
               <CipherValue>fR4hoJy4[parts omitted]Gmq1xg==</CipherValue>
            </CipherData>
         </EncryptedKey>
         <DerivedKeyToken xmlns="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" 
                          xmlns:wsc="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" 
                          wsu:Id="Enc-43F73EB2-77EC-4D72-9DAD-17B1781BC49C">
            <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
               <Reference URI="#Id-A0CBB4B7-18A8-40C1-A2CD-C0C383BF9531"></Reference>
            </SecurityTokenReference>
            <Nonce>Q1wDt0PSSLmARcy+Pg49Sg==</Nonce>
         </DerivedKeyToken>
         <DerivedKeyToken xmlns="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" 
                          xmlns:wsc="http://docs.oasis-open.org/ws-sx/ws-secureconversation/200512" 
                          wsu:Id="Enc-ADE64310-E695-4630-9DA6-A818EF5CEE9D">
            <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
               <Reference URI="#Id-A0CBB4B7-18A8-40C1-A2CD-C0C383BF9531"></Reference>
            </SecurityTokenReference>
            <Offset>0</Offset>
            <Length>24</Length>
            <Nonce>PvaakhgdxoBVLR6I1j6KGA==</Nonce>
         </DerivedKeyToken>
         <ReferenceList xmlns="http://www.w3.org/2001/04/xmlenc#">
            <DataReference URI="#Enc-F8013636-5339-4C25-87CD-C241330865F5"></DataReference>
            <DataReference URI="#Enc-CDF877AC-8347-4903-97D9-E8238C473DC4"></DataReference>
         </ReferenceList>
         <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#" 
                        Id="Enc-F8013636-5339-4C25-87CD-C241330865F5" 
                        Type="http://www.w3.org/2001/04/xmlenc#Element">
            <EncryptionMethod Algorithm="[parts omitted]#aes256-cbc"></EncryptionMethod>
            <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
               <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
                  <Reference URI="#Enc-43F73EB2-77EC-4D72-9DAD-17B1781BC49C"></Reference>
               </SecurityTokenReference>
            </KeyInfo>
            <CipherData>
               <CipherValue>ebxkmD[parts omitted]ijtJg==</CipherValue>
            </CipherData>
         </EncryptedData>
         <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
               <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#">
               </CanonicalizationMethod>
               <SignatureMethod Algorithm="[parts omitted]#hmac-sha1"></SignatureMethod>
               <Reference URI="#Body-C0D7FF05-EE59-41F6-939D-7B2F2B883E5F">
                  <Transforms>
                     <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"></Transform>
                  </Transforms>
                  <DigestMethod Algorithm="[parts omitted]#sha1"></DigestMethod>
                  <DigestValue>vic7p2selz4Wvm1nAX67p0xF1VI=</DigestValue>
               </Reference>
            </SignedInfo>
            <SignatureValue>TxIBa4a8wX5oFN+eyjjsUuLdn7U=</SignatureValue>
            <KeyInfo>
               <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
                  <Reference URI="#Enc-ADE64310-E695-4630-9DA6-A818EF5CEE9D"></Reference>
               </SecurityTokenReference>
            </KeyInfo>
         </Signature>
      </Security>  
   </SOAP-ENV:Header>  
   <SOAP-ENV:Body wsu:Id="Body-C0D7FF05-EE59-41F6-939D-7B2F2B883E5F">
      <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#" 
                     Id="Enc-CDF877AC-8347-4903-97D9-E8238C473DC4" 
                     Type="http://www.w3.org/2001/04/xmlenc#Content">
         <EncryptionMethod Algorithm="[parts omitted]#aes256-cbc"></EncryptionMethod>
         <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SecurityTokenReference xmlns="[parts omitted]oasis-200401-wss-wssecurity-secext-1.0.xsd">
               <Reference URI="#Enc-43F73EB2-77EC-4D72-9DAD-17B1781BC49C"></Reference>
            </SecurityTokenReference>
         </KeyInfo>
         <CipherData>
            <CipherValue>vYtzDsv[parts omitted]GohGsL6</CipherValue>
         </CipherData>
      </EncryptedData>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用对称密钥加密并签名

加密然后签名(使用对称密钥时):

  1. 按照使用<DerivedKeyToken>进行签名中的步骤进行操作。
  2. 按照使用<DerivedKeyToken>进行加密中的步骤进行操作。

安全标头元素的顺序

一般情况下,应该按照执行处理的顺序将安全元素添加到安全标头中。消息接收者应该能够从头到尾处理该消息,而无需任何前向引用。

下表列出了使用非对称密钥时安全标头元素的最终顺序(这些场景使用非对称密钥绑定):

签名然后加密 签名然后加密
Other header elements
<EncryptedKey>
<Signature>
Other header elements
<EncryptedKey>
<Signature>
<ReferenceList>

下表列出了使用对称密钥时安全标头元素的结果顺序(这些场景使用对称密钥绑定):

签名然后加密 签名然后加密
Other header elements
<EncryptedKey>
<DerivedKeyToken>
<DerivedKeyToken>
<ReferenceList>
<Signature>
Other header elements
<EncryptedKey>
<DerivedKeyToken>
<DerivedKeyToken>
<Signature>
<ReferenceList>

相关文章

  • 认证授权 归纳(4)(密钥概念)

    1: 密钥分类 (对称密钥与非对称密钥) 2:密钥,私钥,公钥的区分 3:摘要 签名 签名验证 4:加密解密 ...

  • 密码学

    对称加密算法 非对称加密算法 非对称加密:加密密钥与解密密钥成对出现,一般为私钥和公钥。私钥用于签名或加密;公钥用...

  • Android签名

    Android签名用到的基础概念 对称加密和非对称加密 对称加密是加密解密密钥是同一个的加密方案,可以加密大数据量...

  • ssh与非对称加密

    1.非对称加密与数字签名 a.非对称加密 在非对称加密中有不同的密钥:私钥(private key)和公钥(pub...

  • CA概述(加密类型介绍以及证书申请过程)

    对称加密 非对称加密 公钥加密交换对称密钥 hash加密与数字签名 加密算法的特性比较 证书作用:公钥的安全传递 ...

  • 011各种加密算法比较

    算法选择:对称加密AES,非对称加密: ECC,消息摘要: MD5,数字签名:DSA 对称加密算法(加解密密钥相同...

  • 密码学基础(五):证书体系

    摘要算法 略 对称加密 略 非对称加密 略 数字签名 使用摘要算法对原文进行签名后使用rsa的私钥对摘要进行加密。...

  • HTTPS中的数字证书认证过程解析

    RSA非对称加密的2个用途:加密和签名 加密(防窃听) RSA非对称加密会用到一对密钥,分别称为公钥和私钥,公钥加...

  • 信息安全基础知识

    加密技术分为对称加密和非对称加密,区别在于加密和解密是否使用相同的密钥。对称加密使用相同的密钥,非对称加密的密钥则...

  • 深入浅出:区块链技术中的非对称加密算法--签名和加密

    签名是一种非对称加密算法的用法,即使用不同的加密密钥与解密密钥,而由已知加密密钥推导出解密密钥在计算上是不可行的。...

网友评论

      本文标题:第三十六章 结合加密和签名 - 使用对称密钥签名并加密

      本文链接:https://www.haomeiwen.com/subject/fwtjrjtx.html