Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Security / Cryptography / TripleDES.cs / 1305376 / TripleDES.cs
using System.Diagnostics.Contracts; // ==++== // // 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"); Contract.EndContractBlock(); 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 // [System.Security.SecuritySafeCritical] // auto-generated new static public TripleDES Create() { return Create("System.Security.Cryptography.TripleDES"); } [System.Security.SecuritySafeCritical] // auto-generated 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")); Contract.EndContractBlock(); 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. using System.Diagnostics.Contracts; // ==++== // // 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"); Contract.EndContractBlock(); 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 // [System.Security.SecuritySafeCritical] // auto-generated new static public TripleDES Create() { return Create("System.Security.Cryptography.TripleDES"); } [System.Security.SecuritySafeCritical] // auto-generated 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")); Contract.EndContractBlock(); 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
- IIS7WorkerRequest.cs
- MarkedHighlightComponent.cs
- IFlowDocumentViewer.cs
- OdbcParameterCollection.cs
- RtfToken.cs
- InkCanvasSelection.cs
- ContainerUIElement3D.cs
- InkCanvasFeedbackAdorner.cs
- RunInstallerAttribute.cs
- ContractNamespaceAttribute.cs
- ScrollBar.cs
- M3DUtil.cs
- EvidenceBase.cs
- InkSerializer.cs
- WebBaseEventKeyComparer.cs
- Visitors.cs
- FileDialog.cs
- CheckBox.cs
- SafeNativeMethods.cs
- CorrelationManager.cs
- Formatter.cs
- _NegotiateClient.cs
- SessionKeyExpiredException.cs
- X500Name.cs
- ConstraintStruct.cs
- Style.cs
- ResourceAttributes.cs
- PreviewPrintController.cs
- FormsAuthenticationModule.cs
- IntSumAggregationOperator.cs
- XmlSerializationReader.cs
- PropertyGridEditorPart.cs
- DataSourceControlBuilder.cs
- DocumentXmlWriter.cs
- CaseExpr.cs
- HostProtectionException.cs
- LockCookie.cs
- EventProviderTraceListener.cs
- ExpressionVisitor.cs
- AppDomainGrammarProxy.cs
- ADMembershipUser.cs
- XslTransform.cs
- XamlClipboardData.cs
- DateTimeConverter2.cs
- SaveFileDialog.cs
- MailMessageEventArgs.cs
- Thumb.cs
- ItemTypeToolStripMenuItem.cs
- SqlDataReader.cs
- XamlTypeMapper.cs
- StaticContext.cs
- PerfCounters.cs
- SynchronizedRandom.cs
- FlagsAttribute.cs
- EntityDataSourceDataSelection.cs
- ImageBrush.cs
- Calendar.cs
- ListBoxItemAutomationPeer.cs
- DataGridViewButtonColumn.cs
- XPathNode.cs
- FunctionOverloadResolver.cs
- TemplateBaseAction.cs
- ListSurrogate.cs
- LoginCancelEventArgs.cs
- TreeView.cs
- FormatterConverter.cs
- GZipStream.cs
- SchemaAttDef.cs
- MessageHeaderInfoTraceRecord.cs
- MultiBinding.cs
- BulletedListDesigner.cs
- PrimaryKeyTypeConverter.cs
- TemplateBindingExtension.cs
- GridViewCancelEditEventArgs.cs
- TableCell.cs
- SqlBuffer.cs
- Schema.cs
- FacetDescriptionElement.cs
- PrimitiveType.cs
- ActivityExecutionContext.cs
- ViewCellSlot.cs
- RelatedPropertyManager.cs
- ADMembershipProvider.cs
- SHA512Managed.cs
- followingquery.cs
- SessionStateUtil.cs
- CodeDomSerializerException.cs
- MediaSystem.cs
- NamedObject.cs
- DefaultObjectMappingItemCollection.cs
- XmlNullResolver.cs
- PageVisual.cs
- HttpApplicationFactory.cs
- FaultDescriptionCollection.cs
- QuadraticBezierSegment.cs
- MediaContext.cs
- HwndProxyElementProvider.cs
- SizeChangedEventArgs.cs
- AnnouncementDispatcherAsyncResult.cs
- TextBoxView.cs