2 // EncryptedData.cs - EncryptedData implementation for XML Encryption
3 // http://www.w3.org/2001/04/xmlenc#sec-EncryptedData
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Tim Coleman, 2004
14 namespace System.Security.Cryptography.Xml {
15 public sealed class EncryptedData : EncryptedType {
19 public EncryptedData ()
24 #endregion // Constructors
28 public override XmlElement GetXml ()
30 return GetXml (new XmlDocument ());
33 internal XmlElement GetXml (XmlDocument document)
35 if (CipherData == null)
36 throw new CryptographicException ("Cipher data is not specified.");
38 XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptedData, XmlEncryption.NamespaceURI);
40 if (EncryptionMethod != null)
41 xel.AppendChild (EncryptionMethod.GetXml (document));
43 xel.AppendChild (document.ImportNode (KeyInfo.GetXml (), true));
44 if (CipherData != null)
45 xel.AppendChild (CipherData.GetXml (document));
47 if (EncryptionProperties.Count > 0) {
48 XmlElement xep = document.CreateElement (XmlEncryption.ElementNames.EncryptionProperties, XmlEncryption.NamespaceURI);
49 foreach (EncryptionProperty p in EncryptionProperties)
50 xep.AppendChild (p.GetXml (document));
51 xel.AppendChild (xep);
55 xel.SetAttribute (XmlEncryption.AttributeNames.Id, Id);
57 xel.SetAttribute (XmlEncryption.AttributeNames.Type, Type);
59 xel.SetAttribute (XmlEncryption.AttributeNames.MimeType, MimeType);
61 xel.SetAttribute (XmlEncryption.AttributeNames.Encoding, Encoding);
65 public override void LoadXml (XmlElement value)
68 throw new ArgumentNullException ("value");
70 if ((value.LocalName != XmlEncryption.ElementNames.EncryptedData) || (value.NamespaceURI != XmlEncryption.NamespaceURI))
71 throw new CryptographicException ("Malformed EncryptedData element.");
73 EncryptionMethod = null;
74 KeyInfo keyInfo = null;
75 CipherData cipherData = null;
76 EncryptionMethod = null;
77 EncryptionProperties = new EncryptionProperties ();
83 foreach (XmlNode n in value.ChildNodes) {
84 if (n is XmlWhitespace)
87 switch (n.LocalName) {
88 case XmlEncryption.ElementNames.EncryptionMethod:
89 EncryptionMethod = new EncryptionMethod ();
90 EncryptionMethod.LoadXml ((XmlElement) n);
92 case XmlSignature.ElementNames.KeyInfo:
93 KeyInfo = new KeyInfo ();
94 KeyInfo.LoadXml ((XmlElement) n);
96 case XmlEncryption.ElementNames.CipherData:
97 CipherData = new CipherData ();
98 CipherData.LoadXml ((XmlElement) n);
100 case XmlEncryption.ElementNames.EncryptionProperties:
101 foreach (XmlElement element in ((XmlElement) n).GetElementsByTagName (XmlEncryption.ElementNames.EncryptionProperty, XmlEncryption.NamespaceURI))
102 EncryptionProperties.Add (new EncryptionProperty (element));
107 if (value.HasAttribute (XmlEncryption.AttributeNames.Id))
108 Id = value.Attributes [XmlEncryption.AttributeNames.Id].Value;
109 if (value.HasAttribute (XmlEncryption.AttributeNames.Type))
110 Type = value.Attributes [XmlEncryption.AttributeNames.Type].Value;
111 if (value.HasAttribute (XmlEncryption.AttributeNames.MimeType))
112 MimeType = value.Attributes [XmlEncryption.AttributeNames.MimeType].Value;
113 if (value.HasAttribute (XmlEncryption.AttributeNames.Encoding))
114 Encoding = value.Attributes [XmlEncryption.AttributeNames.Encoding].Value;
118 #endregion // Methods