CryptEncrypt and CryptDecrypt functions of CryptoApi problems
-
Hello: I am having following error while using CryptEncrypt and CryptDecrypt for public private key pair of Windows CryptoApi .I am using windowsXP as OS. The code of the class I am using is given below. I am having problem while using Encrypt and Decrypt functions of the class. Can anybody would tell me where is the error and how to remove it or could make the code right where error occurs. Thanks, Naeem //////////////////////////////Header File///////////////////////// #define _WIN32_WINNT 0x0400 #include #include #include // Helper class definition to generate and export Public/Private keys // for Asymmetric encryption. The semantics for usage are: // Call AcquireContext with a container name, call // GenerateKeyPair next and then call ExportPublicKey or // ExportPrivateKey. class CryptoHelper { private: HCRYPTPROV m_hCryptProv; HCRYPTKEY m_hSessionKey; BYTE *m_pPublicKey; BYTE *m_pPrivateKey; BYTE *m_pEncSessionKey; DWORD dwPrivateKeyLen; DWORD dwPublicKeyLen; DWORD dwEncSessionKeyLen; public: HCRYPTKEY m_hCryptKey; CryptoHelper(); ~CryptoHelper(); HRESULT AcquireContext(LPCTSTR wszContainerName); HRESULT GenerateKeyPair(); HRESULT ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize);; HRESULT ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize); void Encrypt(char **Input,char **Output,DWORD &dwLen); void Decrypt(char **Input,char **Output,DWORD &dwLen); }; //////////////////////////////The CPP File///////////////////////// #include "CryptoHelper.h" #include //#include "Cspdk.h" // The RSA public-key key exchange algorithm #define ENCRYPT_ALGORITHM CALG_RSA_KEYX // The high order WORD 0x0200 (decimal 512) // determines the key length in bits. #define KEYLENGTH 0x02000000 #define SESS_KEYLENGTH 0x00800000 //128 Bit //-------------------------------------------------------------------- // The constructor initializes the member variables // to NULL. //-------------------------------------------------------------------- CryptoHelper::CryptoHelper() { m_hCryptProv = NULL; m_hCryptKey = NULL; m_hSessionKey = NULL; m_pPublicKey=NULL; m_pPrivateKey=NULL; m_pEncSessionKey=NULL; dwPrivateKeyLen=0; dwPublicKeyLen=0; dwEncSessionKeyLen=0; } //-----------------------------------------------
-
Hello: I am having following error while using CryptEncrypt and CryptDecrypt for public private key pair of Windows CryptoApi .I am using windowsXP as OS. The code of the class I am using is given below. I am having problem while using Encrypt and Decrypt functions of the class. Can anybody would tell me where is the error and how to remove it or could make the code right where error occurs. Thanks, Naeem //////////////////////////////Header File///////////////////////// #define _WIN32_WINNT 0x0400 #include #include #include // Helper class definition to generate and export Public/Private keys // for Asymmetric encryption. The semantics for usage are: // Call AcquireContext with a container name, call // GenerateKeyPair next and then call ExportPublicKey or // ExportPrivateKey. class CryptoHelper { private: HCRYPTPROV m_hCryptProv; HCRYPTKEY m_hSessionKey; BYTE *m_pPublicKey; BYTE *m_pPrivateKey; BYTE *m_pEncSessionKey; DWORD dwPrivateKeyLen; DWORD dwPublicKeyLen; DWORD dwEncSessionKeyLen; public: HCRYPTKEY m_hCryptKey; CryptoHelper(); ~CryptoHelper(); HRESULT AcquireContext(LPCTSTR wszContainerName); HRESULT GenerateKeyPair(); HRESULT ExportPublicKey(BYTE **ppPublicKey, DWORD &cbKeySize);; HRESULT ExportPrivateKey(BYTE **ppPrivateKey, DWORD &cbKeySize); void Encrypt(char **Input,char **Output,DWORD &dwLen); void Decrypt(char **Input,char **Output,DWORD &dwLen); }; //////////////////////////////The CPP File///////////////////////// #include "CryptoHelper.h" #include //#include "Cspdk.h" // The RSA public-key key exchange algorithm #define ENCRYPT_ALGORITHM CALG_RSA_KEYX // The high order WORD 0x0200 (decimal 512) // determines the key length in bits. #define KEYLENGTH 0x02000000 #define SESS_KEYLENGTH 0x00800000 //128 Bit //-------------------------------------------------------------------- // The constructor initializes the member variables // to NULL. //-------------------------------------------------------------------- CryptoHelper::CryptoHelper() { m_hCryptProv = NULL; m_hCryptKey = NULL; m_hSessionKey = NULL; m_pPublicKey=NULL; m_pPrivateKey=NULL; m_pEncSessionKey=NULL; dwPrivateKeyLen=0; dwPublicKeyLen=0; dwEncSessionKeyLen=0; } //-----------------------------------------------