By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,020 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Demo for Microsoft's Crypt Library

Mihai Moga
P: 2
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 GetChecksumBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, DWORD& dwOutputLength, LPBYTE lpszInputBuffer, DWORD dwInputLength);
  • BOOL GetChecksumString(ALG_ID nAlgorithm, CString& strResult, CString strBuffer);
  • BOOL GetChecksumFile(ALG_ID nAlgorithm, CString& strResult, CString strPathName);
  • BOOL EncryptBuffer(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, 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(ALG_ID nAlgorithm, LPBYTE lpszOutputBuffer, 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
Expand|Select|Wrap|Line Numbers
  1. CString strBuffer1 = _T("abc"), strResult1;
  2. VERIFY(GetChecksumString(CALG_MD5, strResult1, strBuffer1));
  3. TRACE(_T("MD5(%s) => %s\n"), strBuffer1, strResult1);
  4.  
  5. CString strBuffer2 = _T("abc"), strResult2;
  6. VERIFY(GetChecksumFile(CALG_SHA1, strResult2, _T("D:\\AddressBook.csv")));
  7. TRACE(_T("SHA1(%s) => %s\n"), strBuffer2, strResult2);
  8.  
  9. CString strSecretKey = GetComputerID();
  10. LPBYTE lpszSecretKey = (LPBYTE)(LPCTSTR)strSecretKey;
  11. DWORD dwSecretKey = (strSecretKey.GetLength() + 1) * sizeof(TCHAR);
  12. CString strFilename = _T("D:\\AddressBook.csv");
  13. CString strEncrypt = _T("D:\\AddressBook.rc4");
  14. CString strDecrypt = _T("D:\\AddressBook.txt");
  15. VERIFY(EncryptFile(CALG_RC4, strEncrypt, strFilename, lpszSecretKey, dwSecretKey));
  16. VERIFY(DecryptFile(CALG_RC4, strDecrypt, strEncrypt, lpszSecretKey, dwSecretKey));
Attached Files
File Type: zip CryptLibraryDemo.zip (1.73 MB, 142 views)
Jul 17 '11 #1
Share this Article
Share on Google+
1 Comment


Mihai Moga
P: 2
How to read decrypted value from registry:
Expand|Select|Wrap|Line Numbers
  1. CString ReadCryptRegistry(HKEY hRoot, CString strPath, CString strName, CString strDefaultValue)
  2. {
  3.     CString strValue = strDefaultValue;
  4.     HKEY hKey = NULL;
  5.     DWORD dwResult = 0;
  6.     DWORD dwType = 0;
  7.  
  8.     BYTE lpszSecretKey[0x1000] = { 0 };
  9.     _tcscpy_s((LPTSTR) lpszSecretKey, sizeof(lpszSecretKey) / sizeof(TCHAR), GetComputerID());
  10.     DWORD dwSecretKey = (_tcslen((LPTSTR) lpszSecretKey) + 1) * sizeof(TCHAR);
  11.  
  12.     BYTE lpszDataBuffer[0x1000] = { 0 };
  13.     DWORD dwDataLength = 0x1000;
  14.  
  15.     BYTE lpszTempBuffer[0x1000] = { 0 };
  16.     DWORD dwTempLength = 0x1000;
  17.  
  18.     if (RegCreateKeyEx(hRoot, strPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  19.         KEY_QUERY_VALUE | KEY_SET_VALUE, NULL, &hKey, &dwResult) == ERROR_SUCCESS)
  20.     {
  21.         if (RegQueryValueEx(hKey, strName, NULL, &dwType, lpszTempBuffer, &dwTempLength) == ERROR_SUCCESS)
  22.         {
  23.             if (DecryptBuffer(CALG_RC4, lpszDataBuffer, dwDataLength, lpszTempBuffer, dwTempLength, lpszSecretKey, dwSecretKey))
  24.             {
  25.                 OutputDebugString(_T("Successfully read decrypted key from registry.\n"));
  26.                 strValue = (LPCTSTR) lpszDataBuffer;
  27.             }
  28.         }
  29.  
  30.         VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS);
  31.     }
  32.  
  33.     return strValue;
  34. }
How to write encrypted value to registry:
Expand|Select|Wrap|Line Numbers
  1. CString WriteCryptRegistry(HKEY hRoot, CString strPath, CString strName, CString strValue)
  2. {
  3.     HKEY hKey = NULL;
  4.     DWORD dwResult = 0;
  5.     DWORD dwType = 0;
  6.  
  7.     BYTE lpszSecretKey[0x1000] = { 0 };
  8.     _tcscpy_s((LPTSTR) lpszSecretKey, sizeof(lpszSecretKey) / sizeof(TCHAR), GetComputerID());
  9.     DWORD dwSecretKey = (_tcslen((LPTSTR) lpszSecretKey) + 1) * sizeof(TCHAR);
  10.  
  11.     BYTE lpszDataBuffer[0x1000] = { 0 };
  12.     DWORD dwDataLength = 0x1000;
  13.  
  14.     BYTE lpszTempBuffer[0x1000] = { 0 };
  15.     DWORD dwTempLength = (strValue.GetLength() + 1) * sizeof(TCHAR);
  16.     ::CopyMemory(lpszTempBuffer, (LPCTSTR) strValue, dwTempLength);
  17.  
  18.     if (EncryptBuffer(CALG_RC4, lpszDataBuffer, dwDataLength, lpszTempBuffer, dwTempLength, lpszSecretKey, dwSecretKey))
  19.     {
  20.         if (RegCreateKeyEx(hRoot, strPath, 0, NULL, REG_OPTION_NON_VOLATILE,
  21.             KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY, NULL, &hKey, &dwResult) == ERROR_SUCCESS)
  22.         {
  23.             if (RegSetValueEx(hKey, strName, 0, REG_BINARY, lpszDataBuffer, dwDataLength) == ERROR_SUCCESS)
  24.             {
  25.                 OutputDebugString(_T("Successfully written encrypted key to registry.\n"));
  26.             }
  27.  
  28.             VERIFY(RegCloseKey(hKey) == ERROR_SUCCESS);
  29.         }
  30.     }
  31.  
  32.     return strValue;
  33. }
Jan 19 '12 #2