Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Security / Cryptography / SymmetricAlgorithm.cs / 1 / SymmetricAlgorithm.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// SymmetricAlgorithm.cs
//
namespace System.Security.Cryptography {
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class SymmetricAlgorithm : IDisposable {
protected int BlockSizeValue;
protected int FeedbackSizeValue;
protected byte[] IVValue;
protected byte[] KeyValue;
protected KeySizes[] LegalBlockSizesValue;
protected KeySizes[] LegalKeySizesValue;
protected int KeySizeValue;
protected CipherMode ModeValue;
protected PaddingMode PaddingValue;
//
// protected constructors
//
protected SymmetricAlgorithm() {
// Default to cipher block chaining (CipherMode.CBC) and
// PKCS-style padding (pad n bytes with value n)
ModeValue = CipherMode.CBC;
PaddingValue = PaddingMode.PKCS7;
}
// SymmetricAlgorithm implements IDisposable
///
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
// Note: we always want to zeroize the sensitive key material
if (KeyValue != null) {
Array.Clear(KeyValue, 0, KeyValue.Length);
KeyValue = null;
}
if (IVValue != null) {
Array.Clear(IVValue, 0, IVValue.Length);
IVValue = null;
}
}
}
//
// public properties
//
public virtual int BlockSize {
get { return BlockSizeValue; }
set {
int i;
int j;
for (i=0; i BlockSizeValue || (value % 8) != 0)
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFeedbackSize"));
FeedbackSizeValue = value;
}
}
public virtual byte[] IV {
get {
if (IVValue == null) GenerateIV();
return (byte[]) IVValue.Clone();
}
set {
if (value == null) throw new ArgumentNullException("value");
if (value.Length != BlockSizeValue / 8)
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
IVValue = (byte[]) value.Clone();
}
}
public virtual byte[] Key {
get {
if (KeyValue == null) GenerateKey();
return (byte[]) KeyValue.Clone();
}
set {
if (value == null) throw new ArgumentNullException("value");
if (!ValidKeySize(value.Length * 8))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
// must convert bytes to bits
KeyValue = (byte[]) value.Clone();
KeySizeValue = value.Length * 8;
}
}
public virtual KeySizes[] LegalBlockSizes {
get { return (KeySizes[]) LegalBlockSizesValue.Clone(); }
}
public virtual KeySizes[] LegalKeySizes {
get { return (KeySizes[]) LegalKeySizesValue.Clone(); }
}
public virtual int KeySize {
get { return KeySizeValue; }
set {
if (!ValidKeySize(value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
KeySizeValue = value;
KeyValue = null;
}
}
public virtual CipherMode Mode {
get { return ModeValue; }
set {
if ((value < CipherMode.CBC) || (CipherMode.CFB < value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidCipherMode"));
ModeValue = value;
}
}
public virtual PaddingMode Padding {
get { return PaddingValue; }
set {
if ((value < PaddingMode.None) || (PaddingMode.ISO10126 < value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidPaddingMode"));
PaddingValue = value;
}
}
//
// public methods
//
// The following method takes a bit length input and returns whether that length is a valid size
// according to LegalKeySizes
public bool ValidKeySize(int bitLength) {
KeySizes[] validSizes = this.LegalKeySizes;
int i,j;
if (validSizes == null) return false;
for (i=0; i< validSizes.Length; i++) {
if (validSizes[i].SkipSize == 0) {
if (validSizes[i].MinSize == bitLength) { // assume MinSize = MaxSize
return true;
}
} else {
for (j = validSizes[i].MinSize; j<= validSizes[i].MaxSize;
j += validSizes[i].SkipSize) {
if (j == bitLength) {
return true;
}
}
}
}
return false;
}
static public SymmetricAlgorithm Create() {
// use the crypto config system to return an instance of
// the default SymmetricAlgorithm on this machine
return Create("System.Security.Cryptography.SymmetricAlgorithm");
}
static public SymmetricAlgorithm Create(String algName) {
return (SymmetricAlgorithm) CryptoConfig.CreateFromName(algName);
}
public virtual ICryptoTransform CreateEncryptor() {
return CreateEncryptor(Key, IV);
}
public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
public virtual ICryptoTransform CreateDecryptor() {
return CreateDecryptor(Key, IV);
}
public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
public abstract void GenerateKey();
public abstract void GenerateIV();
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// SymmetricAlgorithm.cs
//
namespace System.Security.Cryptography {
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class SymmetricAlgorithm : IDisposable {
protected int BlockSizeValue;
protected int FeedbackSizeValue;
protected byte[] IVValue;
protected byte[] KeyValue;
protected KeySizes[] LegalBlockSizesValue;
protected KeySizes[] LegalKeySizesValue;
protected int KeySizeValue;
protected CipherMode ModeValue;
protected PaddingMode PaddingValue;
//
// protected constructors
//
protected SymmetricAlgorithm() {
// Default to cipher block chaining (CipherMode.CBC) and
// PKCS-style padding (pad n bytes with value n)
ModeValue = CipherMode.CBC;
PaddingValue = PaddingMode.PKCS7;
}
// SymmetricAlgorithm implements IDisposable
///
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
// Note: we always want to zeroize the sensitive key material
if (KeyValue != null) {
Array.Clear(KeyValue, 0, KeyValue.Length);
KeyValue = null;
}
if (IVValue != null) {
Array.Clear(IVValue, 0, IVValue.Length);
IVValue = null;
}
}
}
//
// public properties
//
public virtual int BlockSize {
get { return BlockSizeValue; }
set {
int i;
int j;
for (i=0; i BlockSizeValue || (value % 8) != 0)
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFeedbackSize"));
FeedbackSizeValue = value;
}
}
public virtual byte[] IV {
get {
if (IVValue == null) GenerateIV();
return (byte[]) IVValue.Clone();
}
set {
if (value == null) throw new ArgumentNullException("value");
if (value.Length != BlockSizeValue / 8)
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidIVSize"));
IVValue = (byte[]) value.Clone();
}
}
public virtual byte[] Key {
get {
if (KeyValue == null) GenerateKey();
return (byte[]) KeyValue.Clone();
}
set {
if (value == null) throw new ArgumentNullException("value");
if (!ValidKeySize(value.Length * 8))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
// must convert bytes to bits
KeyValue = (byte[]) value.Clone();
KeySizeValue = value.Length * 8;
}
}
public virtual KeySizes[] LegalBlockSizes {
get { return (KeySizes[]) LegalBlockSizesValue.Clone(); }
}
public virtual KeySizes[] LegalKeySizes {
get { return (KeySizes[]) LegalKeySizesValue.Clone(); }
}
public virtual int KeySize {
get { return KeySizeValue; }
set {
if (!ValidKeySize(value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
KeySizeValue = value;
KeyValue = null;
}
}
public virtual CipherMode Mode {
get { return ModeValue; }
set {
if ((value < CipherMode.CBC) || (CipherMode.CFB < value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidCipherMode"));
ModeValue = value;
}
}
public virtual PaddingMode Padding {
get { return PaddingValue; }
set {
if ((value < PaddingMode.None) || (PaddingMode.ISO10126 < value))
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidPaddingMode"));
PaddingValue = value;
}
}
//
// public methods
//
// The following method takes a bit length input and returns whether that length is a valid size
// according to LegalKeySizes
public bool ValidKeySize(int bitLength) {
KeySizes[] validSizes = this.LegalKeySizes;
int i,j;
if (validSizes == null) return false;
for (i=0; i< validSizes.Length; i++) {
if (validSizes[i].SkipSize == 0) {
if (validSizes[i].MinSize == bitLength) { // assume MinSize = MaxSize
return true;
}
} else {
for (j = validSizes[i].MinSize; j<= validSizes[i].MaxSize;
j += validSizes[i].SkipSize) {
if (j == bitLength) {
return true;
}
}
}
}
return false;
}
static public SymmetricAlgorithm Create() {
// use the crypto config system to return an instance of
// the default SymmetricAlgorithm on this machine
return Create("System.Security.Cryptography.SymmetricAlgorithm");
}
static public SymmetricAlgorithm Create(String algName) {
return (SymmetricAlgorithm) CryptoConfig.CreateFromName(algName);
}
public virtual ICryptoTransform CreateEncryptor() {
return CreateEncryptor(Key, IV);
}
public abstract ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV);
public virtual ICryptoTransform CreateDecryptor() {
return CreateDecryptor(Key, IV);
}
public abstract ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV);
public abstract void GenerateKey();
public abstract void GenerateIV();
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Directory.cs
- CriticalFinalizerObject.cs
- ToolStripItem.cs
- TransformerInfo.cs
- MessageProperties.cs
- PropertyIDSet.cs
- ConfigurationElementProperty.cs
- CodeGenerator.cs
- XmlTextReader.cs
- QueueAccessMode.cs
- EventPrivateKey.cs
- ProfileGroupSettingsCollection.cs
- XmlQueryStaticData.cs
- TransformFinalBlockRequest.cs
- HostingEnvironmentException.cs
- TableRowsCollectionEditor.cs
- Context.cs
- CompressedStack.cs
- ConsoleKeyInfo.cs
- Identifier.cs
- TypeUnloadedException.cs
- SerTrace.cs
- PlainXmlWriter.cs
- DataGridViewRowsRemovedEventArgs.cs
- XLinq.cs
- ByteConverter.cs
- mediaeventshelper.cs
- EdgeProfileValidation.cs
- Guid.cs
- DurableOperationAttribute.cs
- DetailsViewModeEventArgs.cs
- Image.cs
- TextEndOfLine.cs
- CharEntityEncoderFallback.cs
- DefaultAuthorizationContext.cs
- Int64Storage.cs
- AppDomain.cs
- LinkConverter.cs
- XmlConvert.cs
- SystemBrushes.cs
- DataGridColumn.cs
- IdnElement.cs
- ResourceManager.cs
- ProcessProtocolHandler.cs
- DataGridViewCellLinkedList.cs
- GB18030Encoding.cs
- DataServiceRequestArgs.cs
- FastEncoderWindow.cs
- XmlSchemaCompilationSettings.cs
- CompositionTarget.cs
- InertiaRotationBehavior.cs
- ColorComboBox.cs
- ContextMenu.cs
- MediaCommands.cs
- Duration.cs
- DataGridDesigner.cs
- SystemTcpConnection.cs
- WindowsFont.cs
- EntityViewGenerator.cs
- TimeStampChecker.cs
- MonthCalendar.cs
- HashRepartitionStream.cs
- PropVariant.cs
- TextProviderWrapper.cs
- TemplateBindingExtensionConverter.cs
- ContentElement.cs
- ThreadExceptionEvent.cs
- LocalIdCollection.cs
- ColumnHeader.cs
- TimeSpanStorage.cs
- CategoryNameCollection.cs
- WindowsPrincipal.cs
- ObjectReferenceStack.cs
- RenderContext.cs
- SystemSounds.cs
- StringConcat.cs
- InvalidDataException.cs
- COM2IPerPropertyBrowsingHandler.cs
- WebHttpBehavior.cs
- WebPartConnection.cs
- PeerPresenceInfo.cs
- MethodRental.cs
- CompoundFileReference.cs
- QuaternionKeyFrameCollection.cs
- HtmlEncodedRawTextWriter.cs
- VisualBasicDesignerHelper.cs
- SynchronizationLockException.cs
- XmlException.cs
- Decimal.cs
- ReflectEventDescriptor.cs
- Module.cs
- _UriSyntax.cs
- ComponentSerializationService.cs
- TemplateBindingExtension.cs
- ToolStripContentPanelRenderEventArgs.cs
- DesignerForm.cs
- ChtmlTextWriter.cs
- ObjectListItem.cs
- Tokenizer.cs
- DbConnectionClosed.cs