// CgeepApiExample.cpp: implementation of the CCgeepApiExample class.
// Copyright (c) SafeLogic, 2000 - 2009
// Last Updates:
// 19 oct. 2009 20:42:14 Nicolas de Pomereu


#include "stdafx.h"
#include "CgeepApVC6.h"
#include "CgeepApiExample.h"

#include "CgeepApiExample.h"

#include "cgeepapi4x.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;

#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

const CString TEMP_DIR = "c:\\temp";

CCgeepApiExample::CCgeepApiExample()
{

	// 1) Load cGeep API Instance
	ICgeepApi cgeepApi;

	if (! cgeepApi.CreateDispatch("cgeepapi4x.CgeepApi")) 
	{

		
		AfxMessageBox("Dll cgeepapi4x.dll not loaded!", MB_ICONSTOP);
		return;
	}

    // 1) Set the keyring in c:\temp
    cgeepApi.setKeyRingDirectory(TEMP_DIR);

    // 2) Create two key pairs
    // (it will generate a private key ring & a public key ring in c:\\temp)
    
    // Create a key with test1@test.com as PGP UserId
    cgeepApi.generateKeyPair("test1@test.com", "passphrase",
            "RSA", 1024, "AES-256", "NEVER");

    if (!cgeepApi.isOperationOk())
    {
       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());

       AfxMessageBox("Exception: " + cgeepApi.getException());
        return;
    }

    // Create a key with test2@test.com as PGP UserId
    cgeepApi.generateKeyPair("test2@test.com", "passphrase",
            "RSA", 1024, "AES-256", "NEVER");

    if (!cgeepApi.isOperationOk())
    {
       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());

       AfxMessageBox("Exception: " + cgeepApi.getException());
        return;
    }
    
    // We display the key list

    CString list = cgeepApi.listKeys("");

    if(!cgeepApi.isOperationOk())
    {

       AfxMessageBox("Error: " + cgeepApi.getErrorCode());
       AfxMessageBox("Exception: " + cgeepApi.getException());

        return;
    }        
    
   AfxMessageBox(list);
    
    //
    // 3) Symmetric encryption & decryption
    //
    
    // encryption
    CString fileIn   = TEMP_DIR + "\\text.txt";

    CString fileOut  = TEMP_DIR + "\\text.txt.pgp"; // The ecnrypted file
    cgeepApi.encryptSymmetric(fileIn, fileOut, "paspshrase", 0);         
    
    if (!cgeepApi.isOperationOk())
    {

       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());
       AfxMessageBox("Exception: " + cgeepApi.getException());

        return;
    }
    
    // decryption
    CString fileEncrypted   = TEMP_DIR + "\\text.txt.pgp";

    CString fileDecrypted   = TEMP_DIR + "\\text_decrypted.txt"; // The decrypted file
    cgeepApi.decryptSymmetric(fileEncrypted, fileDecrypted, "paspshrase", 0);         
    
    if (!cgeepApi.isOperationOk())
    {

       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());
       AfxMessageBox("Exception: " + cgeepApi.getException());

        return;
    }        

    //
    // 4) Asymmetric encryption & decryption
    //        
    
    // We reset the recipients list (Cautious to be done before each encryption)
    cgeepApi.resetRecipientsKeys();
    
    // We add test1@test.com as first recipient
    cgeepApi.addRecipientKey("test1@test.com");

    
    // We add test2@test.com as second recipient
    cgeepApi.addRecipientKey("test2@test.com");
    
    fileIn   = TEMP_DIR + "\\text.txt";

    fileOut  = TEMP_DIR + "\\text.txt.pgp"; // The ecnrypted file    
    
    // Now we encrypt a file for these two keys / recipients
    cgeepApi.encrypt(fileIn, fileOut, 0);       
    
    if (!cgeepApi.isOperationOk())
    {

       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());
       AfxMessageBox("Exception: " + cgeepApi.getException());

        return;
    }     
    
    // Now, we try to decrypt the encrypted file.
    // We can decrypt with test1@test.com, our first private key:
    
    cgeepApi.decrypt(fileEncrypted, fileDecrypted, "test1@test.com", "paspshrase", 0); 
    
    if (!cgeepApi.isOperationOk())
    {

       AfxMessageBox("Error    : " + cgeepApi.getErrorCode());
       AfxMessageBox("Exception: " + cgeepApi.getException());

        return;
    }     
    
    //
    // 5) Symmetric encryption example using a Task Number for 
    //    asynchronous processing
    //    We assume the test.txt file is a "big" file. ;-)
    // 
    
    CString fileClear   = TEMP_DIR + "\\text.txt";

    CString fileEnc     = TEMP_DIR + "\\text.txt.pgp"; 
    
    // Task number is 1
    cgeepApi.encryptSymmetric(fileClear, 
                              fileEnc, 
                              "paspshrase", 
                              1); 
    
    // The previous line of code is executed in a separated 
    // thread/sub task ==> We a have an immediate release.

    
	char strPercentProgress[10];

    // Loop while encryption is working in background
    while (cgeepApi.getPercentProcessedForTask(1) < 100)
    {               
        Sleep(1000); // Sleep One second

        
        int percentProgress = cgeepApi.getPercentProcessedForTask(1);
		sprintf(strPercentProgress,"%d", percentProgress);

		CString progress;
		progress += "Progress";
		progress += strPercentProgress;

		progress += "%";

        AfxMessageBox(progress);            
    }
    
    // Task is done (or killed) if percentProgress = 100
    // We can now check the Error Code

    if (!cgeepApi.isOperationOkForTask(1))
    {
       AfxMessageBox("Error    : " 
                + cgeepApi.getErrorCodeForTask(1));

       AfxMessageBox("Exception: " 
                + cgeepApi.getExceptionForTask(1));
        return;
    }     
                    
    // We are done:

   AfxMessageBox("Done!");        


}

CCgeepApiExample::~CCgeepApiExample()
{

}