Demo for Microsoft's Crypt Library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mihai Moga
    New Member
    • Feb 2011
    • 2

    Demo for Microsoft's Crypt Library

    In my spare time I have implemented a simple Visual C++ application to show the power of Microsoft Crypt Library. The following functions have been implemented and tested using MD5 checksum and RC4 encryption/decryption:
    • BOOL GetChecksumBuff er(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffe r, DWORD& dwOutputLength, LPBYTE lpszInputBuffer , DWORD dwInputLength);
    • BOOL GetChecksumStri ng(ALG_ID nAlgorithm, CString& strResult, CString strBuffer);
    • BOOL GetChecksumFile (ALG_ID nAlgorithm, CString& strResult, CString strPathName);
    • BOOL EncryptBuffer(A LG_ID nAlgorithm, LPBYTE lpszOutputBuffe r, DWORD& dwOutputLength, LPBYTE lpszInputBuffer , DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey);
    • BOOL EncryptFile(ALG _ID nAlgorithm, CString strOutputName, CString strInputName, LPBYTE lpszSecretKey, DWORD dwSecretKey);
    • BOOL DecryptBuffer(A LG_ID nAlgorithm, LPBYTE lpszOutputBuffe r, DWORD& dwOutputLength, LPBYTE lpszInputBuffer , DWORD dwInputLength, LPBYTE lpszSecretKey, DWORD dwSecretKey);
    • BOOL DecryptFile(ALG _ID nAlgorithm, CString strOutputName, CString strInputName, LPBYTE lpszSecretKey, DWORD dwSecretKey);

    The demo application is using the computer's name as secret key for encryption/decryption. Target OS: 32bit Windows 2000, XP, Vista, 7. License: GNU General Public License version 3 (GPLv3).

    Here is sample usage of the program
    Code:
    CString strBuffer1 = _T("abc"), strResult1;
    VERIFY(GetChecksumString(CALG_MD5, strResult1, strBuffer1));
    TRACE(_T("MD5(%s) => %s\n"), strBuffer1, strResult1);
    
    CString strBuffer2 = _T("abc"), strResult2;
    VERIFY(GetChecksumFile(CALG_SHA1, strResult2, _T("D:\\AddressBook.csv")));
    TRACE(_T("SHA1(%s) => %s\n"), strBuffer2, strResult2);
    
    CString strSecretKey = GetComputerID();
    LPBYTE lpszSecretKey = (LPBYTE)(LPCTSTR)strSecretKey;
    DWORD dwSecretKey = (strSecretKey.GetLength() + 1) * sizeof(TCHAR);
    CString strFilename = _T("D:\\AddressBook.csv");
    CString strEncrypt = _T("D:\\AddressBook.rc4");
    CString strDecrypt = _T("D:\\AddressBook.txt");
    VERIFY(EncryptFile(CALG_RC4, strEncrypt, strFilename, lpszSecretKey, dwSecretKey));
    VERIFY(DecryptFile(CALG_RC4, strDecrypt, strEncrypt, lpszSecretKey, dwSecretKey));
    Attached Files
    Last edited by Niheel; Jul 21 '11, 07:24 PM.
  • Mihai Moga
    New Member
    • Feb 2011
    • 2

    #2
    How to read decrypted value from registry:
    Code:
    CString ReadCryptRegistry(HKEY hRoot, CString strPath, CString strName, CString strDefaultValue)
    {
        CString strValue = strDefaultValue;
        HKEY hKey = NULL;
        DWORD dwResult = 0;
        DWORD dwType = 0;
    
        BYTE lpszSecretKey[0x1000] = { 0 };
        _tcscpy_s((LPTSTR) lpszSecretKey, sizeof(lpszSecretKey) / sizeof(TCHAR), GetComputerID());
        DWORD dwSecretKey = (_tcslen((LPTSTR) lpszSecretKey) + 1) * sizeof(TCHAR);
    
        BYTE lpszDataBuffer[0x1000] = { 0 };
        DWORD dwDataLength = 0x1000;
    
        BYTE lpszTempBuffer[0x1000] = { 0 };
        DWORD dwTempLength = 0x1000;
    
        if (RegCreateKeyEx(hRoot, strPath, 0, NULL, REG_OPTION_NON_VOLATILE,
            KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &hKey, &dwResult) == ERROR_SUCCESS)
        {
            if (RegQueryValueEx(hKey, strName, NULL, &dwType, lpszTempBuffer, &dwTempLength) == ERROR_SUCCESS)
            {
                if (DecryptBuffer(CALG_RC4, lpszDataBuffer, dwDataLength, lpszTempBuffer, dwTempLength, lpszSecretKey, dwSecretKey))
                {
                    OutputDebugString(_T("Successfully read decrypted key from registry.\n"));
                    strValue = (LPCTSTR) lpszDataBuffer;
                }
            }
    
            VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS);
        }
    
        return strValue;
    }
    How to write encrypted value to registry:
    Code:
    CString WriteCryptRegistry(HKEY hRoot, CString strPath, CString strName, CString strValue)
    {
        HKEY hKey = NULL;
        DWORD dwResult = 0;
        DWORD dwType = 0;
    
        BYTE lpszSecretKey[0x1000] = { 0 };
        _tcscpy_s((LPTSTR) lpszSecretKey, sizeof(lpszSecretKey) / sizeof(TCHAR), GetComputerID());
        DWORD dwSecretKey = (_tcslen((LPTSTR) lpszSecretKey) + 1) * sizeof(TCHAR);
    
        BYTE lpszDataBuffer[0x1000] = { 0 };
        DWORD dwDataLength = 0x1000;
    
        BYTE lpszTempBuffer[0x1000] = { 0 };
        DWORD dwTempLength = (strValue.GetLength() + 1) * sizeof(TCHAR);
        ::CopyMemory(lpszTempBuffer, (LPCTSTR) strValue, dwTempLength);
    
        if (EncryptBuffer(CALG_RC4, lpszDataBuffer, dwDataLength, lpszTempBuffer, dwTempLength, lpszSecretKey, dwSecretKey))
        {
            if (RegCreateKeyEx(hRoot, strPath, 0, NULL, REG_OPTION_NON_VOLATILE,
                KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY, NULL, &hKey, &dwResult) == ERROR_SUCCESS)
            {
                if (RegSetValueEx(hKey, strName, 0, REG_BINARY, lpszDataBuffer, dwDataLength) == ERROR_SUCCESS)
                {
                    OutputDebugString(_T("Successfully written encrypted key to registry.\n"));
                }
    
                VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS);
            }
        }
    
        return strValue;
    }

    Comment

    Working...