EncryptionMethod.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // EncryptionMethod.cs - EncryptionMethod implementation for XML Encryption
  3. // http://www.w3.org/2001/04/xmlenc#sec-EncryptionMethod
  4. //
  5. // Author:
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2004
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System.Xml;
  31. namespace System.Security.Cryptography.Xml {
  32. public class EncryptionMethod {
  33. #region Fields
  34. string algorithm;
  35. int keySize;
  36. #endregion // Fields
  37. #region Constructors
  38. public EncryptionMethod ()
  39. {
  40. KeyAlgorithm = null;
  41. }
  42. public EncryptionMethod (string strAlgorithm)
  43. {
  44. KeyAlgorithm = strAlgorithm;
  45. }
  46. #endregion // Constructors
  47. #region Properties
  48. public string KeyAlgorithm {
  49. get { return algorithm; }
  50. set { algorithm = value; }
  51. }
  52. public int KeySize {
  53. get { return keySize; }
  54. set {
  55. if (value <= 0)
  56. throw new ArgumentOutOfRangeException ("The key size should be a non negative integer.");
  57. keySize = value;
  58. }
  59. }
  60. #endregion // Properties
  61. #region Methods
  62. public XmlElement GetXml ()
  63. {
  64. return GetXml (new XmlDocument ());
  65. }
  66. internal XmlElement GetXml (XmlDocument document)
  67. {
  68. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionMethod, EncryptedXml.XmlEncNamespaceUrl);
  69. if (KeySize != 0) {
  70. XmlElement xks = document.CreateElement (XmlEncryption.ElementNames.KeySize, EncryptedXml.XmlEncNamespaceUrl);
  71. xks.InnerText = String.Format ("{0}", keySize);
  72. xel.AppendChild (xks);
  73. }
  74. if (KeyAlgorithm != null)
  75. xel.SetAttribute (XmlEncryption.AttributeNames.Algorithm, KeyAlgorithm);
  76. return xel;
  77. }
  78. public void LoadXml (XmlElement value)
  79. {
  80. if (value == null)
  81. throw new ArgumentNullException ("value");
  82. if ((value.LocalName != XmlEncryption.ElementNames.EncryptionMethod) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  83. throw new CryptographicException ("Malformed EncryptionMethod element.");
  84. else {
  85. KeyAlgorithm = null;
  86. foreach (XmlNode n in value.ChildNodes) {
  87. if (n is XmlWhitespace)
  88. continue;
  89. switch (n.LocalName) {
  90. case XmlEncryption.ElementNames.KeySize:
  91. KeySize = Int32.Parse (n.InnerText);
  92. break;
  93. }
  94. }
  95. if (value.HasAttribute (XmlEncryption.AttributeNames.Algorithm))
  96. KeyAlgorithm = value.Attributes [XmlEncryption.AttributeNames.Algorithm].Value;
  97. }
  98. }
  99. #endregion // Methods
  100. }
  101. }
  102. #endif