Click here to Skip to main content
11,716,938 members (90,208 online)
Click here to Skip to main content

Crypt Library Demo

, 28 Jul 2014 CPOL 5.8K 353 17
Rate this:
Please Sign up or sign in to vote.
Learn the basics of Microsoft CryptoAPI Library

Crypt Library Demo

Introduction

In simple terms, cryptography is the application of selected processes for encoding data so that the information can be stored and transmitted securely. The Microsoft CryptoAPI allows developers to build cryptographic security into their applications by providing a flexible set of functions to encrypt or digitally sign data. You can use cryptography to achieve many security requirements, including:

  • Ensuring secrecy by coding sensitive files so that an interloper cannot understand them;
  • Guaranteeing secure communications even though the transmission media is not secure;
  • Verifying the origin of messages and data using digital signatures.

The fundamental cryptographic operations supported by the CryptoAPI are encryption, decryption, and signing. Encryption is somewhat like controlled fragmentation: the data is there, but it’s scattered according to the encryption rules. Decryption is simply the inverse of encryption, where the encryption rules are reversed to reassemble the data. Digital signing is analogous to physically hand-signing a document, but with one significant improvement: it is very, very difficult to forge a digital signature. Here you can find more about this subject.

Using the Code

The main implementation is done in CryptographyExt.h and CryptographyExt.cpp files, as follows:

  • BOOL GetChecksumBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, LPBYTE lpszInputBuffer, DWORD dwInputLength); computes hash code for the given binary buffer, using the specified algorithm;
  • BOOL GetChecksumString(ALG_ID nAlgorithm, CString& strResult, CString strBuffer); computes hash code for the given CString, using the specified algorithm;
  • BOOL GetChecksumFile(ALG_ID nAlgorithm, CString& strResult, CString strPathName); computes hash code for the given file, using the specified algorithm;
  • BOOL EncryptBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, LPBYTE lpszInputBuffer, DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey); encrypt the given binary buffer, using the specified algorithm;
  • BOOL EncryptFile(ALG_ID nAlgorithm, CString strOutputName, CString strInputName, LPBYTE lpszSecretKey, DWORD dwSecretKey); encrypt the given file, using the specified algorithm;
  • BOOL DecryptBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, LPBYTE lpszInputBuffer, DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey); decrypt the given binary buffer, using the specified algorithm;
  • BOOL DecryptFile(ALG_ID nAlgorithm, CString strOutputName, CString strInputName, LPBYTE lpszSecretKey, DWORD dwSecretKey); decrypt the given file, using the specified algorithm;
  • Helper function for secret key: CString GetComputerID();
BOOL GetChecksumBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, 
DWORD& dwOutputLength, LPBYTE lpszInputBuffer, DWORD dwInputLength)
{
    BOOL retVal = FALSE;

    ASSERT(lpszOutputBuffer != NULL);
    ASSERT(dwOutputLength != 0);
    ASSERT(lpszInputBuffer != NULL);
    ASSERT(dwInputLength != 0);

    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;

    if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    {
        if (CryptCreateHash(hCryptProv, nAlgorithm, NULL, 0, &hCryptHash))
        {
            if (CryptHashData(hCryptHash, lpszInputBuffer, dwInputLength, 0))
            {
                if (CryptGetHashParam(hCryptHash, HP_HASHVAL, lpszOutputBuffer, &dwOutputLength, 0))
                {
                    retVal = TRUE;
                }
                else
                {
                    TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptGetHashParam"), GetLastError());
                }
            }
            else
            {
                TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptHashData"), GetLastError());
            }
            VERIFY(CryptDestroyHash(hCryptHash));
        }
        else
        {
            TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptCreateHash"), GetLastError());
        }
        VERIFY(CryptReleaseContext(hCryptProv, 0));
    }
    else
    {
        TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptAcquireContext"), GetLastError());
    }

    return retVal;
}

BOOL EncryptBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, 
LPBYTE lpszInputBuffer, DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey)
{
    BOOL retVal = FALSE;
    DWORD dwHowManyBytes = dwInputLength;

    ASSERT(lpszOutputBuffer != NULL);
    ASSERT(dwOutputLength != 0);
    ASSERT(lpszInputBuffer != NULL);
    ASSERT(dwInputLength != 0);
    ASSERT(lpszSecretKey != NULL);
    ASSERT(dwSecretKey != 0);

    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;
    HCRYPTKEY hCryptKey = NULL;

    ::CopyMemory(lpszOutputBuffer, lpszInputBuffer, dwHowManyBytes);

    if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    {
        if (CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash))
        {
            if (CryptHashData(hCryptHash, lpszSecretKey, dwSecretKey, 0))
            {
                if (CryptDeriveKey(hCryptProv, nAlgorithm, 
                    hCryptHash, CRYPT_EXPORTABLE, &hCryptKey))
                {
                    if (CryptEncrypt(hCryptKey, NULL, TRUE, 0, 
                        lpszOutputBuffer, &dwHowManyBytes, dwOutputLength))
                    {
                        dwOutputLength = dwHowManyBytes;
                        retVal = TRUE;
                    }
                    else
                    {
                        TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptEncrypt"), GetLastError());
                    }
                    VERIFY(CryptDestroyKey(hCryptKey));
                }
                else
                {
                    TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptDeriveKey"), GetLastError());
                }
            }
            else
            {
                TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptHashData"), GetLastError());
            }
            VERIFY(CryptDestroyHash(hCryptHash));
        }
        else
        {
            TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptCreateHash"), GetLastError());
        }
        VERIFY(CryptReleaseContext(hCryptProv, 0));
    }
    else
    {
        TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptAcquireContext"), GetLastError());
    }

    return retVal;
}

BOOL DecryptBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, 
LPBYTE lpszInputBuffer, DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey)
{
    BOOL retVal = FALSE;
    DWORD dwHowManyBytes = dwInputLength;

    ASSERT(lpszOutputBuffer != NULL);
    ASSERT(dwOutputLength != 0);
    ASSERT(lpszInputBuffer != NULL);
    ASSERT(dwInputLength != 0);
    ASSERT(lpszSecretKey != NULL);
    ASSERT(dwSecretKey != 0);

    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;
    HCRYPTKEY hCryptKey = NULL;

    ::CopyMemory(lpszOutputBuffer, lpszInputBuffer, dwHowManyBytes);

    if (CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
    {
        if (CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash))
        {
            if (CryptHashData(hCryptHash, lpszSecretKey, dwSecretKey, 0))
            {
                if (CryptDeriveKey(hCryptProv, nAlgorithm, 
                    hCryptHash, CRYPT_EXPORTABLE, &hCryptKey))
                {
                    if (CryptDecrypt(hCryptKey, NULL, TRUE, 0, lpszOutputBuffer, &dwHowManyBytes))
                    {
                        dwOutputLength = dwHowManyBytes;
                        retVal = TRUE;
                    }
                    else
                    {
                        TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptDecrypt"), GetLastError());
                    }
                    VERIFY(CryptDestroyKey(hCryptKey));
                }
                else
                {
                    TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptDeriveKey"), GetLastError());
                }
            }
            else
            {
                TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptHashData"), GetLastError());
            }
            VERIFY(CryptDestroyHash(hCryptHash));
        }
        else
        {
            TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptCreateHash"), GetLastError());
        }
        VERIFY(CryptReleaseContext(hCryptProv, 0));
    }
    else
    {
        TraceLastError(CRYPT_LIBRARY_NAME, _T("CryptAcquireContext"), GetLastError());
    }

    return retVal;
}

History

  • Version 1.00 (July 27th, 2014) - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mihai MOGA
Architect Printec Group Romania SRL
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (XHTML, CSS, XML, PHP/MySQL, JavaScript, Flash).

If you like my articles, please visit my website for more: http://www.mihaimoga.com/

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 28 Jul 2014
Article Copyright 2014 by Mihai MOGA
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid