mscorlib(4.0.0.0) API with additions
SymmetricAlgorithm.cs
2 
4 {
6  [ComVisible(true)]
7  public abstract class SymmetricAlgorithm : IDisposable
8  {
10  protected int BlockSizeValue;
11 
13  protected int FeedbackSizeValue;
14 
16  protected byte[] IVValue;
17 
19  protected byte[] KeyValue;
20 
23 
26 
28  protected int KeySizeValue;
29 
31  protected CipherMode ModeValue;
32 
35 
39  public virtual int BlockSize
40  {
41  get
42  {
43  return BlockSizeValue;
44  }
45  set
46  {
47  for (int i = 0; i < LegalBlockSizesValue.Length; i++)
48  {
49  if (LegalBlockSizesValue[i].SkipSize == 0)
50  {
51  if (LegalBlockSizesValue[i].MinSize == value)
52  {
53  BlockSizeValue = value;
54  IVValue = null;
55  return;
56  }
57  continue;
58  }
59  for (int j = LegalBlockSizesValue[i].MinSize; j <= LegalBlockSizesValue[i].MaxSize; j += LegalBlockSizesValue[i].SkipSize)
60  {
61  if (j == value)
62  {
63  if (BlockSizeValue != value)
64  {
65  BlockSizeValue = value;
66  IVValue = null;
67  }
68  return;
69  }
70  }
71  }
72  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidBlockSize"));
73  }
74  }
75 
79  public virtual int FeedbackSize
80  {
81  get
82  {
83  return FeedbackSizeValue;
84  }
85  set
86  {
87  if (value <= 0 || value > BlockSizeValue || value % 8 != 0)
88  {
89  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFeedbackSize"));
90  }
91  FeedbackSizeValue = value;
92  }
93  }
94 
99  public virtual byte[] IV
100  {
101  get
102  {
103  if (IVValue == null)
104  {
105  GenerateIV();
106  }
107  return (byte[])IVValue.Clone();
108  }
109  set
110  {
111  if (value == null)
112  {
113  throw new ArgumentNullException("value");
114  }
115  if (value.Length != BlockSizeValue / 8)
116  {
117  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
118  }
119  IVValue = (byte[])value.Clone();
120  }
121  }
122 
127  public virtual byte[] Key
128  {
129  get
130  {
131  if (KeyValue == null)
132  {
133  GenerateKey();
134  }
135  return (byte[])KeyValue.Clone();
136  }
137  set
138  {
139  if (value == null)
140  {
141  throw new ArgumentNullException("value");
142  }
143  if (!ValidKeySize(value.Length * 8))
144  {
145  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
146  }
147  KeyValue = (byte[])value.Clone();
148  KeySizeValue = value.Length * 8;
149  }
150  }
151 
154  public virtual KeySizes[] LegalBlockSizes => (KeySizes[])LegalBlockSizesValue.Clone();
155 
158  public virtual KeySizes[] LegalKeySizes => (KeySizes[])LegalKeySizesValue.Clone();
159 
163  public virtual int KeySize
164  {
165  get
166  {
167  return KeySizeValue;
168  }
169  set
170  {
171  if (!ValidKeySize(value))
172  {
173  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
174  }
175  KeySizeValue = value;
176  KeyValue = null;
177  }
178  }
179 
183  public virtual CipherMode Mode
184  {
185  get
186  {
187  return ModeValue;
188  }
189  set
190  {
191  if (value < CipherMode.CBC || CipherMode.CFB < value)
192  {
193  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidCipherMode"));
194  }
195  ModeValue = value;
196  }
197  }
198 
202  public virtual PaddingMode Padding
203  {
204  get
205  {
206  return PaddingValue;
207  }
208  set
209  {
210  if (value < PaddingMode.None || PaddingMode.ISO10126 < value)
211  {
212  throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidPaddingMode"));
213  }
214  PaddingValue = value;
215  }
216  }
217 
220  protected SymmetricAlgorithm()
221  {
222  ModeValue = CipherMode.CBC;
223  PaddingValue = PaddingMode.PKCS7;
224  }
225 
227  public void Dispose()
228  {
229  Dispose(disposing: true);
230  GC.SuppressFinalize(this);
231  }
232 
234  public void Clear()
235  {
236  ((IDisposable)this).Dispose();
237  }
238 
242  protected virtual void Dispose(bool disposing)
243  {
244  if (disposing)
245  {
246  if (KeyValue != null)
247  {
248  Array.Clear(KeyValue, 0, KeyValue.Length);
249  KeyValue = null;
250  }
251  if (IVValue != null)
252  {
253  Array.Clear(IVValue, 0, IVValue.Length);
254  IVValue = null;
255  }
256  }
257  }
258 
263  public bool ValidKeySize(int bitLength)
264  {
265  KeySizes[] legalKeySizes = LegalKeySizes;
266  if (legalKeySizes == null)
267  {
268  return false;
269  }
270  for (int i = 0; i < legalKeySizes.Length; i++)
271  {
272  if (legalKeySizes[i].SkipSize == 0)
273  {
274  if (legalKeySizes[i].MinSize == bitLength)
275  {
276  return true;
277  }
278  continue;
279  }
280  for (int j = legalKeySizes[i].MinSize; j <= legalKeySizes[i].MaxSize; j += legalKeySizes[i].SkipSize)
281  {
282  if (j == bitLength)
283  {
284  return true;
285  }
286  }
287  }
288  return false;
289  }
290 
293  public static SymmetricAlgorithm Create()
294  {
295  return Create("System.Security.Cryptography.SymmetricAlgorithm");
296  }
297 
301  public static SymmetricAlgorithm Create(string algName)
302  {
304  }
305 
309  {
310  return CreateEncryptor(Key, IV);
311  }
312 
317  public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
318 
322  {
323  return CreateDecryptor(Key, IV);
324  }
325 
330  public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
331 
333  public abstract void GenerateKey();
334 
336  public abstract void GenerateIV();
337  }
338 }
The exception that is thrown when an error occurs during a cryptographic operation.
virtual KeySizes [] LegalKeySizes
Gets the key sizes, in bits, that are supported by the symmetric algorithm.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Cryptography.SymmetricAlgorithm and op...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
byte [] KeyValue
Represents the secret key for the symmetric algorithm.
byte [] IVValue
Represents the initialization vector (P:System.Security.Cryptography.SymmetricAlgorithm....
PaddingMode PaddingValue
Represents the padding mode used in the symmetric algorithm.
virtual ICryptoTransform CreateEncryptor()
Creates a symmetric encryptor object with the current P:System.Security.Cryptography....
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
KeySizes [] LegalBlockSizesValue
Specifies the block sizes, in bits, that are supported by the symmetric algorithm.
KeySizes [] LegalKeySizesValue
Specifies the key sizes, in bits, that are supported by the symmetric algorithm.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
virtual int KeySize
Gets or sets the size, in bits, of the secret key used by the symmetric algorithm.
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
Definition: __Canon.cs:3
void Clear()
Releases all resources used by the T:System.Security.Cryptography.SymmetricAlgorithm class.
int MaxSize
Specifies the maximum key size in bits.
Definition: KeySizes.cs:21
virtual ICryptoTransform CreateDecryptor()
Creates a symmetric decryptor object with the current P:System.Security.Cryptography....
virtual int BlockSize
Gets or sets the block size, in bits, of the cryptographic operation.
virtual CipherMode Mode
Gets or sets the mode for operation of the symmetric algorithm.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Accesses the cryptography configuration information.
Definition: CryptoConfig.cs:17
SymmetricAlgorithm()
Initializes a new instance of the T:System.Security.Cryptography.SymmetricAlgorithm class.
virtual byte [] Key
Gets or sets the secret key for the symmetric algorithm.
int KeySizeValue
Represents the size, in bits, of the secret key used by the symmetric algorithm.
virtual KeySizes [] LegalBlockSizes
Gets the block sizes, in bits, that are supported by the symmetric algorithm.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
int SkipSize
Specifies the interval between valid key sizes in bits.
Definition: KeySizes.cs:25
Determines the set of valid key sizes for the symmetric cryptographic algorithms.
Definition: KeySizes.cs:7
virtual int FeedbackSize
Gets or sets the feedback size, in bits, of the cryptographic operation.
CipherMode ModeValue
Represents the cipher mode used in the symmetric algorithm.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
bool ValidKeySize(int bitLength)
Determines whether the specified key size is valid for the current algorithm.
int FeedbackSizeValue
Represents the feedback size, in bits, of the cryptographic operation.
void Dispose()
Releases all resources used by the current instance of the T:System.Security.Cryptography....
virtual PaddingMode Padding
Gets or sets the padding mode used in the symmetric algorithm.
Represents the abstract base class from which all implementations of symmetric algorithms must inheri...
int BlockSizeValue
Represents the block size, in bits, of the cryptographic operation.
abstract void GenerateIV()
When overridden in a derived class, generates a random initialization vector (P:System....
CipherMode
Specifies the block cipher mode to use for encryption.
Definition: CipherMode.cs:8
Defines the basic operations of cryptographic transformations.
static SymmetricAlgorithm Create()
Creates a default cryptographic object used to perform the symmetric algorithm.
static SymmetricAlgorithm Create(string algName)
Creates the specified cryptographic object used to perform the symmetric algorithm.
virtual byte [] IV
Gets or sets the initialization vector (P:System.Security.Cryptography.SymmetricAlgorithm....
abstract void GenerateKey()
When overridden in a derived class, generates a random key (P:System.Security.Cryptography....
PaddingMode
Specifies the type of padding to apply when the message data block is shorter than the full number of...
Definition: PaddingMode.cs:8