AesCryptoServiceProvider.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // System.Security.Cryptography.AesCryptoServiceProvider class
  3. //
  4. // Authors:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. // Copyright 2013 Xamarin Inc (http://www.xamarin.com)
  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. using System.Security.Permissions;
  30. using Mono.Security.Cryptography;
  31. namespace System.Security.Cryptography {
  32. [HostProtection (SecurityAction.LinkDemand, MayLeakOnAbort=true)]
  33. public sealed class AesCryptoServiceProvider : Aes {
  34. public AesCryptoServiceProvider ()
  35. {
  36. FeedbackSizeValue = 8;
  37. }
  38. public override void GenerateIV ()
  39. {
  40. IVValue = KeyBuilder.IV (BlockSizeValue >> 3);
  41. }
  42. public override void GenerateKey ()
  43. {
  44. KeyValue = KeyBuilder.Key (KeySizeValue >> 3);
  45. }
  46. public override ICryptoTransform CreateDecryptor (byte[] rgbKey, byte[] rgbIV)
  47. {
  48. if ((Mode == CipherMode.CFB) && (FeedbackSize > 64))
  49. throw new CryptographicException ("CFB with Feedbaack > 64 bits");
  50. return new AesTransform (this, false, rgbKey, rgbIV);
  51. }
  52. public override ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV)
  53. {
  54. if ((Mode == CipherMode.CFB) && (FeedbackSize > 64))
  55. throw new CryptographicException ("CFB with Feedbaack > 64 bits");
  56. return new AesTransform (this, true, rgbKey, rgbIV);
  57. }
  58. // I suppose some attributes differs ?!? because this does not look required
  59. public override byte[] IV {
  60. get { return base.IV; }
  61. set { base.IV = value; }
  62. }
  63. public override byte[] Key {
  64. get { return base.Key; }
  65. set { base.Key = value; }
  66. }
  67. public override int KeySize {
  68. get { return base.KeySize; }
  69. set { base.KeySize = value; }
  70. }
  71. public override int FeedbackSize {
  72. get { return base.FeedbackSize; }
  73. set { base.FeedbackSize = value; }
  74. }
  75. public override CipherMode Mode {
  76. get { return base.Mode; }
  77. set {
  78. switch (value) {
  79. case CipherMode.CTS:
  80. throw new CryptographicException ("CTS is not supported");
  81. default:
  82. base.Mode = value;
  83. break;
  84. }
  85. }
  86. }
  87. public override PaddingMode Padding {
  88. get { return base.Padding; }
  89. set { base.Padding = value; }
  90. }
  91. public override ICryptoTransform CreateDecryptor ()
  92. {
  93. return CreateDecryptor (Key, IV);
  94. }
  95. public override ICryptoTransform CreateEncryptor()
  96. {
  97. return CreateEncryptor (Key, IV);
  98. }
  99. protected override void Dispose (bool disposing)
  100. {
  101. base.Dispose (disposing);
  102. }
  103. }
  104. }