473,394 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

Demo for Microsoft's Crypt Library

Mihai Moga
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, 213 views)
Jul 17 '11 #1
1 3764
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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Khai Lu | last post by:
I am doing development for a Symobl Portable Data Terminal and it requires me to use a very old version of Visual Studio (which I don't have). It seems to me that all I need are a couple library...
6
by: Steve | last post by:
What would happen if an Access97 database had a reference to Microsoft Office 10 library (but only used Access97 stuff) and it was run on a machine that only had Access97 and correspondingly only...
0
by: AllenF | last post by:
I have a library (named DSG) created in C# which other programs use to build Excel spreadsheeets. This library uses the office PIAs "Microsoft.Office.Interop.Excel.dll" and also references the...
1
by: JM | last post by:
I've installed the Microsoft MSDN Express Library 2005 Beta and it looks OK. How does one access this library? I can't seem to figure that one out! It has to be simple?
0
by: Mads Westen | last post by:
Hi, I'm trying to code a application that can create a new email in Outlook I have coded my project in VS 2003, but now I have upgraded to VS 2005. I can not build my project anymore, I get a...
4
by: siggi | last post by:
Hi all, newbie question: I'd like to try speech synthesis with PythonWin 2.5. Problem ****** according to several instructions, such as found on...
72
by: jacob navia | last post by:
We have discussed often the proposition from Microsoft for a safer C library. A rationale document is published here by one of the members of the design team at microsoft:...
2
by: Peter K | last post by:
Hi does anyone know of a forum/newsgroup for help with Enterprise Library (application blocks etc)? I have been to www.codeplex.com/entlib, but there is not a terribly large amount of...
0
by: Kevin S Gallagher | last post by:
When building a project using the Speech library version 5. I am getting tons of warnings similar to the two below using option strict and implicit on. The application runs fine otherwise. Any...
0
by: ram1927 | last post by:
Here is my platform: .net framewrok 1.1 Windows Xp Propfessional MS Office 2007. Visual Studio 2003. Window based application. I did below mentioned steps: 1. Right click on C#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.