Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Tokens / RsaSecurityKey.cs / 1 / RsaSecurityKey.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel.Tokens { using System.IdentityModel.Selectors; using System.Security.Cryptography; using System.Security.Cryptography.Xml; sealed public class RsaSecurityKey : AsymmetricSecurityKey { PrivateKeyStatus privateKeyStatus = PrivateKeyStatus.AvailabilityNotDetermined; readonly RSA rsa; public RsaSecurityKey(RSA rsa) { if (rsa == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rsa"); this.rsa = rsa; } public override int KeySize { get { return this.rsa.KeySize; } } public override byte[] DecryptKey(string algorithm, byte[] keyData) { switch (algorithm) { case SecurityAlgorithms.RsaV15KeyWrap: return EncryptedXml.DecryptKey(keyData, rsa, false); case SecurityAlgorithms.RsaOaepKeyWrap: return EncryptedXml.DecryptKey(keyData, rsa, true); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "DecryptKey"))); } } public override byte[] EncryptKey(string algorithm, byte[] keyData) { switch (algorithm) { case SecurityAlgorithms.RsaV15KeyWrap: return EncryptedXml.EncryptKey(keyData, rsa, false); case SecurityAlgorithms.RsaOaepKeyWrap: return EncryptedXml.EncryptKey(keyData, rsa, true); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "EncryptKey"))); } } public override AsymmetricAlgorithm GetAsymmetricAlgorithm(string algorithm, bool requiresPrivateKey) { if (requiresPrivateKey && !HasPrivateKey()) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.NoPrivateKeyAvailable))); } return this.rsa; } public override HashAlgorithm GetHashAlgorithmForSignature(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return CryptoHelper.NewSha1HashAlgorithm(); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetHashAlgorithmForSignature"))); } } public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return new RSAPKCS1SignatureDeformatter(rsa); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureDeformatter"))); } } public override AsymmetricSignatureFormatter GetSignatureFormatter(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return new RSAPKCS1SignatureFormatter(rsa); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureFormatter"))); } } public override bool HasPrivateKey() { if (this.privateKeyStatus == PrivateKeyStatus.AvailabilityNotDetermined) { RSACryptoServiceProvider rsaCryptoServiceProvider = this.rsa as RSACryptoServiceProvider; if (rsaCryptoServiceProvider != null) { this.privateKeyStatus = rsaCryptoServiceProvider.PublicOnly ? PrivateKeyStatus.DoesNotHavePrivateKey : PrivateKeyStatus.HasPrivateKey; } else { try { byte[] hash = new byte[20]; this.rsa.DecryptValue(hash); // imitate signing this.privateKeyStatus = PrivateKeyStatus.HasPrivateKey; } catch (CryptographicException) { this.privateKeyStatus = PrivateKeyStatus.DoesNotHavePrivateKey; } } } return this.privateKeyStatus == PrivateKeyStatus.HasPrivateKey; } public override bool IsAsymmetricAlgorithm(string algorithm) { return CryptoHelper.IsAsymmetricAlgorithm(algorithm); } public override bool IsSupportedAlgorithm(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaV15KeyWrap: case SecurityAlgorithms.RsaOaepKeyWrap: case SecurityAlgorithms.RsaSha1Signature: return true; default: return false; } } public override bool IsSymmetricAlgorithm(string algorithm) { return CryptoHelper.IsSymmetricAlgorithm(algorithm); } enum PrivateKeyStatus { AvailabilityNotDetermined, HasPrivateKey, DoesNotHavePrivateKey } } } // 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
- QilScopedVisitor.cs
- StatusBarItemAutomationPeer.cs
- PTProvider.cs
- MarkupProperty.cs
- CheckBox.cs
- DataGridColumnHeaderAutomationPeer.cs
- ColumnMap.cs
- DataGridViewCellCollection.cs
- SimpleFileLog.cs
- nulltextnavigator.cs
- FontInfo.cs
- ListViewInsertionMark.cs
- MenuItem.cs
- QueryResult.cs
- Util.cs
- CookieHandler.cs
- TextRunCache.cs
- AttachedAnnotation.cs
- MetadataUtil.cs
- SqlError.cs
- SequenceDesignerAccessibleObject.cs
- DataControlPagerLinkButton.cs
- DesignerVerbToolStripMenuItem.cs
- TabletDevice.cs
- StrongNamePublicKeyBlob.cs
- ButtonBaseAutomationPeer.cs
- XmlSecureResolver.cs
- XmlSchemaObject.cs
- StringSorter.cs
- EastAsianLunisolarCalendar.cs
- AsnEncodedData.cs
- ActivityCodeDomReferenceService.cs
- ArrangedElementCollection.cs
- ListBase.cs
- PreviousTrackingServiceAttribute.cs
- DynamicRouteExpression.cs
- ElementNotAvailableException.cs
- ControlsConfig.cs
- SchemaCollectionCompiler.cs
- Funcletizer.cs
- TailPinnedEventArgs.cs
- IisTraceWebEventProvider.cs
- BrowserDefinition.cs
- ZipIOExtraFieldZip64Element.cs
- Rotation3D.cs
- ProcessHostServerConfig.cs
- ExpanderAutomationPeer.cs
- HitTestDrawingContextWalker.cs
- TreeNodeBinding.cs
- Rfc2898DeriveBytes.cs
- RegexReplacement.cs
- ListContractAdapter.cs
- SqlResolver.cs
- RightsManagementSuppressedStream.cs
- Boolean.cs
- AssemblyAssociatedContentFileAttribute.cs
- PreviewPageInfo.cs
- COM2ComponentEditor.cs
- Emitter.cs
- DocumentPageViewAutomationPeer.cs
- DictionaryEntry.cs
- TimelineGroup.cs
- WebPartConnectionsConfigureVerb.cs
- DataListCommandEventArgs.cs
- IndexExpression.cs
- InheritanceRules.cs
- InterleavedZipPartStream.cs
- StandardRuntimeEnumValidator.cs
- ActivityExecutionContextCollection.cs
- GridEntry.cs
- HashFinalRequest.cs
- DataGridViewCell.cs
- SessionStateUtil.cs
- DetailsViewInsertEventArgs.cs
- SpecularMaterial.cs
- CompletedAsyncResult.cs
- ImageBrush.cs
- RbTree.cs
- EnumUnknown.cs
- ConfigXmlCDataSection.cs
- FileDialogPermission.cs
- CopyCodeAction.cs
- DesignerAdapterUtil.cs
- EnumerableRowCollection.cs
- ExtentCqlBlock.cs
- InvokeSchedule.cs
- Parser.cs
- MouseButton.cs
- Int64.cs
- RtType.cs
- StaticExtensionConverter.cs
- SqlProfileProvider.cs
- InfocardExtendedInformationEntry.cs
- CodeNamespaceImportCollection.cs
- CompositeTypefaceMetrics.cs
- BinaryUtilClasses.cs
- LogoValidationException.cs
- CatalogPartChrome.cs
- ClientRoleProvider.cs
- ScrollBar.cs