Use WSE to encrypt SOAP packets (4)

Source: Internet
Author: User
Tags soap
Encrypt messages sent externally
Here I will briefly describe how to create a Web service that can return an encrypted XML document. In the first step, use the using indicator to add the necessary namespace, as shown below:
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;
The GetXmlDocument method uses the. NET framework to implement the ternary DES algorithm. It uses a 128-bit key and a 64-bit initialization vector (IV) to generate a symmetric key. This key will also have a name and be added to the SoapContext element of the response message. Then it will be used by SecurityOutputFilter to encrypt simple XML documents. This method will finally be returned to the client. For more information about encryption technology of the. NET framework, see Cryptography Overview in the. NET Framework Developer Guide.
// Return the data encrypted by the ternary DES symmetric algorithm.
[WebMethod (Description = "returns a sensitive XML document confidential by the symmetric encryption algorithm", EnableSession = false)]

Public XmlDocument GetXmlDocument ()
{
// Create a simple XML document to return
XmlDocument myDoc = new XmlDocument ();
MyDoc. InnerXml =
"<EncryptedResponse> sensitive data. </EncryptedResponse> ";

// Obtain the SoapContext of the response message sent externally
SoapContext myContext = HttpSoapContext. ResponseContext;

// Create a symmetric key for encryption. Because the key is symmetric, the same data must exist on the client.

// Define the shared 16-byte array to represent the 128-bit key
Byte [] keyBytes = {48,218, 89, 25,222,209,227, 51, 50,168,146,
188,250,166, 5,206 };

// Define the shared 8-byte (64-bit) array, that is, the initialization vector (IV)
Byte [] ivBytes = {16,143,111, 77,233,137, 12, 72 };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.