3 // Copyright (c) Microsoft Corporation. All rights reserved.
9 using System.Diagnostics.Contracts;
10 #endif // !SILVERLIGHT
12 namespace System.Security.Cryptography {
14 /// Managed implementation of the AES algorithm. AES is esentially Rijndael with a fixed block size
15 /// and iteration count, so we just wrap the RijndaelManaged class and allow only 128 bit blocks to
18 public sealed class AesManaged : Aes {
19 private RijndaelManaged m_rijndael;
23 Contract.Ensures(m_rijndael != null);
25 if (CryptoConfig.AllowOnlyFipsAlgorithms) {
26 throw new InvalidOperationException(SR.GetString(SR.Cryptography_NonCompliantFIPSAlgorithm));
28 #endif // !SILVERLIGHT
30 m_rijndael = new RijndaelManaged();
31 m_rijndael.BlockSize = BlockSize;
32 m_rijndael.KeySize = KeySize;
36 public override int FeedbackSize {
37 get { return m_rijndael.FeedbackSize; }
38 set { m_rijndael.FeedbackSize = value; }
40 #endif // !SILVERLIGHT
42 public override byte[] IV {
43 get { return m_rijndael.IV; }
44 set { m_rijndael.IV = value; }
47 public override byte[] Key {
48 get { return m_rijndael.Key; }
49 set { m_rijndael.Key = value; }
52 public override int KeySize {
53 get { return m_rijndael.KeySize; }
54 set { m_rijndael.KeySize = value; }
58 public override CipherMode Mode {
59 get { return m_rijndael.Mode; }
62 Contract.Ensures(m_rijndael.Mode != CipherMode.CFB && m_rijndael.Mode != CipherMode.OFB);
64 // RijndaelManaged will implicitly change the block size of an algorithm to match the number
65 // of feedback bits being used. Since AES requires a block size of 128 bits, we cannot allow
66 // the user to use the feedback modes, as this will end up breaking that invarient.
67 if (value == CipherMode.CFB || value == CipherMode.OFB) {
68 throw new CryptographicException(SR.GetString(SR.Cryptography_InvalidCipherMode));
71 m_rijndael.Mode = value;
75 public override PaddingMode Padding {
76 get { return m_rijndael.Padding; }
77 set { m_rijndael.Padding = value; }
79 #endif // !SILVERLIGHT
81 public override ICryptoTransform CreateDecryptor() {
82 return m_rijndael.CreateDecryptor();
85 public override ICryptoTransform CreateDecryptor(byte[] key, byte[] iv) {
87 throw new ArgumentNullException("key");
90 if (!ValidKeySize(key.Length * 8)) {
91 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeySize), "key");
93 if (iv != null && iv.Length * 8 != BlockSizeValue) {
94 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidIVSize), "iv");
98 return m_rijndael.CreateDecryptor(key, iv);
102 public override ICryptoTransform CreateEncryptor() {
103 return m_rijndael.CreateEncryptor();
106 public override ICryptoTransform CreateEncryptor(byte[] key, byte[] iv) {
108 throw new ArgumentNullException("key");
111 if (!ValidKeySize(key.Length * 8)) {
112 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidKeySize), "key");
114 if (iv != null && iv.Length * 8 != BlockSizeValue) {
115 throw new ArgumentException(SR.GetString(SR.Cryptography_InvalidIVSize), "iv");
117 #endif // SILVERLIGHT
119 return m_rijndael.CreateEncryptor(key, iv);
122 protected override void Dispose(bool disposing) {
125 (m_rijndael as IDisposable).Dispose();
129 base.Dispose(disposing);
133 public override void GenerateIV() {
134 m_rijndael.GenerateIV();
137 public override void GenerateKey() {
138 m_rijndael.GenerateKey();