Link to home
Start Free TrialLog in
Avatar of hp746
hp746

asked on

CALG_AES_256 vc++ 6.0

I am ahving errors  
C:\Documents and Settings\hariere\Desktop\testcrypto\testcrypto.cpp(67) : error C2065: 'PROV_RSA_AES' : undeclared identifier
C:\Documents and Settings\hariere\Desktop\testcrypto\testcrypto.cpp(101) : error C2065: 'CALG_AES_256' : undeclared identifier
Error executing cl.exe.
Please help where i am worng
// testcrypto.cpp : Defines the entry point for the console application.
//
 
 
#include "stdafx.h"
#include <windows.h>
#include<string.h>
#include <cstring>
 
#include <stdio.h>
#include <stdlib.h>
#include <wincrypt.h>
#include<malloc.h>
 
#ifdef USE_BLOCK_CIPHER
    // defines for RC2 block cipher
    #define ENCRYPT_ALGORITHM      CALG_RC2
    #define ENCRYPT_BLOCK_SIZE      8
#else
    // defines for RC4 stream cipher
    #define ENCRYPT_ALGORITHM      CALG_RC4
    #define ENCRYPT_BLOCK_SIZE      1
#endif
#ifndef MS_ENH_RSA_AES_PROV
#ifdef UNICODE
#define MS_ENH_RSA_AES_PROV \
L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#else
#define MS_ENH_RSA_AES_PROV \
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#endif
#endif /* MS_ENH_RSA_AES_PROV */
#ifndef CALG_HMAC
#define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
#endif
#ifdef CONFIG_TLS_INTERNAL
#ifdef __MINGW32_VERSION
#define PROV_RSA_AES  "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#endif
#endif /* MS_ENH_RSA_AES_PROV */
#ifndef CALG_HMAC
#define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
#endif
 
 
 
 
	DWORD dwResult;
	HCRYPTPROV hProv;
	HCRYPTKEY hKey;
	HCRYPTKEY hSessionKey;
	DWORD cbBlob;
	BYTE *pbBlob;
	HCRYPTHASH hHash;
 
	//CString	m_cipher;
void Encrypt();
int main()
{
 
	
	if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0))
				{
						dwResult = GetLastError();
					if (dwResult == NTE_BAD_KEYSET)
						{
								if (!CryptAcquireContext(&hProv, 
									NULL, MS_DEF_PROV, PROV_RSA_AES, 
										CRYPT_NEWKEYSET))
									{
										printf("error creating cryptoAcquireContext");
									}
						}
    
 
				}
 
			if(CryptCreateHash(
									hProv, 
									 CALG_MD5, 
										0, 
										0, 
									 &hHash)) 
						{
							printf("An empty hash object has been created. \n");
						}
				else
			{
						printf("Error during CryptBeginHash!\n");
					exit(1);
			}
 
		
	
						if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey))
								{
			
									printf("error creating CryptImportKey");
								}
		
 
					if (!CryptGenKey(hProv, CALG_AES_256, CRYPT_EXPORTABLE, &hSessionKey))
								{
			
									printf("error creating CryptGenKey");
								}	
	
    //Encrypt();
	return 0;
}
 
 
/*void Encrypt()
{
    //unsigned long length = m_clear.GetLength() +1;
	unsigned long length = 10;
    unsigned char * cipherBlock  ; // = (unsigned char*)malloc(length);
    LPCSTR mm = "hari";
	char myname[5] = "hari";
    memcpy(&cipherBlock, &myname, 10 -1); 
 
    if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, mm, &length, 9))
    {
        printf("error creating CryptEncrypt");
    }
 
    //m_cipher = cipherBlock;
    //m_clear = "";
    //free(cipherBlock);
}*/

Open in new window

Avatar of migoEX
migoEX

"undeclared identifier" - are you missing some includes? "wincrypt.h"?
>>>> are you missing some includes? "wincrypt

migoEx is right. You need to add

#include <wincrypt.h>

below include "stdafx.h"  and recompile.

The missing constants were defined there.
Avatar of hp746

ASKER

i did tat i have received error in wincrypt.h

error C2146: syntax error : missing ';' before identifier 'HRESULT'
Avatar of hp746

ASKER

i paste some part of my wincrypt.h code am i using the old one?is it correct wincrypt.h?


//+---------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright 1992 - 1998 Microsoft Corporation.
//
//  File:       wincrypt.h
//
//  Contents:   Cryptographic API Prototypes and Definitions
//
//----------------------------------------------------------------------------

#ifndef __WINCRYPT_H__
#define __WINCRYPT_H__


#if(_WIN32_WINNT >= 0x0400)

#ifdef __cplusplus
extern "C" {
#endif

#ifndef _HRESULT_DEFINED
#define _HRESULT_DEFINED
typedef LONG HRESULT;

#endif // !_HRESULT_DEFINED

#if !defined(_CRYPT32_)
#define W
try adding
#include <windows.h>
before you include "wincrypt"
>>>> try adding
>>>> #include <windows.h>
>>>> before you include "wincrypt"

Hmmm. Isn't the windows.h already included in stdafx.h ? Note, a precompiled header like stdafx.h makes only sense if it does include some of the (rarely changing) big headers like windows.h and/or afx.h (MFC) respectively atlmfc.h (ATL and MFC since VC7). If not, you should remove it and switch off the Precompiled Header option which is in the C++ settings.

If your stdafx.h already includes windows.h your error  may occur because you need to rebuild the precompiled header. Either compile stdafx.cpp for that or use 'rebuild all'.

If the error still exists you may have to set the _WIN32_WINNT preprocessor macro. Go to menu Project - Properties - Configuration Properties - C++ -Preprocessor  and add  _WIN32_WINNT=0x0500 to the list of preprocessor macros (use a semicolon ; as separator).
Avatar of hp746

ASKER

i did it still i have same problem btw i am using visual c++ 6.0 console application
and #include <windows.h> in stadfx.h

 
Avatar of hp746

ASKER

I wonder if vc++ 6.0 is support 'CALG_AES_256'  alogrithms
>>>> I wonder if vc++ 6.0 is support 'CALG_AES_256'  alogrithms

You are right. wincrypt.h supplied with VC6 doesn't offer the CALG_AES_256.


But you could try to add the missing definitions below wyncrypt.h. If the advapi32.dll on the target system is younger then 1998 (check your Windows\system32) it probably supports additional codes.

Try to put

#define ALG_SID_TLS1PRF                 10
#define ALG_SID_HASH_REPLACE_OWF        11
#define ALG_SID_SHA_256                 12
#define ALG_SID_SHA_384                 13
#define ALG_SID_SHA_512                 14

#define CALG_AES_128            (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_128)
#define CALG_AES_192            (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_192)
#define CALG_AES_256            (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)
#define CALG_AES                (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES)

below #include <wincrypt.h>




It was the wrong ALG_SID... codes take

#define ALG_SID_AES_128                 14
#define ALG_SID_AES_192                 15
#define ALG_SID_AES_256                 16
#define ALG_SID_AES                     17


instead
Avatar of jkr
Defining these constants manually is not the usual way to go - instead, update your SDK from http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=e6e1c3df-a74f-4207-8586-711ebe331cdc (Don't get confused by the name, that only representy the latest product that SDK complies with)
Avatar of hp746

ASKER

jkr am using vc++ 6.0  am not developingin .net
Yes, I am aware of that. Yet that's where you update your native C++ headers and libraries also.
>>>> Defining these constants manually is not the usual way to go

No problem if it works. It is no code but only constants which were fully supported by any advapi32.dll younger than 2003.


>>>> update your SDK from
No, don't make updates which could spoil VC6. With VC7 MS made a major step towards .NET. That step is not compatible with VC6.

Defining the constants is a harmless thing. It either works or fails but won't make things worse. The SDK would bring an update which was made for later versions of C++ compiler and will change your VC6 environment. It is like to crack a nut with a sledgehammer and I highly recommend against it as long as you want to keep with VC6.
Avatar of hp746

ASKER

I have changed my code and i have few errors
// testcrypto.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"


#include<string.h>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <wincrypt.h>
#define ALG_SID_AES_128                 14
#define ALG_SID_AES_192                 15
#define ALG_SID_AES_256                 16
#define ALG_SID_AES                     17


#define CALG_AES_128                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_128)
#define CALG_AES_192                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_192)
#define CALG_AES_256                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)
#define CALG_AES                       (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES)
#ifndef MS_ENH_RSA_AES_PROV
#ifdef UNICODE
#define MS_ENH_RSA_AES_PROV \
L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#else
#define MS_ENH_RSA_AES_PROV \
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#endif
#endif /* MS_ENH_RSA_AES_PROV */

#ifndef CALG_HMAC
#define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
#endif

#define PROV_RSA_AES                           "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"

      


      DWORD dwResult;
      HCRYPTPROV hProv;
      HCRYPTKEY hKey;
      HCRYPTKEY hSessionKey;
      DWORD cbBlob;
      BYTE *pbBlob;
      HCRYPTHASH hHash;

      //CString      m_cipher;
void Encrypt();
int main()
{

      
      if (!CryptAcquireContext(&hProv, NULL, MS_ENH_RSA_AES_PROV" (Prototype)", PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
                        {
                                    dwResult = GetLastError();
                              if (dwResult == NTE_BAD_KEYSET)
                                    {
                                                if (!CryptAcquireContext(&hProv,
                                                      NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
                                                            CRYPT_NEWKEYSET))
                                                      {
                                                            printf("error creating cryptoAcquireContext");
                                                      }
                                    }
   

                        }

                  if(CryptCreateHash(
                                                      hProv,
                                                       CALG_MD5,
                                                            0,
                                                            0,
                                                       &hHash))
                                    {
                                          printf("An empty hash object has been created. \n");
                                    }
                        else
                  {
                                    printf("Error during CryptBeginHash!\n");
                              exit(1);
                  }

            
      
                                    if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey))
                                                {
                  
                                                      printf("error creating CryptImportKey");
                                                }
            

                              if (!CryptGenKey(hProv, CALG_AES_256, CRYPT_EXPORTABLE, &hSessionKey))
                                                {
                  
                                                      printf("error creating CryptGenKey");
                                                }      
      
    //Encrypt();
      return 0;
}


/*void Encrypt()
{
    //unsigned long length = m_clear.GetLength() +1;
      unsigned long length = 10;
    unsigned char * cipherBlock  ; // = (unsigned char*)malloc(length);
    LPCSTR mm = "hari";
      char myname[5] = "hari";
    memcpy(&cipherBlock, &myname, 10 -1);

    if (!CryptEncrypt(hSessionKey, 0, TRUE, 0, mm, &length, 9))
    {
        printf("error creating CryptEncrypt");
    }

    //m_cipher = cipherBlock;
    //m_clear = "";
    //free(cipherBlock);
}*/



Errors are

Deleting intermediate files and output files for project 'testcrypto - Win32 Debug'.
--------------------Configuration: testcrypto - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
testcrypto.cpp
C:\Documents and Settings\hariere\Desktop\testcrypto\testcrypto.cpp(57) : error C2664: 'CryptAcquireContextA' : cannot convert parameter 4 from 'char [66]' to 'unsigned long'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Documents and Settings\hariere\Desktop\testcrypto\testcrypto.cpp(64) : error C2664: 'CryptAcquireContextA' : cannot convert parameter 4 from 'char [66]' to 'unsigned long'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

testcrypto.exe - 2 error(s), 0 warning(s)
Avatar of hp746

ASKER

if (!CryptAcquireContext(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
the error is at the fourth parameter
>>>>>error C2664: 'CryptAcquireContextA' : cannot convert parameter 4 from 'char [66]' to 'unsigned long'

It is the PROV_RSA_AES which you defined at the top

      #define PROV_RSA_AES  "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"

as a string while the CryptAcquireContext expects an unsigned long.

The provider is the third argument while the fourth is the provider type whchis rarely is a string. You probably have to exchange parameter 3 and 4.




Avatar of hp746

ASKER

what values i can exchange?
I would try to exchange MS_ENH_RSA_AES_PROV and PROV_RSA_AES cause the first is a ULONG and the second a LPCSTR as required from function.
Avatar of hp746

ASKER

i have exchanged parameters but it did not work
>>>> MS_ENH_RSA_AES_PROV and PROV_RSA_AES

where did you get these parameters from?

>>>  have exchanged parameters but it did not work

Does it compile? If yes, does it call the function and what error (return) did you get?

Check both advapi32.dll and wincrypt.dll in system32 folder and tell me which create time they have.

Check all folders of your machine for elder versions of these dlls so that not the (old) ones coming with VC6 come to use.
Avatar of hp746

ASKER

advapi32.dll   =  Wednesday, August 04, 2004, 5:00:00 AM
and i have not seen wincrypt.dll  in my system32 directory.
i am using wincrypt.h header file in my program

>>>> and i have not seen wincrypt.dll  in my system32 directory.

it's crypt32.dll

>>>> advapi32.dll   =  Wednesday, August 04, 2004, 5:00:00 AM

young enough.

Could you get any error return when trying it?

And where are the constants MS_ENH_RSA_AES_PROV and PROV_RSA_AES from? Where did you know them?
Avatar of hp746

ASKER

crypt32.dll = Wednesday, August 04, 2004, 5:00:00 AM


>>>>the constants MS_ENH_RSA_AES_PROV and PROV_RSA_AES
I have defind in my program

#define ALG_SID_AES_128                 14
#define ALG_SID_AES_192                 15
#define ALG_SID_AES_256                 16
#define ALG_SID_AES                     17


#define CALG_AES_128                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_128)
#define CALG_AES_192                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_192)
#define CALG_AES_256                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)
#define CALG_AES                       (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES)
#ifndef MS_ENH_RSA_AES_PROV
#ifdef UNICODE
#define MS_ENH_RSA_AES_PROV \
L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#else
#define MS_ENH_RSA_AES_PROV \
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#endif
>>>>the constants MS_ENH_RSA_AES_PROV and PROV_RSA_AES
>>> I have defind in my program

Yes, but where do you know from that they were the right ones?

And what is the error you can retrieve by calling GetLastError()?
Avatar of hp746

ASKER

This i have get on online i am  not if they are the right ones for the vc++ 6.0
I have to do encrypt/decrypt using CALG_AES_256  algorithim using VC++ 6.0
I am not sure if it is support vc++ 6.0
if it support please let me know how can i apporach to solve the problem or if you have any example in vc++ 6.0 using CALG_AES_256 that would be great
>>>> I am not sure if it is support vc++ 6.0

It definitively isn't supported by VC 6.0 (as proofed by the missing constant definitions). But there has been a chance that newer advapi32.dll and crypt32.dll could provide the needed functionality.

But you didn't tell what errors you have though I asked a few times. So it is difficult to help.

Avatar of hp746

ASKER

here is the error





error C2664: 'CryptAcquireContextA' : cannot convert parameter 4 from 'char [66]' to 'unsigned long'


here is my code snippet


// testcrypto.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"


#include<string.h>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include <wincrypt.h>
#define ALG_SID_AES_128                 14
#define ALG_SID_AES_192                 15
#define ALG_SID_AES_256                 16
#define ALG_SID_AES                     17


#define CALG_AES_128                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_128)
#define CALG_AES_192                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_192)
#define CALG_AES_256                   (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES_256)
#define CALG_AES                       (ALG_CLASS_DATA_ENCRYPT|ALG_TYPE_BLOCK|ALG_SID_AES)
#ifndef MS_ENH_RSA_AES_PROV
#ifdef UNICODE
#define MS_ENH_RSA_AES_PROV \
L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#else
#define MS_ENH_RSA_AES_PROV \
"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
#endif
#endif /* MS_ENH_RSA_AES_PROV */

#ifndef CALG_HMAC
#define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
#endif

#define PROV_RSA_AES                           "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"

      


      DWORD dwResult;
      HCRYPTPROV hProv;
      HCRYPTKEY hKey;
      HCRYPTKEY hSessionKey;
      DWORD cbBlob;
      BYTE *pbBlob;
      HCRYPTHASH hHash;

      //CString      m_cipher;
void Encrypt();
int main()
{

      
      if (!CryptAcquireContext(&hProv, NULL,  MS_ENH_RSA_AES_PROV ,PROV_RSA_AES,CRYPT_VERIFYCONTEXT))
                        {
                                    
            
                              dwResult = GetLastError();
                              if (dwResult == NTE_BAD_KEYSET)
                                    {
                                                if (!CryptAcquireContext(&hProv,
                                                      NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
                                                            CRYPT_NEWKEYSET))
                                                      {
                                                            printf("error creating cryptoAcquireContext");
                                                      }
                                    }
   

                        }

                  if(CryptCreateHash(
                                                      hProv,
                                                       CALG_AES_256,
                                                            0,
                                                            0,
                                                       &hHash))
                                    {
                                          printf("An empty hash object has been created. \n");
                                    }
                        else
                  {
                                    printf("Error during CryptBeginHash!\n");
                              exit(1);
                  }

            
      
                                    if (!CryptImportKey(hProv, pbBlob, cbBlob, 0, 0, &hSessionKey))
                                                {
                  
                                                      printf("error creating CryptImportKey");
                                                }
            

                              if (!CryptGenKey(hProv, CALG_AES_256, CRYPT_EXPORTABLE, &hSessionKey))
                                                {
                  
                                                      printf("error creating CryptGenKey");
                                                }      
      
    //Encrypt();
      return 0;
}



That is a compiler error, which doesn't let make any statements to the runtime behavior.


Here is the prototype of ContextAcquireNext

BOOL WINAPI CryptAcquireContext(
  HCRYPTPROV* phProv,
  LPCTSTR pszContainer,
  LPCTSTR pszProvider,
  DWORD dwProvType,
  DWORD dwFlags
);

You see, argument 2 and 3 are strings (long pointer to const T string, where T means single-byte - ANSI - if not a UNICODE project) while argument 4 is a DWORD (unsigned integer). But you were passing PROV_RSA_AES as 4th argument which is defined as a string.

#define PROV_RSA_AES   "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"

The 4th argument is the 'provider type' while you were passing the 'provider name'.

If you check the definition of MS_ENH_RSA_AES_PROV  which you were passing as 3rd argument, you see that it is the same string as for PROV_RSA_AES what obviously is false.

In my wincrypt.h I found

#define PROV_RSA_AES            24

You should check, whether your wincrypt.h already contains that definition. If yes, simply drop the above redefinition. If no, exchange the wrong definition of PROV_RSA_AES with the one above. You also could call

if (!CryptAcquireContext(&hProv, NULL,  MS_ENH_RSA_AES_PROV ,24,CRYPT_VERIFYCONTEXT))

though that is less recommended.
Avatar of hp746

ASKER

if(!CryptCreateHash(hProv,CALG_AES_256, 0, 0,  &hHash)) is failing

when i change to CALG_AES_128 i have not seen any issue .
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hp746

ASKER

hmm My Problem sloved  I Really thank you very much  "itsmeandnobodyelse" to lsove this problem
I thank you very much EE Community .