CryptoAPI获取3DES会话密钥值

转至:http://www.codeproject.com/KB/security/plaintextsessionkey.aspx

不太会C++的异常捕获,用这代码会报try的错误,保持原创的代码直接COPY过来

#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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值