EncryptRequest.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / EncryptRequest.cs / 1 / EncryptRequest.cs

                            //------------------------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{ 
   using System;
   using System.IO; 
   using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace; 
   using System.Security.Principal;
 
   using System.Diagnostics;
   //
   // Summary:
   // Manages an encryption request from the client against a specified cryptosession. 
   //
   class EncryptRequest : ClientRequest 
   { 
       //
       // The cryptosession id we are attaching to. 
       //
       int m_cryptoSession;

       // 
       // Whether the data is padded using OAEP.
       // 
       bool m_useOAEP; 

       // 
       // The cleartext data we wish to encrypt..
       //
       byte[] m_cleartext;
 
       //
       // The encrypted data 
       // 
       byte[] m_encrypted;
 
        //
        // Sumamry:
        // Construct an EncyrptRequest object
        // 
        // Arguments:
        //  callingProcess          - The process in which the caller originated. 
        //  callingIdentity         - The WindowsIdentity of the caller 
        //  rpcHandle               - The handle of the native RPC request
        //  inArgs                  - The stream to read input data from 
        //  outArgs                 - The stream to write output data to
        //
       public EncryptRequest( Process callingProcess, WindowsIdentity callingIdentity, IntPtr rpcHandle, Stream inArgs, Stream outArgs )
           : base( callingProcess, callingIdentity, rpcHandle, inArgs, outArgs ) 
       {
 
           IDT.TraceDebug( "Intiating an Encrypt request" ); 
           m_cryptoSession      = 0;
           m_useOAEP            = false; 
           m_cleartext          = null;
           m_encrypted          = null;
       }
 

       protected override void OnMarshalInArgs() 
       { 
           IDT.DebugAssert( null != InArgs, "null inargs" );
           BinaryReader reader = new InfoCardBinaryReader( InArgs ); 

           //
           // Reader should have data in the order:
           // crytposession ( int32 ) 
           // isOAEP        ( int32 )
           // cleartext len ( int32 ) 
           // cleartext bytes 
           //
 
           m_cryptoSession     = reader.ReadInt32();
           m_useOAEP           = reader.ReadBoolean();
           int count           = reader.ReadInt32();
           m_cleartext         = reader.ReadBytes( count ); 

           IDT.ThrowInvalidArgumentConditional( 0 == m_cryptoSession, "cryptoSession" ); 
           IDT.ThrowInvalidArgumentConditional( null == m_cleartext || 0 == m_cleartext.Length, "clearText" ); 
       }
 
       //
       // Summary:
       // Attach to the appropriate cryptosession and encrypt the data.
       // 
       protected override void OnProcess()
       { 
           IDT.DebugAssert( 0 != m_cryptoSession, "null crypto session" ); 
           IDT.DebugAssert( null != m_cleartext && 0 != m_cleartext.Length, "null cleartext data" );
           try 
           {
               AsymmetricCryptoSession session =
                   (AsymmetricCryptoSession) CryptoSession.Find( m_cryptoSession, CallerPid, RequestorIdentity.User );
 
               m_encrypted = session.Encrypt( m_useOAEP, m_cleartext );
           } 
           finally 
           {
               Array.Clear( m_cleartext, 0, m_cleartext.Length ); 
           }
       }

 
       protected override void OnMarshalOutArgs()
       { 
           IDT.DebugAssert( null != OutArgs, "Null out args" ); 
           IDT.DebugAssert( null != m_encrypted, "null encrypted buffer" );
 
           BinaryWriter writer = new BinaryWriter( OutArgs );

           Utility.SerializeBytes( writer, m_encrypted );
       } 

   } 
 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK