CipherData.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // CipherData.cs - CipherData implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-CipherData
  4. //
  5. // Author:
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. #if NET_1_2
  10. using System.Security.Cryptography;
  11. using System.IO;
  12. using System.Xml;
  13. namespace System.Security.Cryptography.Xml {
  14. public sealed class CipherData {
  15. #region Fields
  16. byte[] cipherValue;
  17. CipherReference cipherReference;
  18. #endregion // Fields
  19. #region Constructors
  20. public CipherData ()
  21. {
  22. }
  23. public CipherData (byte[] cipherValue)
  24. {
  25. CipherValue = cipherValue;
  26. }
  27. public CipherData (CipherReference cipherReference)
  28. {
  29. CipherReference = cipherReference;
  30. }
  31. #endregion // Constructors
  32. #region Properties
  33. public CipherReference CipherReference {
  34. get { return cipherReference; }
  35. set {
  36. if (CipherValue != null)
  37. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  38. cipherReference = value;
  39. }
  40. }
  41. public byte[] CipherValue {
  42. get { return cipherValue; }
  43. set {
  44. if (CipherReference != null)
  45. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  46. cipherValue = value;
  47. }
  48. }
  49. #endregion // Properties
  50. #region Methods
  51. public XmlElement GetXml ()
  52. {
  53. return GetXml (new XmlDocument ());
  54. }
  55. internal XmlElement GetXml (XmlDocument document)
  56. {
  57. if (CipherReference == null && CipherValue == null)
  58. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  59. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.CipherData, XmlEncryption.NamespaceURI);
  60. if (CipherReference != null)
  61. xel.AppendChild (document.ImportNode (cipherReference.GetXml (), true));
  62. if (CipherValue != null) {
  63. XmlElement xcv = document.CreateElement (XmlEncryption.ElementNames.CipherValue, XmlEncryption.NamespaceURI);
  64. StreamReader reader = new StreamReader (new CryptoStream (new MemoryStream (cipherValue), new ToBase64Transform (), CryptoStreamMode.Read));
  65. xcv.InnerText = reader.ReadToEnd ();
  66. reader.Close ();
  67. xel.AppendChild (xcv);
  68. }
  69. return xel;
  70. }
  71. public void LoadXml (XmlElement value)
  72. {
  73. CipherReference = null;
  74. CipherValue = null;
  75. if (value == null)
  76. throw new ArgumentNullException ("value");
  77. if ((value.LocalName != XmlEncryption.ElementNames.CipherData) || (value.NamespaceURI != XmlEncryption.NamespaceURI))
  78. throw new CryptographicException ("Malformed Cipher Data element.");
  79. else {
  80. foreach (XmlNode n in value.ChildNodes) {
  81. if (n is XmlWhitespace)
  82. continue;
  83. switch (n.LocalName) {
  84. case XmlEncryption.ElementNames.CipherReference:
  85. cipherReference = new CipherReference ();
  86. cipherReference.LoadXml ((XmlElement) n);
  87. break;
  88. case XmlEncryption.ElementNames.CipherValue:
  89. CipherValue = Convert.FromBase64String (n.InnerText);
  90. break;
  91. }
  92. }
  93. if (CipherReference == null && CipherValue == null)
  94. throw new CryptographicException ("A Cipher Data element should have either a CipherValue or a CipherReference element.");
  95. }
  96. }
  97. #endregion // Methods
  98. }
  99. }
  100. #endif