EncryptionMethod.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #if NET_1_2
  10. using System.Xml;
  11. namespace System.Security.Cryptography.Xml {
  12. public class EncryptionMethod {
  13. #region Fields
  14. string algorithm;
  15. int keySize;
  16. #endregion // Fields
  17. #region Constructors
  18. public EncryptionMethod ()
  19. {
  20. KeyAlgorithm = null;
  21. }
  22. public EncryptionMethod (string strAlgorithm)
  23. {
  24. KeyAlgorithm = strAlgorithm;
  25. }
  26. #endregion // Constructors
  27. #region Properties
  28. public string KeyAlgorithm {
  29. get { return algorithm; }
  30. set { algorithm = value; }
  31. }
  32. public int KeySize {
  33. get { return keySize; }
  34. set {
  35. if (value <= 0)
  36. throw new ArgumentOutOfRangeException ("The key size should be a non negative integer.");
  37. keySize = value;
  38. }
  39. }
  40. #endregion // Properties
  41. #region Methods
  42. public XmlElement GetXml ()
  43. {
  44. return GetXml (new XmlDocument ());
  45. }
  46. internal XmlElement GetXml (XmlDocument document)
  47. {
  48. XmlElement xel = document.CreateElement (XmlEncryption.ElementNames.EncryptionMethod, EncryptedXml.XmlEncNamespaceUrl);
  49. if (KeySize != 0) {
  50. XmlElement xks = document.CreateElement (XmlEncryption.ElementNames.KeySize, EncryptedXml.XmlEncNamespaceUrl);
  51. xks.InnerText = String.Format ("{0}", keySize);
  52. xel.AppendChild (xks);
  53. }
  54. if (KeyAlgorithm != null)
  55. xel.SetAttribute (XmlEncryption.AttributeNames.Algorithm, KeyAlgorithm);
  56. return xel;
  57. }
  58. public void LoadXml (XmlElement value)
  59. {
  60. if (value == null)
  61. throw new ArgumentNullException ("value");
  62. if ((value.LocalName != XmlEncryption.ElementNames.EncryptionMethod) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
  63. throw new CryptographicException ("Malformed EncryptionMethod element.");
  64. else {
  65. KeyAlgorithm = null;
  66. foreach (XmlNode n in value.ChildNodes) {
  67. if (n is XmlWhitespace)
  68. continue;
  69. switch (n.LocalName) {
  70. case XmlEncryption.ElementNames.KeySize:
  71. KeySize = Int32.Parse (n.InnerText);
  72. break;
  73. }
  74. }
  75. if (value.HasAttribute (XmlEncryption.AttributeNames.Algorithm))
  76. KeyAlgorithm = value.Attributes [XmlEncryption.AttributeNames.Algorithm].Value;
  77. }
  78. }
  79. #endregion // Methods
  80. }
  81. }
  82. #endif