472,954 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,954 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, 206 views)
Jul 17 '11 #1
1 3715
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.