views:

89

answers:

4

I have two classes, TProvider and TEncrypt. The calling application will talk to the TProvider class. The calling application will call Initialise first to obtain the handle mhProvider. I require access to this handle later when i try to perform encryption, as the TEncrypt class donot have access to this handle mhProvider. How can i get access to this handle?

class TProvider
{
public:
    int Initialise();
    int Encrypt();
private:
    HCRYPTPROV mhProvider;
    TEncrypt* mpEncrypt;
};
    //------------------------------------
class TEncrypt
{
public:
    int Encryption();
private:
    int GenerateEncryptionKey();
    HCRYPTKEY mhKey;
};
    //------------------------------------
int TEncrypt::Encryption()
{
    vStatus = GenerateEncryptionKey();
    // will go on to perform encryption after obtaining the key
return(vStatus);
}
    //------------------------------------
int TEncrypt::GenerateEncryptionKey()
{
    BOOL bRet = CryptGenKey(mhProvider,
                            CALG_AES_256,
                            CRYPT_EXPORTABLE,
                            &mhKey);
}
+2  A: 

Either you pass the handle to TEncrypt via a (constructor/method) parameter, or you make it available via a global variable. I would prefer the former, as global variables make the code harder to understand, maintain and test.

Availability may also be indirect, e.g. you pass an object to TEncrypt::Encryption() which provides access to the handle via one of its public methods.

(of course you can also pass it through a file, DB, ... but let's keep the focus within the program.)

Update: an example

class TEncrypt
{
public:
    int Encrypt(HCRYPTPROV& mhProvider);
private:
    int GenerateEncryptionKey(HCRYPTPROV& mhProvider);
    HCRYPTKEY mhKey;
};
    //------------------------------------
int TEncrypt::Encrypt(HCRYPTPROV& mhProvider)
{
    vStatus = GenerateEncryptionKey(mhProvider);
    // will go on to perform encryption after obtaining the key
    return(vStatus);
}
    //------------------------------------
int TEncrypt::GenerateEncryptionKey(HCRYPTPROV& mhProvider)
{
    BOOL bRet = CryptGenKey(mhProvider,
                            CALG_AES_256,
                            CRYPT_EXPORTABLE,
                            &mhKey);
}

Note: I renamed TEncrypt::Encrypt because it is better to use verbs as method names rather than nouns.

Péter Török
@Peter, class TEncrypt { public: int Encryption(HCRYPTPROV private: int GenerateEncryptionKey(); HCRYPTKEY mhKey; }; Is this is what you are referring? Can you please show me your explnation as a snippet? Thanks
Raj
@Raj, yes, that would be the simplest solution. I did not provide an example because there can be so many variations, and without knowing more of the context it is impossible to choose. But I added one now.
Péter Török
A: 

Why not just pass it to TEncrypt's constructor or Encryption member function?

Marcelo Cantos
+2  A: 

Use a getter in the TProvider class, instead of Initialise to handle talk between classes:

HCRYPTPROV TProvider::get_hProvider() const
{
    return mhProvider;
}

You could have a look at the Mediator Pattern too.

wok
@Wok When you refer as getter, can you please explain me with a snippet? Thanks
Raj
A: 

If mhProvider is needed by TEncrypt, then why is it in the class TProvider? Somehow your classes don't look to be properly designed.

Chubsdad