Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Security / Cryptography / RSAPKCS1KeyExchangeFormatter.cs / 1 / RSAPKCS1KeyExchangeFormatter.cs
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
namespace System.Security.Cryptography {
using System.Globalization;
[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");
_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");
_rsaKey = (RSA) key;
}
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(String.Format(CultureInfo.CurrentCulture, 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- IntegrationExceptionEventArgs.cs
- StringCollectionMarkupSerializer.cs
- AutoGeneratedField.cs
- XLinq.cs
- Switch.cs
- KeyGestureConverter.cs
- DataGridViewColumnTypeEditor.cs
- SQLByte.cs
- SerializationHelper.cs
- ObjectQueryExecutionPlan.cs
- SettingsAttributes.cs
- AnonymousIdentificationSection.cs
- Baml2006Reader.cs
- SystemSounds.cs
- Operand.cs
- BinaryWriter.cs
- HTMLTextWriter.cs
- Pair.cs
- HttpWrapper.cs
- oledbmetadatacolumnnames.cs
- XmlTextEncoder.cs
- BasicCellRelation.cs
- IntMinMaxAggregationOperator.cs
- GB18030Encoding.cs
- Color.cs
- PackWebRequest.cs
- JoinSymbol.cs
- CharacterBuffer.cs
- ServiceNotStartedException.cs
- BuildProviderAppliesToAttribute.cs
- PtsPage.cs
- _OSSOCK.cs
- BaseParagraph.cs
- ISAPIApplicationHost.cs
- WebExceptionStatus.cs
- PeerCollaborationPermission.cs
- SecurityState.cs
- MarkupProperty.cs
- InvokeProviderWrapper.cs
- AsymmetricAlgorithm.cs
- Helpers.cs
- SchemaName.cs
- SqlCommandBuilder.cs
- EdmScalarPropertyAttribute.cs
- AuthenticationManager.cs
- BrowserCapabilitiesFactory35.cs
- RelatedEnd.cs
- ToolStripMenuItem.cs
- RowToParametersTransformer.cs
- UnmanagedMemoryStream.cs
- RenderOptions.cs
- RangeValueProviderWrapper.cs
- ContainerControl.cs
- DataControlPagerLinkButton.cs
- AttributeEmitter.cs
- TimerEventSubscriptionCollection.cs
- TabControlDesigner.cs
- Byte.cs
- OdbcStatementHandle.cs
- TiffBitmapDecoder.cs
- CodeCastExpression.cs
- PageThemeBuildProvider.cs
- dbdatarecord.cs
- XsltCompileContext.cs
- OrthographicCamera.cs
- XmlSchemaExternal.cs
- BitmapDecoder.cs
- ContextMenuService.cs
- ControlValuePropertyAttribute.cs
- BamlMapTable.cs
- ExpressionPrefixAttribute.cs
- SqlCrossApplyToCrossJoin.cs
- SessionParameter.cs
- MimeTypeAttribute.cs
- ArraySegment.cs
- DoubleAnimationClockResource.cs
- ChangeBlockUndoRecord.cs
- CompilationUtil.cs
- LinearGradientBrush.cs
- BinarySecretSecurityToken.cs
- CommonBehaviorsSection.cs
- IntegerCollectionEditor.cs
- ModuleElement.cs
- ThreadInterruptedException.cs
- DiscreteKeyFrames.cs
- BinaryNode.cs
- HtmlControlPersistable.cs
- HtmlAnchor.cs
- Focus.cs
- Geometry3D.cs
- ToolStripPanelCell.cs
- externdll.cs
- Subtract.cs
- ConfigurationManagerHelper.cs
- RemotingServices.cs
- NonPrimarySelectionGlyph.cs
- ImageCodecInfo.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- PrePostDescendentsWalker.cs
- XmlDataSourceView.cs