Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Security / Cryptography / TripleDES.cs / 1 / TripleDES.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // TripleDES.cs // namespace System.Security.Cryptography { [System.Runtime.InteropServices.ComVisible(true)] public abstract class TripleDES : SymmetricAlgorithm { private static KeySizes[] s_legalBlockSizes = { new KeySizes(64, 64, 0) }; private static KeySizes[] s_legalKeySizes = { new KeySizes(2*64, 3*64, 64) }; // // protected constructors // protected TripleDES() { KeySizeValue = 3*64; BlockSizeValue = 64; FeedbackSizeValue = BlockSizeValue; LegalBlockSizesValue = s_legalBlockSizes; LegalKeySizesValue = s_legalKeySizes; } // // public properties // public override byte[] Key { get { if (KeyValue == null) { // Never hand back a weak key do { GenerateKey(); } while (IsWeakKey(KeyValue)); } return (byte[]) KeyValue.Clone(); } set { if (value == null) throw new ArgumentNullException("value"); if (!ValidKeySize(value.Length * 8)) { // must convert bytes to bits throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } if (IsWeakKey(value)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKey_Weak"),"TripleDES"); } KeyValue = (byte[]) value.Clone(); KeySizeValue = value.Length * 8; } } // // public methods // new static public TripleDES Create() { return Create("System.Security.Cryptography.TripleDES"); } new static public TripleDES Create(String str) { return (TripleDES) CryptoConfig.CreateFromName(str); } public static bool IsWeakKey(byte[] rgbKey) { // All we have to if (!IsLegalKeySize(rgbKey)) { throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize")); } byte[] rgbOddParityKey = Utils.FixupKeyParity(rgbKey); if (EqualBytes(rgbOddParityKey,0,8,8)) return(true); if ((rgbOddParityKey.Length == 24) && EqualBytes(rgbOddParityKey,8,16,8)) return(true); return(false); } // // private methods // private static bool EqualBytes(byte[] rgbKey, int start1, int start2, int count) { if (start1 < 0) throw new ArgumentOutOfRangeException("start1", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if (start2 < 0) throw new ArgumentOutOfRangeException("start2", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); if ((start1+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); if ((start2+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue")); for (int i = 0; i < count; i++) { if (rgbKey[start1+i] != rgbKey[start2+i]) return(false); } return(true); } private static bool IsLegalKeySize(byte[] rgbKey) { if (rgbKey != null && ((rgbKey.Length == 16) || (rgbKey.Length == 24))) return(true); return(false); } } } // 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
- SqlDataSource.cs
- SQLDouble.cs
- CompModSwitches.cs
- CopyOnWriteList.cs
- SafeSerializationManager.cs
- MaskedTextProvider.cs
- FixedSOMSemanticBox.cs
- HttpListenerContext.cs
- DefaultEventAttribute.cs
- HttpServerUtilityBase.cs
- DesignerActionUIService.cs
- DocumentAutomationPeer.cs
- CodePropertyReferenceExpression.cs
- FixedNode.cs
- SecurityElement.cs
- CachedFontFace.cs
- WindowsScrollBarBits.cs
- IndexingContentUnit.cs
- FixedPageProcessor.cs
- WindowsTab.cs
- SystemSounds.cs
- EntityTransaction.cs
- IConvertible.cs
- ApplicationServicesHostFactory.cs
- MetadataArtifactLoaderResource.cs
- HttpCacheVary.cs
- Constants.cs
- AlphaSortedEnumConverter.cs
- UdpAnnouncementEndpoint.cs
- DynamicPropertyHolder.cs
- PhoneCall.cs
- FloaterBaseParaClient.cs
- FocusWithinProperty.cs
- ModifierKeysValueSerializer.cs
- TextEditorSelection.cs
- NameTable.cs
- Command.cs
- DataGridViewCheckBoxColumn.cs
- XmlSchemaAttributeGroup.cs
- WebRequest.cs
- SubMenuStyleCollection.cs
- PseudoWebRequest.cs
- GraphicsState.cs
- DynamicActivityTypeDescriptor.cs
- AsyncOperation.cs
- InfoCardKeyedHashAlgorithm.cs
- SystemSounds.cs
- AuditLogLocation.cs
- ConsumerConnectionPointCollection.cs
- VarRemapper.cs
- SecurityRuntime.cs
- Win32.cs
- DetailsViewDeletedEventArgs.cs
- SourceInterpreter.cs
- BooleanStorage.cs
- PlacementWorkspace.cs
- ScopeCollection.cs
- NullableDecimalAverageAggregationOperator.cs
- PointAnimationUsingKeyFrames.cs
- StyleTypedPropertyAttribute.cs
- DataGridViewSelectedRowCollection.cs
- CodeParameterDeclarationExpressionCollection.cs
- ProfileParameter.cs
- ResourceReferenceExpression.cs
- TypeInitializationException.cs
- HttpServerVarsCollection.cs
- ThousandthOfEmRealDoubles.cs
- Transform.cs
- ImageClickEventArgs.cs
- SchemaDeclBase.cs
- LabelAutomationPeer.cs
- DesigntimeLicenseContext.cs
- TextControlDesigner.cs
- ClientRoleProvider.cs
- errorpatternmatcher.cs
- Literal.cs
- ArraySegment.cs
- ICspAsymmetricAlgorithm.cs
- MiniParameterInfo.cs
- ThreadSafeList.cs
- ListViewCancelEventArgs.cs
- MetadataArtifactLoader.cs
- ProcessModuleCollection.cs
- EventArgs.cs
- XmlHierarchyData.cs
- RawAppCommandInputReport.cs
- RegexTree.cs
- MimeBasePart.cs
- CapabilitiesPattern.cs
- QueryCacheManager.cs
- XmlJsonReader.cs
- ComPlusServiceHost.cs
- StringValidatorAttribute.cs
- TaiwanCalendar.cs
- DataGridItemAutomationPeer.cs
- PaperSource.cs
- sqlnorm.cs
- ListViewUpdatedEventArgs.cs
- PointIndependentAnimationStorage.cs
- InternalConfirm.cs