Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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 check for here is (a) we're in 3-key mode (192 bits), and // (b) either K1 == K2 or K2 == K3 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. // // ==--== // // 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 check for here is (a) we're in 3-key mode (192 bits), and // (b) either K1 == K2 or K2 == K3 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SimpleWebHandlerParser.cs
- XsltLoader.cs
- FlowLayout.cs
- URLMembershipCondition.cs
- BindableAttribute.cs
- KeyValuePairs.cs
- ZoneLinkButton.cs
- PathTooLongException.cs
- RtfToken.cs
- PerformanceCounterManager.cs
- UdpDiscoveryEndpointElement.cs
- BevelBitmapEffect.cs
- ResourcePool.cs
- ServiceBehaviorElementCollection.cs
- AsymmetricKeyExchangeDeformatter.cs
- SafeHandle.cs
- XmlWrappingWriter.cs
- ConstNode.cs
- MessageDecoder.cs
- Binding.cs
- Light.cs
- BindingWorker.cs
- SettingsPropertyValueCollection.cs
- WindowsImpersonationContext.cs
- WebResponse.cs
- WebPageTraceListener.cs
- Listen.cs
- TPLETWProvider.cs
- StreamHelper.cs
- RemotingException.cs
- JoinQueryOperator.cs
- WaitHandleCannotBeOpenedException.cs
- XsdDuration.cs
- VirtualizingPanel.cs
- TypeLibConverter.cs
- HtmlGenericControl.cs
- ResourceAttributes.cs
- SqlGenericUtil.cs
- AppearanceEditorPart.cs
- COSERVERINFO.cs
- ReadOnlyObservableCollection.cs
- MergeFilterQuery.cs
- PositiveTimeSpanValidatorAttribute.cs
- InplaceBitmapMetadataWriter.cs
- PeerCollaboration.cs
- StorageMappingItemLoader.cs
- WindowsServiceCredential.cs
- SqlRemoveConstantOrderBy.cs
- RuntimeResourceSet.cs
- SplitContainer.cs
- DesignerImageAdapter.cs
- XappLauncher.cs
- MSG.cs
- ExpressionBuilderContext.cs
- COM2PictureConverter.cs
- DisplayNameAttribute.cs
- handlecollector.cs
- StoreItemCollection.cs
- CompositeControl.cs
- LinkGrep.cs
- ConfigXmlAttribute.cs
- EmptyReadOnlyDictionaryInternal.cs
- DataGridRowAutomationPeer.cs
- SaveFileDialog.cs
- ModelServiceImpl.cs
- KnownTypesProvider.cs
- _ChunkParse.cs
- DivideByZeroException.cs
- Int16AnimationUsingKeyFrames.cs
- TemplatedAdorner.cs
- InstanceData.cs
- XmlDictionaryReader.cs
- MimeFormReflector.cs
- MachineKeySection.cs
- FamilyMap.cs
- CLSCompliantAttribute.cs
- TreeIterator.cs
- ServiceDescriptionData.cs
- HtmlTernaryTree.cs
- ScriptIgnoreAttribute.cs
- BindingCompleteEventArgs.cs
- LinearKeyFrames.cs
- RemoteCryptoTokenProvider.cs
- ToolboxBitmapAttribute.cs
- NameValueConfigurationElement.cs
- _ListenerRequestStream.cs
- FixedHighlight.cs
- MapPathBasedVirtualPathProvider.cs
- DataGridViewAdvancedBorderStyle.cs
- ProcessManager.cs
- ConfigXmlReader.cs
- MaterializeFromAtom.cs
- ObjectStateEntryDbUpdatableDataRecord.cs
- COM2ComponentEditor.cs
- IMembershipProvider.cs
- Authorization.cs
- AlternationConverter.cs
- ScriptControlDescriptor.cs
- WebPartChrome.cs
- Permission.cs