Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / clr / src / BCL / System / Security / Cryptography / base64Transforms.cs / 1 / base64Transforms.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// Base64Transform.cs
//
// This file contains two ICryptoTransforms: ToBase64Transform and FromBase64Transform
// they may be attached to a CryptoStream in either read or write mode
namespace System.Security.Cryptography {
using System;
using System.IO;
using System.Text;
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FromBase64TransformMode {
IgnoreWhiteSpaces = 0,
DoNotIgnoreWhiteSpaces = 1,
}
[System.Runtime.InteropServices.ComVisible(true)]
public class ToBase64Transform : ICryptoTransform {
private ASCIIEncoding asciiEncoding = new ASCIIEncoding();
// converting to Base64 takes 3 bytes input and generates 4 bytes output
public int InputBlockSize {
get { return(3); }
}
public int OutputBlockSize {
get { return(4); }
}
public bool CanTransformMultipleBlocks {
get { return(false); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
if (asciiEncoding == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
// for now, only convert 3 bytes to 4
char[] temp = new char[4];
Convert.ToBase64CharArray(inputBuffer, inputOffset, 3, temp, 0);
byte[] tempBytes = asciiEncoding.GetBytes(temp);
if (tempBytes.Length != 4) throw new CryptographicException(Environment.GetResourceString( "Cryptography_SSE_InvalidDataSize" ));
Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length);
return(tempBytes.Length);
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
if (asciiEncoding == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
// Convert.ToBase64CharArray already does padding, so all we have to check is that
// the inputCount wasn't 0
// again, for now only a block at a time
if (inputCount == 0) {
return(new byte[0]);
} else {
char[] temp = new char[4];
Convert.ToBase64CharArray(inputBuffer, inputOffset, inputCount, temp, 0);
byte[] tempBytes = asciiEncoding.GetBytes(temp);
return(tempBytes);
}
}
// must implement IDisposable, but in this case there's nothing to do.
///
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
asciiEncoding = null;
}
}
~ToBase64Transform() {
Dispose(false);
}
}
[System.Runtime.InteropServices.ComVisible(true)]
public class FromBase64Transform : ICryptoTransform {
private byte[] _inputBuffer = new byte[4];
private int _inputIndex;
private FromBase64TransformMode _whitespaces;
// Constructors
public FromBase64Transform() : this(FromBase64TransformMode.IgnoreWhiteSpaces) {}
public FromBase64Transform(FromBase64TransformMode whitespaces) {
_whitespaces = whitespaces;
_inputIndex = 0;
}
// converting from Base64 generates 3 bytes output from each 4 bytes input block
public int InputBlockSize {
get { return(1); }
}
public int OutputBlockSize {
get { return(3); }
}
public bool CanTransformMultipleBlocks {
get { return(false); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
byte[] temp = new byte[inputCount];
char[] tempChar;
int effectiveCount;
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (_inputBuffer == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) {
temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount);
effectiveCount = temp.Length;
} else {
Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount);
effectiveCount = inputCount;
}
if (effectiveCount + _inputIndex < 4) {
Buffer.InternalBlockCopy(temp, 0, _inputBuffer, _inputIndex, effectiveCount);
_inputIndex += effectiveCount;
return 0;
}
// Get the number of 4 bytes blocks to transform
int numBlocks = (effectiveCount + _inputIndex) / 4;
byte[] transformBuffer = new byte[_inputIndex + effectiveCount];
Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex);
Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount);
_inputIndex = (effectiveCount + _inputIndex) % 4;
Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex);
tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks);
byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks);
Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length);
return(tempBytes.Length);
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
byte[] temp = new byte[inputCount];
char[] tempChar;
int effectiveCount;
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (_inputBuffer == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) {
temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount);
effectiveCount = temp.Length;
} else {
Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount);
effectiveCount = inputCount;
}
if (effectiveCount + _inputIndex < 4) {
Reset();
return (new byte[0]);
}
// Get the number of 4 bytes blocks to transform
int numBlocks = (effectiveCount + _inputIndex) / 4;
byte[] transformBuffer = new byte[_inputIndex + effectiveCount];
Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex);
Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount);
_inputIndex = (effectiveCount + _inputIndex) % 4;
Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex);
tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks);
byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks);
// reinitialize the transform
Reset();
return(tempBytes);
}
private byte[] DiscardWhiteSpaces(byte[] inputBuffer, int inputOffset, int inputCount) {
int i, iCount = 0;
for (i=0; i
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
// Reset the state of the transform so it can be used again
private void Reset() {
_inputIndex = 0;
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
// we always want to clear the input buffer
if (disposing) {
if (_inputBuffer != null)
Array.Clear(_inputBuffer, 0, _inputBuffer.Length);
_inputBuffer = null;
_inputIndex = 0;
}
}
~FromBase64Transform() {
Dispose(false);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
//
// Base64Transform.cs
//
// This file contains two ICryptoTransforms: ToBase64Transform and FromBase64Transform
// they may be attached to a CryptoStream in either read or write mode
namespace System.Security.Cryptography {
using System;
using System.IO;
using System.Text;
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FromBase64TransformMode {
IgnoreWhiteSpaces = 0,
DoNotIgnoreWhiteSpaces = 1,
}
[System.Runtime.InteropServices.ComVisible(true)]
public class ToBase64Transform : ICryptoTransform {
private ASCIIEncoding asciiEncoding = new ASCIIEncoding();
// converting to Base64 takes 3 bytes input and generates 4 bytes output
public int InputBlockSize {
get { return(3); }
}
public int OutputBlockSize {
get { return(4); }
}
public bool CanTransformMultipleBlocks {
get { return(false); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
if (asciiEncoding == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
// for now, only convert 3 bytes to 4
char[] temp = new char[4];
Convert.ToBase64CharArray(inputBuffer, inputOffset, 3, temp, 0);
byte[] tempBytes = asciiEncoding.GetBytes(temp);
if (tempBytes.Length != 4) throw new CryptographicException(Environment.GetResourceString( "Cryptography_SSE_InvalidDataSize" ));
Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length);
return(tempBytes.Length);
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
if (asciiEncoding == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
// Convert.ToBase64CharArray already does padding, so all we have to check is that
// the inputCount wasn't 0
// again, for now only a block at a time
if (inputCount == 0) {
return(new byte[0]);
} else {
char[] temp = new char[4];
Convert.ToBase64CharArray(inputBuffer, inputOffset, inputCount, temp, 0);
byte[] tempBytes = asciiEncoding.GetBytes(temp);
return(tempBytes);
}
}
// must implement IDisposable, but in this case there's nothing to do.
///
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
if (disposing) {
asciiEncoding = null;
}
}
~ToBase64Transform() {
Dispose(false);
}
}
[System.Runtime.InteropServices.ComVisible(true)]
public class FromBase64Transform : ICryptoTransform {
private byte[] _inputBuffer = new byte[4];
private int _inputIndex;
private FromBase64TransformMode _whitespaces;
// Constructors
public FromBase64Transform() : this(FromBase64TransformMode.IgnoreWhiteSpaces) {}
public FromBase64Transform(FromBase64TransformMode whitespaces) {
_whitespaces = whitespaces;
_inputIndex = 0;
}
// converting from Base64 generates 3 bytes output from each 4 bytes input block
public int InputBlockSize {
get { return(1); }
}
public int OutputBlockSize {
get { return(3); }
}
public bool CanTransformMultipleBlocks {
get { return(false); }
}
public virtual bool CanReuseTransform {
get { return(true); }
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
byte[] temp = new byte[inputCount];
char[] tempChar;
int effectiveCount;
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (_inputBuffer == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) {
temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount);
effectiveCount = temp.Length;
} else {
Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount);
effectiveCount = inputCount;
}
if (effectiveCount + _inputIndex < 4) {
Buffer.InternalBlockCopy(temp, 0, _inputBuffer, _inputIndex, effectiveCount);
_inputIndex += effectiveCount;
return 0;
}
// Get the number of 4 bytes blocks to transform
int numBlocks = (effectiveCount + _inputIndex) / 4;
byte[] transformBuffer = new byte[_inputIndex + effectiveCount];
Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex);
Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount);
_inputIndex = (effectiveCount + _inputIndex) % 4;
Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex);
tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks);
byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks);
Buffer.BlockCopy(tempBytes, 0, outputBuffer, outputOffset, tempBytes.Length);
return(tempBytes.Length);
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
byte[] temp = new byte[inputCount];
char[] tempChar;
int effectiveCount;
// Do some validation
if (inputBuffer == null) throw new ArgumentNullException("inputBuffer");
if (inputOffset < 0) throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (inputCount < 0 || (inputCount > inputBuffer.Length)) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((inputBuffer.Length - inputCount) < inputOffset) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if (_inputBuffer == null)
throw new ObjectDisposedException(null, Environment.GetResourceString("ObjectDisposed_Generic"));
if (_whitespaces == FromBase64TransformMode.IgnoreWhiteSpaces) {
temp = DiscardWhiteSpaces(inputBuffer, inputOffset, inputCount);
effectiveCount = temp.Length;
} else {
Buffer.InternalBlockCopy(inputBuffer, inputOffset, temp, 0, inputCount);
effectiveCount = inputCount;
}
if (effectiveCount + _inputIndex < 4) {
Reset();
return (new byte[0]);
}
// Get the number of 4 bytes blocks to transform
int numBlocks = (effectiveCount + _inputIndex) / 4;
byte[] transformBuffer = new byte[_inputIndex + effectiveCount];
Buffer.InternalBlockCopy(_inputBuffer, 0, transformBuffer, 0, _inputIndex);
Buffer.InternalBlockCopy(temp, 0, transformBuffer, _inputIndex, effectiveCount);
_inputIndex = (effectiveCount + _inputIndex) % 4;
Buffer.InternalBlockCopy(temp, effectiveCount - _inputIndex, _inputBuffer, 0, _inputIndex);
tempChar = Encoding.ASCII.GetChars(transformBuffer, 0, 4*numBlocks);
byte[] tempBytes = Convert.FromBase64CharArray(tempChar, 0, 4*numBlocks);
// reinitialize the transform
Reset();
return(tempBytes);
}
private byte[] DiscardWhiteSpaces(byte[] inputBuffer, int inputOffset, int inputCount) {
int i, iCount = 0;
for (i=0; i
void IDisposable.Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
// Reset the state of the transform so it can be used again
private void Reset() {
_inputIndex = 0;
}
public void Clear() {
((IDisposable) this).Dispose();
}
protected virtual void Dispose(bool disposing) {
// we always want to clear the input buffer
if (disposing) {
if (_inputBuffer != null)
Array.Clear(_inputBuffer, 0, _inputBuffer.Length);
_inputBuffer = null;
_inputIndex = 0;
}
}
~FromBase64Transform() {
Dispose(false);
}
}
}
// 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
- KerberosSecurityTokenAuthenticator.cs
- InputGestureCollection.cs
- WebSysDisplayNameAttribute.cs
- DynamicControlParameter.cs
- Polygon.cs
- TreeChangeInfo.cs
- PseudoWebRequest.cs
- MsmqVerifier.cs
- FastPropertyAccessor.cs
- TailCallAnalyzer.cs
- ArraySet.cs
- MappingSource.cs
- TextEditorTyping.cs
- ExceptionHelpers.cs
- MethodSignatureGenerator.cs
- Operand.cs
- DesignerCategoryAttribute.cs
- EdmProviderManifest.cs
- DataTable.cs
- FixUp.cs
- CompensatableTransactionScopeActivityDesigner.cs
- WebPartEditorOkVerb.cs
- FunctionImportMapping.cs
- CodeCommentStatementCollection.cs
- DBParameter.cs
- StringHelper.cs
- MemoryFailPoint.cs
- EventLogPermissionEntry.cs
- InputScopeManager.cs
- ScriptBehaviorDescriptor.cs
- SiteMapProvider.cs
- GridViewHeaderRowPresenterAutomationPeer.cs
- TextComposition.cs
- SystemWebExtensionsSectionGroup.cs
- MachineKeyConverter.cs
- shaperfactoryquerycacheentry.cs
- BitmapDownload.cs
- RtfNavigator.cs
- TagMapInfo.cs
- SystemWebSectionGroup.cs
- OdbcException.cs
- ListenerConfig.cs
- PortCache.cs
- ParagraphResult.cs
- AuthorizationRuleCollection.cs
- IpcClientManager.cs
- StopStoryboard.cs
- DataGridColumn.cs
- DesignerView.xaml.cs
- LinkButton.cs
- TimeSpan.cs
- SecurityTokenResolver.cs
- OpCellTreeNode.cs
- BamlStream.cs
- AttachedPropertyBrowsableAttribute.cs
- MetadataCache.cs
- _AutoWebProxyScriptHelper.cs
- BitmapEffectvisualstate.cs
- MachineKeySection.cs
- DomNameTable.cs
- VirtualDirectoryMapping.cs
- EmptyStringExpandableObjectConverter.cs
- ApplicationDirectory.cs
- DrawingContext.cs
- FilteredAttributeCollection.cs
- InkCanvasFeedbackAdorner.cs
- UserControlBuildProvider.cs
- SpecularMaterial.cs
- CircleHotSpot.cs
- TextHidden.cs
- StylusSystemGestureEventArgs.cs
- UnionCqlBlock.cs
- FontFamily.cs
- FilteredDataSetHelper.cs
- StreamUpgradeProvider.cs
- BindingsSection.cs
- CqlIdentifiers.cs
- SqlGenericUtil.cs
- ListSortDescriptionCollection.cs
- WindowsListViewItemCheckBox.cs
- SHA256Managed.cs
- FrameworkElementFactoryMarkupObject.cs
- EventLogRecord.cs
- XpsTokenContext.cs
- TransactionManager.cs
- ObservableCollection.cs
- ScriptingSectionGroup.cs
- Bidi.cs
- SortAction.cs
- UnsafeNativeMethods.cs
- WebPartDeleteVerb.cs
- TabControlAutomationPeer.cs
- elementinformation.cs
- Polyline.cs
- versioninfo.cs
- Gdiplus.cs
- ImageMapEventArgs.cs
- CompoundFileDeflateTransform.cs
- FixedElement.cs
- MulticastIPAddressInformationCollection.cs