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
- CompilationPass2Task.cs
- SimpleColumnProvider.cs
- TextServicesLoader.cs
- CodeNamespace.cs
- MoveSizeWinEventHandler.cs
- SqlBulkCopy.cs
- SequenceNumber.cs
- SystemColors.cs
- MenuTracker.cs
- ClockGroup.cs
- Codec.cs
- TargetControlTypeAttribute.cs
- XmlSignatureManifest.cs
- HandlerMappingMemo.cs
- ThreadPool.cs
- Annotation.cs
- TextPointer.cs
- HtmlUtf8RawTextWriter.cs
- RequestSecurityToken.cs
- StringUtil.cs
- CopyCodeAction.cs
- TreeViewDesigner.cs
- OdbcConnectionHandle.cs
- TitleStyle.cs
- UpdateManifestForBrowserApplication.cs
- ReachPageContentSerializerAsync.cs
- Receive.cs
- DataColumnMappingCollection.cs
- CommandExpr.cs
- CounterSampleCalculator.cs
- WindowsMenu.cs
- MediaContext.cs
- DataGridViewSortCompareEventArgs.cs
- XmlReaderSettings.cs
- PaginationProgressEventArgs.cs
- XmlSerializerFactory.cs
- Point.cs
- DataIdProcessor.cs
- SimpleFileLog.cs
- Verify.cs
- HttpErrorTraceRecord.cs
- NumericUpDownAccelerationCollection.cs
- FieldNameLookup.cs
- webproxy.cs
- DeobfuscatingStream.cs
- RowParagraph.cs
- ThicknessAnimationUsingKeyFrames.cs
- BitmapSourceSafeMILHandle.cs
- ObjectDataSourceMethodEventArgs.cs
- FlowDocumentScrollViewerAutomationPeer.cs
- ConfigurationValidatorAttribute.cs
- SizeValueSerializer.cs
- XmlNamespaceDeclarationsAttribute.cs
- SyntaxCheck.cs
- XsltException.cs
- DetailsViewRow.cs
- DocumentPageHost.cs
- ScrollItemPatternIdentifiers.cs
- SqlException.cs
- PreviousTrackingServiceAttribute.cs
- IdentifierCollection.cs
- SharedUtils.cs
- Brush.cs
- _DigestClient.cs
- FileDialogCustomPlacesCollection.cs
- ISAPIWorkerRequest.cs
- Int16Animation.cs
- Error.cs
- ModifierKeysValueSerializer.cs
- ServicePointManagerElement.cs
- CleanUpVirtualizedItemEventArgs.cs
- AppDomainInfo.cs
- Attribute.cs
- TextTrailingWordEllipsis.cs
- Set.cs
- InlineUIContainer.cs
- ConvertersCollection.cs
- MatrixStack.cs
- SizeAnimationBase.cs
- NavigationEventArgs.cs
- Inflater.cs
- XmlQueryType.cs
- FileDataSourceCache.cs
- DirectoryInfo.cs
- QueryConverter.cs
- SafeNativeMethods.cs
- CfgRule.cs
- OdbcConnectionStringbuilder.cs
- DispatcherHooks.cs
- XmlCharCheckingReader.cs
- AudioFormatConverter.cs
- ScriptingWebServicesSectionGroup.cs
- Trace.cs
- FormsIdentity.cs
- HebrewCalendar.cs
- CheckBoxBaseAdapter.cs
- SystemSounds.cs
- ResetableIterator.cs
- ContextDataSource.cs
- TextFormatter.cs