Click here to Skip to main content
Click here to Skip to main content

Obtain the plain text session key using CryptoAPI

By , 18 Jul 2012
 

Introduction

Sometimes, extracting raw session keys is necessary, particularly when you need to implement cryptographic protocols. However, Microsoft Cryptographic Providers (Base and Enhanced) don't support this feature. CryptExportKey() and CryptImportKey() require a valid key handle to encrypt and decrypt the session key, respectively. MSDN shows a way of doing this using a exponent-of-one private key. This article shows a better way to perform the same process. This way is faster and easier.

It's ready to go, but you must set the following parameters at Project -> Settings (Visual Studio 6.0 ) :

  • Add in C++/Preprocessor definitions: _WIN32_WINNT=0x0500, _CRYPT32_(WIN2K) or _WIN32_WINNT=0x0400, _CRYPT32_(NT4)
  • And Link this library  crypt32.lib

Code Listing

#include <windows.h>
#include <wincrypt.h>


#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;
        }

        //---------------------------------------------------
        //Creating a session key. In this sample we'll use a 
        //3DES key with 168 bits

        if (!CryptGenKey(hProv,CALG_3DES,CRYPT_EXPORTABLE,
            &hSessionKey))
        {
            __leave;
        }

        //---------------------------------------------------
        //Getting a handle to the Exchange Key pair

        if (!CryptGetUserKey( hProv, AT_KEYEXCHANGE, 
            &hExchangeKeyPair))
        {
            __leave;
        }

        //--------------------------------------------------------
        //Encrypting the session key with the public key part
        //of the keypair. The first call gets the size necessary to 
        //hold the encrypted session key and the second exports it.

        if (!CryptExportKey( hSessionKey, hExchangeKeyPair, 
            SIMPLEBLOB, 0, NULL, &dwSize))
        {
            __leave;
        }

        pbExportedKeyBlob = new BYTE[dwSize];

        if (!CryptExportKey( hSessionKey, hExchangeKeyPair, SIMPLEBLOB, 
            0, pbExportedKeyBlob,  &dwSize))
        {
            __leave;
        }

        //--------------------------------------------------------
        //Let's  remove the first 12 bytes of Blob information 

        pbEncryptedKey  =  new BYTE [KEY_PAIR_SIZE];  

        for ( c = 0 ; c < KEY_PAIR_SIZE ; c++ )
        {
            pbEncryptedKey[c] =  pbExportedKeyBlob[c+12]; 
        }

        //--------------------------------------------------------
        //Now it's time to get the value of the session key, we'll
        //use the private part of the key pair. 

        if (!CryptDecrypt( hExchangeKeyPair,0, TRUE, 0,  
            pbEncryptedKey, &dwKeyMaterial))
        {
            __leave;
        }

        //-------------------------------------------------------
        //The pbKeyMaterial is the value of the key

        pbKeyMaterial = new BYTE[ SESSION_KEY_SIZE ];

        for ( c = 0; c <  SESSION_KEY_SIZE ; c++ )
        {
            pbKeyMaterial[c] = pbEncryptedKey[c];    
        }

        //-------------------------------------------------------
        //The key is in big-endian format, because that's the way
        //the encryption block is built.

    }
    __finally
    {
        if (pbKeyMaterial ) LocalFree(pbKeyMaterial );
        if (hSessionKey) CryptDestroyKey(hSessionKey);
        if (hExchangeKeyPair) CryptDestroyKey(hExchangeKeyPair);
        if (hProv) 
        {  
            CryptReleaseContext(hProv, 0);
        }

    }

} // End Main

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Raphael Amorim
Software Developer (Senior) Instituto Atlântico
Brazil Brazil
Member
Senior Software Developer Engineer @ Instituto Atlântico
 
Summary:
 
• Has Acted as Software Architect and Technical Leader in many projects
• Good knowledge of Windows Internals
• More than 9 years of software design and development experience in Visual C/C++.
• More than 8 years of experience in Security/Cryptography software design and programming.
• Good knowledge of .NET framework and C# language, also using ASP.NET.
• Good knowledge of Lua Scripting Language
• Good knowledge of Objective C and Cocoa.
• Good knowledge of Java language.
• Good knowledge of MacOSX/Linux programming in C++
• Good knowledge of Brew Framework for CDMA cell phone software development (using C)
• Some knowledge of Carbon and Qt application development C++.
 
Specialist in:
Visual C++® and C, MFC, ATL, COM, STL, ActiveX, Lua, Cocoa, Carbon and Qt application development, C# and Java (J2SE and J2ME), Brew Framework for CDMA, Linux Programming in C++, PocketPC (embedded C++ and .NET Compact Framework) , Device Driver Development , SQL Server 6.5/7/2000/2005, MySQL, IBM DB2 and Access Visual C++ 6.0, Visual Studio .NET (all versions), Eclipse, NetBeans, XCode, Qt Creator and Carbide. Microsoft Windows NT to 7, Windows CE 3.0/5.0/6.0, Mac OS X and Linux.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralHere is a link to some associated documentationmemberPaul Sanders (AlpineSoft)21 Oct '07 - 3:52 
Nice article, thank you. Microsoft do in fact document the format of their key blobs, but they've squirreled the documentation away under WinCE for some peculiar reason:
 
http://msdn2.microsoft.com/en-us/library/ms884374.aspx[^]
 

GeneralKudosmemberAshutosh Arya2 Oct '07 - 19:59 
So much simpler and more logical compared to the approach outlined in MSDN. The first question that came to my mind after reading the MSDN article was why we can't just decrypt the key using the private key? And you answered that well (I did not think about the 12 byte header). Thanks for the post. It will benefit a lot of people.
QuestionHow to use publickey of another usermemberchakra_r24 Mar '04 - 10:21 
Hi,

In your example you are using keypair of the current user. That will work only if the user has private key. What if i want to encrypt for another user. I can get public key and encrypt using cryptexportkey.
How do i get decrypted key
AnswerRe: How to use publickey of another usersussAnonymous24 Mar '04 - 15:26 
I got the answer. Smile | :)
Generalerror include wincrypt.hmemberLoLem23 Mar '04 - 23:18 
hello,
I am using Windows2000,VC6++. I setted all the information but couldn't include wincrypt.h.
This is:
: error C2146: syntax error : missing ';' before identifier 'HRESULT'
:fatal error C1004: unexpected end of file found
When I click in the error, I get:
typedef LONG HRESULT;
So, what should I do?? I don't undertand!!!!!!!!!Confused | :confused:
Thank you so much
GeneralRe: error include wincrypt.hmemberRaphael Amorim22 Apr '04 - 1:07 
Add in C++/Preprocessor definitions:
 
_WIN32_WINNT=0x0500, _CRYPT32_(WIN2K) or _WIN32_WINNT=0x0400, _CRYPT32_(NT4)
 
And Link this library crypt32.lib

 
Raphael Amorim Dantas Leite
VC++, Java and C# programmer. Win32 and PocketPC enviroments
GeneralThanks, Bug &amp; SuggestionsussC. Jayachandran20 Nov '03 - 21:57 
First of all, thanks for the code example, I've been looking for this.
 
Bug:
the last loop should be :
for ( c = 0; c < SESSION_KEY_SIZE ; c++)
not c--
 
Suggestion:
You could use CryptGenKey( hCryptProv, AT_KEYEXCHANGE, 0, &hExchangeKeyPair) instead of CryptGetUserKey. CryptGetUserKey fails for me(probably because I don't have a userkey).
 
JC.
GeneralRe: Thanks, Bug &amp; SuggestionmemberRaphael Amorim19 Apr '05 - 16:02 
Good Idea Wink | ;)
 
Raphael Amorim Dantas Leite
VC++, Java and C# programmer. Win32 and PocketPC enviroments
QuestionWhy CryptGetUserKey returned the same key every time called at one machine?memberzy_ball10 Nov '03 - 15:02 
Why CryptGetUserKey returned the same key every time called at one machine?
If so,all programs run at one machine will have the same RSA key exchange pairs? Then how to keep secret?
 
--I am a silent programmers.
AnswerRe: Why CryptGetUserKey returned the same key every time called at one machine?memberRaphael Amorim24 Nov '03 - 9:33 
Depends on your CryptAcquireContext call Smile | :)
 
Raphael Amorim Dantas Leite
VC++, Java and C# programmer. Win32 and PocketPC enviroments

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 18 Jul 2012
Article Copyright 2001 by Raphael Amorim
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid