Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / RSAPKCS1KeyExchangeFormatter.cs / 1305376 / RSAPKCS1KeyExchangeFormatter.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
namespace System.Security.Cryptography {
using System.Globalization;
using System.Diagnostics.Contracts;
[System.Runtime.InteropServices.ComVisible(true)]
public class RSAPKCS1KeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
RandomNumberGenerator RngValue;
RSA _rsaKey;
//
// public constructors
//
public RSAPKCS1KeyExchangeFormatter() {}
public RSAPKCS1KeyExchangeFormatter(AsymmetricAlgorithm key) {
if (key == null)
throw new ArgumentNullException("key");
Contract.EndContractBlock();
_rsaKey = (RSA) key;
}
//
// public properties
//
public override String Parameters {
get { return " "; }
}
public RandomNumberGenerator Rng {
get { return RngValue; }
set { RngValue = value; }
}
//
// public methods
//
public override void SetKey(AsymmetricAlgorithm key) {
if (key == null)
throw new ArgumentNullException("key");
Contract.EndContractBlock();
_rsaKey = (RSA) key;
}
[System.Security.SecuritySafeCritical] // auto-generated
public override byte[] CreateKeyExchange(byte[] rgbData) {
if (_rsaKey == null)
throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
byte[] rgbKeyEx;
if (_rsaKey is RSACryptoServiceProvider) {
rgbKeyEx = ((RSACryptoServiceProvider) _rsaKey).Encrypt(rgbData, false);
}
else {
int cb = _rsaKey.KeySize/8;
if ((rgbData.Length + 11) > cb)
throw new CryptographicException(Environment.GetResourceString("Cryptography_Padding_EncDataTooBig", cb-11));
byte[] rgbInput = new byte[cb];
//
// We want to pad to the following format:
// 00 || 02 || PS || 00 || D
//
// PS - pseudorandom non zero bytes
// D - data
//
if (RngValue == null) {
RngValue = RandomNumberGenerator.Create();
}
Rng.GetNonZeroBytes(rgbInput);
rgbInput[0] = 0;
rgbInput[1] = 2;
rgbInput[cb-rgbData.Length-1] = 0;
Buffer.InternalBlockCopy(rgbData, 0, rgbInput, cb-rgbData.Length, rgbData.Length);
//
// Now encrypt the value and return it. (apply public key)
//
rgbKeyEx = _rsaKey.EncryptValue(rgbInput);
}
return rgbKeyEx;
}
public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) {
return CreateKeyExchange(rgbData);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
// [....]
//
namespace System.Security.Cryptography {
using System.Globalization;
using System.Diagnostics.Contracts;
[System.Runtime.InteropServices.ComVisible(true)]
public class RSAPKCS1KeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
RandomNumberGenerator RngValue;
RSA _rsaKey;
//
// public constructors
//
public RSAPKCS1KeyExchangeFormatter() {}
public RSAPKCS1KeyExchangeFormatter(AsymmetricAlgorithm key) {
if (key == null)
throw new ArgumentNullException("key");
Contract.EndContractBlock();
_rsaKey = (RSA) key;
}
//
// public properties
//
public override String Parameters {
get { return " "; }
}
public RandomNumberGenerator Rng {
get { return RngValue; }
set { RngValue = value; }
}
//
// public methods
//
public override void SetKey(AsymmetricAlgorithm key) {
if (key == null)
throw new ArgumentNullException("key");
Contract.EndContractBlock();
_rsaKey = (RSA) key;
}
[System.Security.SecuritySafeCritical] // auto-generated
public override byte[] CreateKeyExchange(byte[] rgbData) {
if (_rsaKey == null)
throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
byte[] rgbKeyEx;
if (_rsaKey is RSACryptoServiceProvider) {
rgbKeyEx = ((RSACryptoServiceProvider) _rsaKey).Encrypt(rgbData, false);
}
else {
int cb = _rsaKey.KeySize/8;
if ((rgbData.Length + 11) > cb)
throw new CryptographicException(Environment.GetResourceString("Cryptography_Padding_EncDataTooBig", cb-11));
byte[] rgbInput = new byte[cb];
//
// We want to pad to the following format:
// 00 || 02 || PS || 00 || D
//
// PS - pseudorandom non zero bytes
// D - data
//
if (RngValue == null) {
RngValue = RandomNumberGenerator.Create();
}
Rng.GetNonZeroBytes(rgbInput);
rgbInput[0] = 0;
rgbInput[1] = 2;
rgbInput[cb-rgbData.Length-1] = 0;
Buffer.InternalBlockCopy(rgbData, 0, rgbInput, cb-rgbData.Length, rgbData.Length);
//
// Now encrypt the value and return it. (apply public key)
//
rgbKeyEx = _rsaKey.EncryptValue(rgbInput);
}
return rgbKeyEx;
}
public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) {
return CreateKeyExchange(rgbData);
}
}
}
// 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
- WebPartVerbsEventArgs.cs
- CancellationState.cs
- SqlUserDefinedTypeAttribute.cs
- GridViewDeleteEventArgs.cs
- TagPrefixCollection.cs
- ServiceOperationParameter.cs
- Switch.cs
- FixedSOMTextRun.cs
- MTConfigUtil.cs
- TransactionBridge.cs
- CollaborationHelperFunctions.cs
- GridViewAutomationPeer.cs
- EntityClientCacheKey.cs
- DataGridViewTextBoxCell.cs
- FileInfo.cs
- XmlObjectSerializerReadContextComplexJson.cs
- figurelength.cs
- DisplayMemberTemplateSelector.cs
- XmlObjectSerializerWriteContextComplex.cs
- LocatorPart.cs
- GeneralTransform3D.cs
- ZeroOpNode.cs
- CatalogPart.cs
- SortQueryOperator.cs
- DeclaredTypeValidator.cs
- PageCatalogPart.cs
- ElementsClipboardData.cs
- assertwrapper.cs
- NamespaceQuery.cs
- DataTableClearEvent.cs
- XmlSchemaInclude.cs
- TreeBuilderXamlTranslator.cs
- XmlNamespaceDeclarationsAttribute.cs
- RequestQueryParser.cs
- MILUtilities.cs
- TransformGroup.cs
- InvalidPrinterException.cs
- ToolBarButton.cs
- DeclarativeExpressionConditionDeclaration.cs
- hwndwrapper.cs
- SourceChangedEventArgs.cs
- UserMapPath.cs
- Decorator.cs
- ControlIdConverter.cs
- TreeViewEvent.cs
- DoubleStorage.cs
- CryptoApi.cs
- StatusBar.cs
- FrameworkContextData.cs
- XamlToRtfParser.cs
- PartialCachingAttribute.cs
- TextDpi.cs
- SerializationObjectManager.cs
- ToolStripRendererSwitcher.cs
- ConfigurationManagerHelper.cs
- LiteralText.cs
- TextSelectionHighlightLayer.cs
- UserPersonalizationStateInfo.cs
- ConfigurationManagerHelperFactory.cs
- VectorConverter.cs
- RequestTimeoutManager.cs
- CFGGrammar.cs
- DbConvert.cs
- ViewPort3D.cs
- TypeUtil.cs
- Substitution.cs
- TextTreeDeleteContentUndoUnit.cs
- DragEventArgs.cs
- SettingsPropertyIsReadOnlyException.cs
- SizeConverter.cs
- GridViewUpdateEventArgs.cs
- Deflater.cs
- SmiEventStream.cs
- TextBounds.cs
- IconConverter.cs
- HandlerBase.cs
- Model3DGroup.cs
- WCFServiceClientProxyGenerator.cs
- DesignTimeResourceProviderFactoryAttribute.cs
- BitVec.cs
- ApplicationSecurityManager.cs
- HotSpot.cs
- UiaCoreApi.cs
- Geometry.cs
- SubtreeProcessor.cs
- SmiXetterAccessMap.cs
- ProtocolsConfiguration.cs
- ScanQueryOperator.cs
- FeatureSupport.cs
- RepeatButtonAutomationPeer.cs
- ListViewItemSelectionChangedEvent.cs
- SecurityManager.cs
- MimeBasePart.cs
- ProtectedConfiguration.cs
- UnicodeEncoding.cs
- SelectionBorderGlyph.cs
- ALinqExpressionVisitor.cs
- WebPartManager.cs
- XmlSchemaInferenceException.cs
- SqlCacheDependencyDatabaseCollection.cs