[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2427
  • Last Modified:

How do I compile this?

Hi,

I'm new to C++ but not new to programming.
I need to compile the following code at:

http://msdn.microsoft.com/library/en-us/seccrypto/security/example_c_program_using_cryptprotectdata.asp?frame=true

I have Visual Studio 2005 with C++. Could anyone please tell me how to compile the code in the site above?
When I try to compile I get errors.

The code above is just an example.
If you are able to get me a code with a working CryptProtectData function (see: http://msdn.microsoft.com/library/en-us/seccrypto/security/cryptprotectdata.asp) - Then that will do too, since that is what I need.

Thank you in advance,
0
JimmyJJ
Asked:
JimmyJJ
  • 4
  • 2
1 Solution
 
smidgie82Commented:
Hi JimmyJJ,
Can you post a copy of the compile-time errors you get?  That'll help out quite a bit.  It's likely not the code that's the problem, but rather the build environment.

0
 
AlexFMCommented:
I created C++ Win32 Console application in VC++ 2005 and pasted this code to it. I got only two errors:
Function exit is not defined - I added stdlib.h include
Unresolved external symbols - I added Crypt32.lib to the list of Linker dependencies.

Program is build correctly.
0
 
JimmyJJAuthor Commented:
Hi,

Ok well Im new to C++, so I simply open a new project in Visual Studio, then in the main cpp file I paste the complete source.

First error I get is: "fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?"

So I add the line "#include <stdafx.h>" to the source. Then it gives many errors:

Error      1      error C2065: 'DATA_BLOB' : undeclared identifier            18
Error      2      error C2146: syntax error : missing ';' before identifier 'DataIn'            18
Error      3      error C2065: 'DataIn' : undeclared identifier            18
Error      4      error C2146: syntax error : missing ';' before identifier 'DataOut'            19
Error      5      error C2065: 'DataOut' : undeclared identifier            19
Error      6      error C2146: syntax error : missing ';' before identifier 'DataVerify'            20
Error      7      error C2065: 'DataVerify' : undeclared identifier            20
Error      8      error C2065: 'BYTE' : undeclared identifier            21
Error      9      error C2065: 'pbDataInput' : undeclared identifier            21
Error      10      error C2059: syntax error : ')'            21
Error      11      error C2065: 'DWORD' : undeclared identifier            22
Error      12      error C2146: syntax error : missing ';' before identifier 'cbDataInput'            22
Error      13      error C2065: 'cbDataInput' : undeclared identifier            22
Error      14      error C3861: 'strlen': identifier not found            22
Error      15      error C2228: left of '.pbData' must have class/struct/union            23
Error      16      error C2228: left of '.cbData' must have class/struct/union            24
Error      17      error C2065: 'CRYPTPROTECT_PROMPTSTRUCT' : undeclared identifier            25
Error      18      error C2146: syntax error : missing ';' before identifier 'PromptStruct'            25
Error      19      error C2065: 'PromptStruct' : undeclared identifier            25
Error      20      error C2065: 'LPWSTR' : undeclared identifier            26
Error      21      error C2146: syntax error : missing ';' before identifier 'pDescrOut'            26
Error      22      error C2065: 'pDescrOut' : undeclared identifier            26
Error      23      error C2070: ''unknown-type'': illegal sizeof operand            36
Error      24      error C3861: 'ZeroMemory': identifier not found            36
Error      25      error C2228: left of '.cbSize' must have class/struct/union            37
Error      26      error C2070: ''unknown-type'': illegal sizeof operand            37
Error      27      error C2228: left of '.dwPromptFlags' must have class/struct/union            38
Error      28      error C2065: 'CRYPTPROTECT_PROMPT_ON_PROTECT' : undeclared identifier            38
Error      29      error C2228: left of '.szPrompt' must have class/struct/union            39
Error      30      error C3861: 'CryptProtectData': identifier not found            44
Error      31      error C3861: 'CryptUnprotectData': identifier not found            63
Error      32      error C2228: left of '.pbData' must have class/struct/union            72
Error      33      error C3861: 'LocalFree': identifier not found            87
Error      34      error C2228: left of '.pbData' must have class/struct/union            88
Error      35      error C3861: 'LocalFree': identifier not found            88
Error      36      error C2228: left of '.pbData' must have class/struct/union            89
Error      37      error C3861: 'LocalFree': identifier not found            89
Error      38      error C3861: 'GetLastError': identifier not found            103
Error      39      error C3861: 'exit': identifier not found            105
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AlexFMCommented:
Open Visual Studio and execute commands:
File - New - Project - Visual C++ - Win32 - Win32 Console Application. Fill application name, press OK twice. In the Class View open Global Functions, double-click in _tmain. Copy code from Microsoft page and paste it after line #include "stdafx.h". include also stdlib.h. Result should be:

#include "stdafx.h"


#include <stdio.h>
#include <stdlib.h>

#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(char *s);

void main()
{

    // Copyright (c) Microsoft Corporation.  All rights reserved.
    // Encrypt data from DATA_BLOB DataIn to DATA_BLOB DataOut.
    // Then decrypt to DATA_BLOB DataVerify.

    //-------------------------------------------------------------------
    // Declare and initialize variables.

    DATA_BLOB DataIn;
    DATA_BLOB DataOut;
    DATA_BLOB DataVerify;
    BYTE *pbDataInput =(BYTE *)"Hello world of data protection.";
    DWORD cbDataInput = strlen((char *)pbDataInput)+1;
    DataIn.pbData = pbDataInput;    
    DataIn.cbData = cbDataInput;
    CRYPTPROTECT_PROMPTSTRUCT PromptStruct;
    LPWSTR pDescrOut = NULL;

    //-------------------------------------------------------------------
    //  Begin processing.

    printf("The data to be encrypted is: %s\n",pbDataInput);

    //-------------------------------------------------------------------
    //  Initialize PromptStruct.

    ZeroMemory(&PromptStruct, sizeof(PromptStruct));
    PromptStruct.cbSize = sizeof(PromptStruct);
    PromptStruct.dwPromptFlags = CRYPTPROTECT_PROMPT_ON_PROTECT;
    PromptStruct.szPrompt = L"This is a user prompt.";

    //-------------------------------------------------------------------
    //  Begin protect phase.

    if(CryptProtectData(
        &DataIn,
        L"This is the description string.", // A description string.
        NULL,                               // Optional entropy
        // not used.
        NULL,                               // Reserved.
        &PromptStruct,                      // Pass a PromptStruct.
        0,
        &DataOut))
    {
        printf("The encryption phase worked. \n");
    }
    else
    {
        MyHandleError("Encryption error!");
    }
    //-------------------------------------------------------------------
    //   Begin unprotect phase.

    if (CryptUnprotectData(
        &DataOut,
        &pDescrOut,
        NULL,                 // Optional entropy
        NULL,                 // Reserved
        &PromptStruct,        // Optional PromptStruct
        0,
        &DataVerify))
    {
        printf("The decrypted data is: %s\n", DataVerify.pbData);
        printf("The description of the data was: %S\n",pDescrOut);
    }
    else
    {
        MyHandleError("Decryption error!");
    }
    //-------------------------------------------------------------------
    // At this point, memcmp could be used to compare DataIn.pbData and
    // DataVerify.pbDate for equality. If the two functions worked
    // correctly, the two byte strings are identical.

    //-------------------------------------------------------------------
    //  Clean up.

    LocalFree(pDescrOut);
    LocalFree(DataOut.pbData);
    LocalFree(DataVerify.pbData);
} // End of main

//-------------------------------------------------------------------
//  This example uses the function MyHandleError, a simple error
//  handling function, to print an error message to the  
//  standard error (stderr) file and exit the program.
//  For most applications, replace this function with one
//  that does more extensive error reporting.

void MyHandleError(char *s)
{
    fprintf(stderr,"An error occurred in running the program. \n");
    fprintf(stderr,"%s\n",s);
    fprintf(stderr, "Error number %x.\n", GetLastError());
    fprintf(stderr, "Program terminating. \n");
    exit(1);
} // End of MyHandleError


Open Project - Properties - Configuration Properties - Linker - Input. Type Crypt32.lib in the Additional Dependencies edit box. Now build the project.
0
 
JimmyJJAuthor Commented:
Thank you very much.
0
 
JimmyJJAuthor Commented:
I have one more question,

where do I now find the compiled .exe file?
0
 
JimmyJJAuthor Commented:
Nevermind ;-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now