SOL9 2.0 Class: MessageEncryptor

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * MessageEncryptor.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2009/03/24
#pragma once

#include <sol/Object.h>
#include <wincrypt.h>

#include <sol/Bytes.h>

namespace SOL {

class MessageEncryptor :public Object {

private:

    //CRYPT_ENCRYPT_MESSAGE_PARA    encryptMessagePara;
    CRYPT_ENCRYPT_MESSAGE_PARA encryptMessagePara;

    DWORD encodingType;
     const char* encAlgorithm;

public:
    /**
     * Constrcutor
     * @param encType        Default encodingType(X509_ASN_ENCODING|PKCS_7_ASN_ENCODING).
     * @param objId            Default encoding algorithm (szOID_RSA_RC4)
     */
    MessageEncryptor(DWORD encType=(X509_ASN_ENCODING|PKCS_7_ASN_ENCODING),
        const char* objId=szOID_RSA_RC4)
    :encodingType(encType),
    encAlgorithm(objId)
    {
        memset(&encryptMessagePara, 0, sizeof(encryptMessagePara));
    }


public:
    /**
     * Encrypt a message parameter by using a pCert certificate passed 
     * by parameter by encAlgorihthm of this class,
     * and return an encrypted message to a parameter encrypteMessage
     */
    int encrypt(
        __in HCRYPTPROV hProv,
        __in PCCERT_CONTEXT pRecipCert, 

        __in Bytes& message,        //Message to be encrypted
        __out Bytes& encryptedMessage)    //Encrypted message
    {
        int rc = NO_ERROR;

        BYTE* encData = NULL;
        DWORD encDataSize = 0;
        if ((rc = encrypt(hProv, pRecipCert, message.getData(), message.getSize(),
            &encData, &encDataSize)) == NO_ERROR) {
            encryptedMessage.shallowCopy(encData, encDataSize);
        } 
        return rc;
    }

public:
    /**
     * Encrypt a data by a certifiate pCert and return a encData
     */
    int encrypt(
        __in HCRYPTPROV hProv,
        __in PCCERT_CONTEXT pRecipCert, 

        __in const BYTE* data,        //Message to be encrypted
        __in DWORD  dataSize,        //Byte size of the message

        __out BYTE** ppEncData,     //Encrypted message
        __out DWORD* pEncDataSize)     //Byte siz of encrypted message 
    {
        int rc = NO_ERROR;

        PCCERT_CONTEXT    recipients[1];
        recipients[0] = pRecipCert;

        CRYPT_ALGORITHM_IDENTIFIER algorithmID;
        memset(&algorithmID, 0, sizeof(algorithmID));
        algorithmID.pszObjId = (char*)this->encAlgorithm;    //szOID_RSA_RC4;


        memset(&encryptMessagePara, 0, sizeof(encryptMessagePara));
        encryptMessagePara.cbSize = sizeof(encryptMessagePara);
        encryptMessagePara.dwMsgEncodingType = this->encodingType;
        encryptMessagePara.hCryptProv = hProv;
        encryptMessagePara.ContentEncryptionAlgorithm = algorithmID;    

        DWORD bufferSize = 0;
        if (!CryptEncryptMessage(
                &encryptMessagePara,
                1,
                recipients,
                data,
                dataSize,
                NULL,
                &bufferSize)) {

            return GetLastError();
        }

        BYTE* buffer = new BYTE [bufferSize];

        if (CryptEncryptMessage(
                &encryptMessagePara,
                1,
                recipients,
                data,
                dataSize,
                buffer,
                &bufferSize)) {
            //OK
            *ppEncData = buffer;
            *pEncDataSize = bufferSize;
        } else {
            delete [] buffer;
            rc = GetLastError();
        }
        return rc;

    }
};

}

Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.