How to obtain a session key for plain text with the cryptographic API

Source: Internet
Author: User

Operating environment: VC6 SP5, Sp1,nt4 SP3.

It is important to get a session key in the usual programming. However, Microsoft's cryptographic Operations API (either Basic or enhanced) does not provide this functionality. CryptExportKey () and Cryptimportkey () each require a valid key handle to encrypt and decrypt the session key. MSDN shows a way to use a private key. But Microsoft's example in MSDN is quite a long one. The following method is not only faster and more efficient.

Before running this example, you need to set the following parameters in Project-> settings (Visual Studio 6.0):

1. Add C + + preprocessing definition: _win32_winnt=0x0500, _crypt32_ (Win2K) or _win32_winnt=0x0400, _crypt32_ (NT4)

2. Join the Library connection: Crypt32.lib

The example code is as follows:

#include


#include


#include


#define Key_pair_size dwSize-12


#define Session_key_size dwkeymaterial


void Main ()


{


 


Hcryptprov Hprov = 0;


Hcryptkey hexchangekeypair = 0;


Hcryptkey hsessionkey = 0;


BYTE *pbkeymaterial = NULL;


DWORD dwkeymaterial;


BYTE *pbexportedkeyblob = NULL;


BYTE *pbencryptedkey = NULL;


DWORD dwsize;


unsigned int c;


   


__try


  {


   


if (! CryptAcquireContext (&hprov,


"Container Name",


Ms_enhanced_prov,


Prov_rsa_full,


crypt_machine_keyset))


  {


__leave;


  }


 


  //---------------------------------------------------


//Create a session key. In this example we will use a 168-bit 3DES key.


if (! CryptGenKey (Hprov, Calg_3des,


crypt_exportable, &hsessionkey))


  {


__leave;


  }


  //---------------------------------------------------


//Get the handle of the exchange key pair


        


if (! Cryptgetuserkey (Hprov, At_keyexchange, &hexchangekeypair))


  {


__leave;


  }


  //--------------------------------------------------------


//The session key is encrypted with the public key part of the key pair


//First gets the necessary byte size for the encrypted session key


//And then output it.


    


if (! CryptExportKey (hSessionKey,


Hexchangekeypair,


Simpleblob,


0,


NULL,


&dwsize))


  {


__leave;


  }


Pbexportedkeyblob = new Byte[dwsize];


if (! CryptExportKey (hSessionKey,


Hexchangekeypair,


Simpleblob,


0,


Pbexportedkeyblob,


&dwsize))


  {


__leave;


  }


 


  //--------------------------------------------------------


//We delete the first 12 byte size blob information


    


Pbencryptedkey = new BYTE [key_pair_size];


    


for (c = 0; c < key_pair_size C + +)


  {


Pbencryptedkey[c] = pbexportedkeyblob[c+12];


  }


    


  //--------------------------------------------------------


//This is when we use the private key part of the key pair to get the value of the session key.


    


if (! CryptDecrypt (Hexchangekeypair, 0,


TRUE, 0,


Pbencryptedkey, &dwkeymaterial))


  {


__leave;


  }


    


  //-------------------------------------------------------

The value of the key is stored in the
//pbkeymaterial


    


pbkeymaterial = new byte[session_key_size];


    


for (c = 0; c < session_key_size C + +)


  {


Pbkeymaterial[c] = pbencryptedkey[c];


  }


 


  }


__finally


  {


if (pbkeymaterial) LocalFree (pbkeymaterial);


if (hsessionkey) Cryptdestroykey (hSessionKey);


if (hexchangekeypair) Cryptdestroykey (Hexchangekeypair);


if (Hprov)


   { 


CryptReleaseContext (Hprov, 0);


   }


 


  }


}//End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.