引用
<SOAP:Envelope SOAP:xmlsn="http://www.w3.org/2002/12/SOAP-envelope">
<SOAP:Header>
...
</SOAP:Header>
<SOAP:Body>
...
<x:Order Type="Purchase" x:xmlns="http://example.com/order">
<x:Payment Type="CreditCard">
<x:CreditCard Type="Visa">
<x:CardNumber>123456789123456</CardNumber>
<x:ExperationDate>1108</ExperationDate>
</x:CreditCard>
</x:Payment>
...
</x:Order>
...
</SOAP:Body>
</SOAP:Envelope>
引用
<SOAP:Envelope SOAP:xmlsn="http://www.w3.org/2002/12/SOAP-envelope"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#"
xmlns:xsig="http://www.w3.org/2000/09/xmldsig#"
xmlns:WSse="http://schemas.xmlSOAP.org/WS/2002/04/secext">
<SOAP:Header>
<WSse:Security>
<xenc:ReferenceList>
<xenc:DataReference URI="#OrderID"/>
</xenc:ReferenceList>
</WSse:Security> ...
</SOAP:Header>
<SOAP:Body>
...
<x:Order Type="Purchase" x:xmlns="http://example.com/order">
<xenc:EncryptedData Id="OrderId">
<xenc:EncryptionMethod
Algorithm= "http://www.w3.org/2001/04/xmlenc#tripledes-cbc"
<xsig:KeyInfo>
<xsig:KeyName>My Symmetric Key</xsig:KeyName>
</xsig:KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>...</CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
...
</x:Order>
...
</SOAP:Body>
</SOAP:Envelope>
引用
<configuration>
<system.Web>
...
<WebServices>
<SOAPExtensionTypes>
<add type=
"Microsoft.Web.Services.WebServicesExtension,
Microsoft.Web.Services,
Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
priority="1" group="0" />
</SOAPExtensionTypes>
</WebServices>
</system.Web>
</configuration>
引用
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;
引用
//返回由三元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);
//给他取个名字?
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;
}
引用
<?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>
引用
public DecryptionKey GetDecryptionKey(string encAlgorithmUri,
KeyInfo keyInfo)
{
//重新创造同样的用于表示128位密钥的16个字节
byte[] keyBytes = {48, 218, 89, 25, 222, 209, 227, 51, 50, 168, 146,
188, 250, 166, 5, 206};
//重新创造表示初始化向量的8个字节(64位)
byte[] ivBytes = {16, 143, 111, 77, 233, 137, 12, 72};
SymmetricAlgorithm mySymAlg = new TripleDESCryptoServiceProvider();
mySymAlg.Key = keyBytes;
mySymAlg.IV = ivBytes;
//重新创建对称加密密钥
DecryptionKey myKey = new SymmetricDecryptionKey(mySymAlg);
return myKey;
}
引用
<configuration>
...
<microsoft.web.services>
<security>
<decryptionKeyProvider
type="MyClient Assembly.DecryptionKeyProvider,
MyClientAssembly" />
</security>
引用
//创建一个用于返回的简单XML文档
XmlDocument myDoc = new XmlDocument();
myDoc.InnerXml =
"<EncryptedResponse>This is sensitive data.</EncryptedResponse>";
"<EncryptedResponse>这里是敏感数据.</EncryptedResponse>";
//得到响应报文的SoapContext
SoapContext myContext = HttpSoapContext.ResponseContext;
//打开并读取本地机器帐号的个人证书储存室
X509CertificateStore myStore =
X509CertificateStore.LocalMachineStore(
X509CertificateStore.MyStore);
myStore.OpenRead();
//查找所有名为”我的证书”的证书,然后将所有匹配的证书添加到证书集合中
X509CertificateCollection myCerts =
myStore.FindCertificateBySubjectString("My Certificate");
X509Certificate myCert = null;
//查找在集合中中的第一个证书
if (myCerts.Count > 0)
{
myCert = myCerts[0];
}
//确定我们有一个可以用于加密的证书
if (myCert == null || !myCert.SupportsDataEncryption)
{
throw new ApplicationException("Service is not able to
encrypt the response");
return null;
}
else
{
//使用有效的证书来创建一个安全Token
X509SecurityToken myToken = new X509SecurityToken(myCert);
//WSE将使用这个标记来加密报文正文的
//WSE产生一个KeyInfo元素,用来请求客户端上曾用于给报文解密的证书
EncryptedData myEncData = new EncryptedData(myToken);
//将已加密数据元素添加到响应报文的SoapContext上
myContext.Security.Elements.Add(myEncData);
return myDoc;
}
引用
<?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-11T01:34:01Z</wsu:Created>
<wsu:Expires>2003-02-11T01:39:01Z</wsu:Expires>
</wsu:Timestamp>
<wsse:Security soap:mustUnderstand="1"
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<xenc:EncryptedKey
Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<wsse:SecurityTokenReference>
<wsse:KeyIdentifier ValueType="wsse:X509v3">
YmlKVwXYD8vuGuYliuIYdEAQQPw=
</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>
</KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>UJ64Addf3Fd59XsaQ=…</xenc:CipherValue>
</xenc:CipherData>
<xenc:ReferenceList>
<xenc:DataReference URI=
"#EncryptedContent-608eef8b-4104-4469-95b6-7cb4703cfa03" />
</xenc:ReferenceList>
</xenc:EncryptedKey>
</wsse:Security>
</soap:Header>
<soap:Body xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
wsu:Id="Id-70179c5b-4975-4932-9ecd-a58feb34b0d3">
<xenc:EncryptedData
Id="EncryptedContent-608eef8b-4104-4469-95b6-7cb4703cfa03"
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" />
<xenc:CipherData>
<xenc:CipherValue>
4o1b4befwBJu6tzuaygfrAaX0UGtaYKcw2klIbuZPjLi...z8i2ypHN4+w==
</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</soap:Body>
</soap:Envelope>
引用
<x509
storeLocation="CurrentUser"
verifyTrust="true"
allowTestRoot="false" />
引用
<Response>
<NotEncrypted>
回应报文的这里没有必要被加密
</NotEncrypted>
<EncryptedResponse>
<EncryptedSubResponse>
这里是敏感数据.
</EncryptedSubResponse>
</EncryptedResponse>
</Response>
引用
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility
引用
using System.Xml;
using System.Xml.Serialization;
引用
string [] myId = {"Id:" + Guid.NewGuid(),"Id:" + Guid.NewGuid()};
//创建一个用于返回XML的XML文档
XmlDocument myDoc = new XmlDocument();
myDoc.LoadXml("<Response>" +
"<NotEncrypted>回应报文的这里没有必要加密" +
"</NotEncrypted>" +
"<EncryptedResponse>" +
"<EncryptedSubResponse>" +
"这里是敏感数据. " +
"</EncryptedSubResponse>" +
"</EncryptedResponse>" +
"</Response>");
//得到EncryptedSubResponse节点
XmlNode = myDoc.FirstChild.LastChild.FirstChild;
//向上遍历元素,添加两个Id属性
//向上保证内部的多数元素可以优先被加密
//否则我们会得到一个异常
for (int i=0;i<myId.Length;i++)
{
//创建新的Id属性
string wsu = "http://schemas.xmlsoap.org/ws/2002/07/utility";
XmlNode myAttr = myDoc.CreateNode(XmlNodeType.Attribute, "wsu",
"Id", wsu);
myAttr.Value = myId[ i ];
//将属性添加到文档
root.Attributes.SetNamedItem(myAttr);
root = root.ParentNode; // 移动到父节点
}
引用
//循环遍历Id值,将其添加到新的EncryptedData元素上
for (int i=0;i<myId.Length;i++)
{
//创建一个新的头,”#”是的前缀,用来保证相关的URI能够引用到头
EncryptedData myEncHeader = new EncryptedData(myToken, "#"+myId[ i ]);
//添加一个新的头到集合中
myContext.Security.Elements.Add(myEncHeader);
}
//返回加密数据
return myDoc;
引用
<?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-11T20:21:52Z</wsu:Created>
<wsu:Expires>2003-02-11T20:26:52Z</wsu:Expires>
</wsu:Timestamp>
<wsse:Security soap:mustUnderstand="1"
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<xenc:EncryptedKey
Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<wsse:SecurityTokenReference>
<wsse:KeyIdentifier ValueType="wsse:X509v3">
YmlKVwXYD8vuGuYliuIOXOY7ZYN9PwHbfAhCiYOV0aYdEAQQPw=
</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>
</KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>
UyKGBEXdY8lYSzqgdgxOXOY7ZYN9PwHbfAhCiYOV0...bwRnWk=
</xenc:CipherValue>
</xenc:CipherData>
<xenc:ReferenceList>
<xenc:DataReference URI=
"#EncryptedContent-cf014249-0e2a-4f8b-9002-13a7de916be0" />
</xenc:ReferenceList>
</xenc:EncryptedKey>
<xenc:EncryptedKey
Type="http://www.w3.org/2001/04/xmlenc#EncryptedKey"
xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod
Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<wsse:SecurityTokenReference>
<wsse:KeyIdentifier ValueType="wsse:X509v3">
YmlKVwXYD8vuGuYliuIYdEAQQPw=
</wsse:KeyIdentifier>
</wsse:SecurityTokenReference>
</KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>
In8Kf1cIdiJJJXCLZ+... wMqBEevXmzk=
</xenc:CipherValue>
</xenc:CipherData>
<xenc:ReferenceList>
<xenc:DataReference URI=
"#EncryptedContent-0744279a-02bf-4ad1-998e-622208eded0e" />
</xenc:ReferenceList>
</xenc:EncryptedKey>
</wsse:Security>
</soap:Header>
<soap:Body>
<GetXmlDocumentResponse xmlns="http://example.com/dime/">
<GetXmlDocumentResult>
<Response>
<NotEncrypted>
This part of the response does not need encryption
</NotEncrypted>
<EncryptedResponse
wsu:Id="Id:e5e8d792-abe7-4476-91d0-856fbdf4a958"
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility">
<xenc:EncryptedData
Id=
"EncryptedContent-cf014249-0e2a-4f8b-9002-13a7de916be0"
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" />
<xenc:CipherData>
<xenc:CipherValue>
2MNHCkGVH/5jb0pF4pCh3u2VaUKsWSA...AfEvJZT=
</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
</EncryptedResponse>
</Response>
</GetXmlDocumentResult>
</GetXmlDocumentResponse>
</soap:Body>
</soap:Envelope>
本文地址:http://com.8s8s.com/it/it43491.htm