人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

運用WSE 加密SOAP報文(4)

運用WSE 加密SOAP報文(4)

更新時間:2022-09-21 文章作者:未知 信息來源:網絡 閱讀次數:

加密對外發送的報文

這里我簡單描述下如何創建一個可以返回個被加密的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元素的引用,這個元素包含了密鑰的名字,使用的加密算法和一個數據的密文形式。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 元朗区| 揭西县| 四平市| 宜良县| 孝义市| 闸北区| 定远县| 平利县| 楚雄市| 闽清县| 平定县| 瓮安县| 南漳县| 讷河市| 芒康县| 孟州市| 井研县| 依安县| 原阳县| 共和县| 苍南县| 大新县| 民勤县| 翁牛特旗| 巢湖市| 丹江口市| 黔南| 汪清县| 正蓝旗| 金秀| 晴隆县| 汉源县| 孝感市| 房山区| 新竹市| 嘉禾县| 合肥市| 桦川县| 三台县| 东宁县| 呈贡县|