Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Security / MachineKey.cs / 1305376 / MachineKey.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* * MachineKey * * Copyright (c) 2009 Microsoft Corporation */ namespace System.Web.Security { using System.Web.Configuration; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public enum MachineKeyProtection { All, Encryption, Validation } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static class MachineKey { ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) throw new ArgumentNullException("data"); ////////////////////////////////////////////////////////////////////// // Step 1: Get the MAC and add to the blob if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length); byte [] bAll = new byte[bHash.Length + data.Length]; Buffer.BlockCopy(data, 0, bAll, 0, data.Length); Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length); data = bAll; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////////// // Step 2: Encryption data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random); } ////////////////////////////////////////////////////////////////////// // Step 3: Covert the buffer to HEX string and return it return MachineKeySection.ByteArrayToHexString(data, 0); } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static byte [] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) throw new ArgumentNullException("encodedData"); if ((encodedData.Length % 2) != 0) throw new ArgumentException(null, "encodedData"); byte [] data = null; try { ////////////////////////////////////////////////////////////////////// // Step 1: Covert the HEX string to byte array data = MachineKeySection.HexStringToByteArray(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if (data == null || data.Length < 1) throw new ArgumentException(null, "encodedData"); if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////// // Step 2: Decrypt the data data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random); if (data == null) return null; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { ////////////////////////////////////////////////////////////////// // Step 3a: Remove the hash from the end of the data if (data.Length < MachineKeySection.HashSize) return null; byte [] originalData = data; data = new byte[originalData.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(originalData, 0, data, 0, data.Length); ////////////////////////////////////////////////////////////////// // Step 3b: Calculate the hash and make sure it matches byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length); if (bHash == null || bHash.Length != MachineKeySection.HashSize) return null; // Sizes don't match for(int iter=0; iter// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* * MachineKey * * Copyright (c) 2009 Microsoft Corporation */ namespace System.Web.Security { using System.Web.Configuration; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public enum MachineKeyProtection { All, Encryption, Validation } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static class MachineKey { ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static string Encode(byte[] data, MachineKeyProtection protectionOption) { if (data == null) throw new ArgumentNullException("data"); ////////////////////////////////////////////////////////////////////// // Step 1: Get the MAC and add to the blob if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length); byte [] bAll = new byte[bHash.Length + data.Length]; Buffer.BlockCopy(data, 0, bAll, 0, data.Length); Buffer.BlockCopy(bHash, 0, bAll, data.Length, bHash.Length); data = bAll; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////////// // Step 2: Encryption data = MachineKeySection.EncryptOrDecryptData(true, data, null, 0, data.Length, false, false, IVType.Random); } ////////////////////////////////////////////////////////////////////// // Step 3: Covert the buffer to HEX string and return it return MachineKeySection.ByteArrayToHexString(data, 0); } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// public static byte [] Decode(string encodedData, MachineKeyProtection protectionOption) { if (encodedData == null) throw new ArgumentNullException("encodedData"); if ((encodedData.Length % 2) != 0) throw new ArgumentException(null, "encodedData"); byte [] data = null; try { ////////////////////////////////////////////////////////////////////// // Step 1: Covert the HEX string to byte array data = MachineKeySection.HexStringToByteArray(encodedData); } catch { throw new ArgumentException(null, "encodedData"); } if (data == null || data.Length < 1) throw new ArgumentException(null, "encodedData"); if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Encryption) { ////////////////////////////////////////////////////////////////// // Step 2: Decrypt the data data = MachineKeySection.EncryptOrDecryptData(false, data, null, 0, data.Length, false, false, IVType.Random); if (data == null) return null; } if (protectionOption == MachineKeyProtection.All || protectionOption == MachineKeyProtection.Validation) { ////////////////////////////////////////////////////////////////// // Step 3a: Remove the hash from the end of the data if (data.Length < MachineKeySection.HashSize) return null; byte [] originalData = data; data = new byte[originalData.Length - MachineKeySection.HashSize]; Buffer.BlockCopy(originalData, 0, data, 0, data.Length); ////////////////////////////////////////////////////////////////// // Step 3b: Calculate the hash and make sure it matches byte [] bHash = MachineKeySection.HashData(data, null, 0, data.Length); if (bHash == null || bHash.Length != MachineKeySection.HashSize) return null; // Sizes don't match for(int iter=0; iter
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Number.cs
- TemplateKeyConverter.cs
- XmlArrayAttribute.cs
- IdnMapping.cs
- XmlnsPrefixAttribute.cs
- HuffCodec.cs
- PublisherIdentityPermission.cs
- DrawListViewItemEventArgs.cs
- CompiledQueryCacheEntry.cs
- ArrayConverter.cs
- EdmToObjectNamespaceMap.cs
- AuthorizationPolicyTypeElement.cs
- OfTypeExpression.cs
- LineVisual.cs
- DbException.cs
- GetPageNumberCompletedEventArgs.cs
- MouseDevice.cs
- ScriptReferenceEventArgs.cs
- StrongNameMembershipCondition.cs
- DataGridColumnCollection.cs
- MouseActionValueSerializer.cs
- ItemList.cs
- PageCatalogPart.cs
- SqlCharStream.cs
- BitmapEffectCollection.cs
- WmlValidationSummaryAdapter.cs
- TrayIconDesigner.cs
- DataServiceQueryProvider.cs
- ListViewTableCell.cs
- OutOfProcStateClientManager.cs
- MgmtResManager.cs
- ObfuscationAttribute.cs
- XdrBuilder.cs
- TypeCacheManager.cs
- CacheOutputQuery.cs
- TcpChannelHelper.cs
- DispatchWrapper.cs
- HandlerFactoryCache.cs
- TypeUtil.cs
- TextDecoration.cs
- StateMachineSubscription.cs
- DataGridViewCellMouseEventArgs.cs
- ImagingCache.cs
- PingOptions.cs
- TriggerActionCollection.cs
- OleDbTransaction.cs
- WebBrowserEvent.cs
- WebHttpSecurityModeHelper.cs
- Verify.cs
- StylusPointPropertyInfoDefaults.cs
- HttpPostedFile.cs
- BamlStream.cs
- ArcSegment.cs
- ObservableCollection.cs
- WindowsToolbarItemAsMenuItem.cs
- ContainerUtilities.cs
- StatusBar.cs
- ImageAttributes.cs
- EncoderBestFitFallback.cs
- DrawingAttributesDefaultValueFactory.cs
- ActivitiesCollection.cs
- Source.cs
- ContentDefinition.cs
- DropShadowBitmapEffect.cs
- PolicyManager.cs
- AuthenticatedStream.cs
- PropertyReferenceExtension.cs
- ItemChangedEventArgs.cs
- ExpressionBuilder.cs
- WindowsEditBoxRange.cs
- XsdValidatingReader.cs
- PerCallInstanceContextProvider.cs
- DrawingBrush.cs
- InfoCardSymmetricAlgorithm.cs
- NamedObject.cs
- TextSegment.cs
- EditCommandColumn.cs
- SimpleBitVector32.cs
- ValueChangedEventManager.cs
- Icon.cs
- WebPartEventArgs.cs
- DesignerWithHeader.cs
- ResourceSet.cs
- XmlSchemaParticle.cs
- GenericTextProperties.cs
- AmbientValueAttribute.cs
- FamilyMap.cs
- TextAnchor.cs
- HttpDebugHandler.cs
- Decoder.cs
- NamespaceCollection.cs
- EventEntry.cs
- View.cs
- LingerOption.cs
- SmtpTransport.cs
- Roles.cs
- ExtenderControl.cs
- AuthorizationContext.cs
- DependencyObjectType.cs
- ReceiveParametersContent.cs