使用WSE 加密SOAP報文(4)
發表時間:2024-02-18 來源:明輝站整理相關軟件相關文章人氣:
[摘要]加密對外發送的報文這里我簡單描述下如何創建一個可以返回個被加密的XML文檔的Web服務。第一步先用using指示符來添加必要的命名空間,如下:using System.Web.Services;using Microsoft.Web.Services;using Microsoft.Web.Ser...
加密對外發送的報文
這里我簡單描述下如何創建一個可以返回個被加密的XML文檔的Web服務。第一步先用using指示符來添加必要的命名空間,如下:
using System.Web.Services;
using Microsoft.Web.Services;
using Microsoft.Web.Services.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;
GetXmlDocument方法使用了的.NET框架實現的三元DES算法,采用128位密鑰和64位初始化向量(IV),能夠生成對稱密鑰。這個密鑰還將擁有一個名字,并被添加到應答報文的SoapContext元素上,之后被SecurityOutputFilter使用于加密簡單的XML文檔,這個方法最后將返回給客戶端。更多關于.NET框架的加密技術,請看.NET框架開發者指南上的Cryptography Overview一文。
//返回由三元DES對稱算法加密后的數據
[WebMethod (Description="返回一個由對稱加密算法機密后的敏感XML文檔", EnableSession=false)]
public XmlDocument GetXmlDocument()
{
//創建一個用于返回的簡單的XML文檔
XmlDocument myDoc = new XmlDocument();
myDoc.InnerXml =
"<EncryptedResponse>這里是敏感數據.</EncryptedResponse>";
//得到對外發送的回應報文的SoapContext
SoapContext myContext = HttpSoapContext.ResponseContext;
//創建一個用于加密的對稱密鑰,由于密鑰是對稱的,這些相同的數據必須存在有需求的客戶端上。
//定義共享的16字節數組,用來表示128位密鑰
byte[] keyBytes = {48, 218, 89, 25, 222, 209, 227, 51, 50, 168, 146,
188, 250, 166, 5, 206};
//定義共享的8字節(64位)數組,也就是初始化向量(IV)
byte[] ivBytes = {16, 143, 111, 77, 233, 137, 12, 72};
//創建三元DES算法的新實例
SymmetricAlgorithm mySymAlg = new TripleDESCryptoServiceProvider();
//設置好密鑰和IV
mySymAlg.Key = keyBytes;
mySymAlg.IV = ivBytes;
//創建一個新的WSE對稱加密密鑰
EncryptionKey myKey = new SymmetricEncryptionKey(mySymAlg);
//給他取個名字J
KeyInfoName myKeyName = new KeyInfoName();
myKeyName.Value = "http://example.com/symmetrictestkey";
myKey.KeyInfo.AddClause(myKeyName);
//使用對稱密鑰來創建一個新的EncryptedData元素
EncryptedData myEncData = new EncryptedData(myKey);
//將EncryptedData元素添加到SOAP回應上,告訴過濾器用指定的密鑰來加密信息正文
myContext.Security.Elements.Add(myEncData);
return myDoc;
}
基于前面的方法,WSE管道產生了下面有相應的安全頭信息,密文和密鑰信息的回應報文:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<wsu:Timestamp
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<wsu:Created>2003-02-11T02:07:23Z</wsu:Created>
<wsu:Expires>2003-02-11T02:12:23Z</wsu:Expires>
</wsu:Timestamp>
<wsse:Security soap:mustUnderstand="1"
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<xenc:ReferenceList
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:DataReference URI=
"#EncryptedContent-f50076e3-5aea-435e-8493-5d7860191411" />
</xenc:ReferenceList>
</wsse:Security>
</soap:Header>
<soap:Body xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
wsu:Id="Id-d2f22e02-a052-4dcb-8fbc-8591a45b8a9f">
<xenc:EncryptedData
Id="EncryptedContent-f50076e3-5aea-435e-8493-5d7860191411"
Type="http://www.w3.org/2001/04/xmlenc#Content"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>http://example.com/symmetrictestkey</KeyName>
</KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>0T5ThoGg14JmElph...qDJS=</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</soap:Body>
</soap:Envelope>
注意,在報文正文中ReferenceList元素包含了一個到EncryptedData元素的引用,這個元素包含了密鑰的名字,使用的加密算法和一個數據的密文形式。