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 - 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));
1 3715
How to read decrypted value from registry: - 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: - 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;
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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?
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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#...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |