|
Thanks to all who took time and replied to my post.
I too admit that I was not very clear in my earlier post so here I am giving the details.
First I have written a code (in C) which should write a signature of an XML file, generate a keyBLOB which I am storing on some speciied path in my PC and then it stroes the signature at some specified location.
In my second code I want to take the input of the orginal XMl file, the kep BLOB from the path where I stored it and the signature of the file and then have to verify the signature.
Now I give the code details which I have done in two parts, the first part tells about creating signature and 2nd part tells about the verification:
1. In the first part (signing of XML file)
HANDLE hFileHandle = CreateFile("C:\\\\SampleConfig.xml",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
HANDLE hsigHandle;
HANDLE hkeyHandle;
if(hFileHandle==INVALID_HANDLE_VALUE)
{
return FALSE;
}
if(dwFileSize==0)
{
CloseHandle(hFileHandle);
return FALSE;
}
SetFilePointer(hFileHandle,0,0,FILE_BEGIN);
ZeroMemory(pXMLData, dwFileSize);
if(!ReadFile(hFileHandle,pXMLData, dwFileSize,&dwBytesRead,NULL))
{
CloseHandle(hFileHandle);
delete[] pXMLData;
return FALSE;
}
printf("XML file extracted ....\n");
if(CryptAcquireContext( &hProv, UserName, MS_DEF_PROV, PROV_RSA_FULL, 0)) //changed the CSP name from NULL to MS_DEF_PROV
{
printf("CSP context with %s named key container has been acquired.\n", UserName);
}
else
{
{
if(CryptAcquireContext(&hProv, UserName, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
printf("A new key container has been created.\n");
}
else
{
printf("Could not create a new key container.\n");
MyHandleError("Error in creating a new key container.");
exit(1);
}
}
else
{
printf("A cryptographic service handle could not be acquired.\n");
MyHandleError("A cryptographic service handle could not be acquired.");
exit(1);
}
} // End of else.
if(CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
{
printf("The signature key has been acquired. \n");
}
else
{
if (NTE_NO_KEY == GetLastError ())
{
// Create signature key pair.
if (! CryptGenKey (hProv, AT_SIGNATURE, 0, &hKey))
{
// Error during CryptGenKey!
return FALSE;
}
else
{
CryptDestroyKey (hKey);
}
MyHandleError("Error during CryptGetUserKey for signkey.");
}
}
if(CryptExportKey(hKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwBlobLen))
{
printf("Size of the BLOB for the public key determined. and its %d long\n", dwBlobLen);
}
else
{
MyHandleError("Error computing BLOB length.");
}
// Allocate memory for the pbKeyBlob.
if(pbKeyBlob = (BYTE*)malloc(dwBlobLen))
{
printf("Memory has been allocated for the BLOB. \n");
}
else
{
MyHandleError("Out of memory. \n");
}
// Do the actual exporting into the key BLOB.
if(CryptExportKey( hKey, NULL, PUBLICKEYBLOB, 0, pbKeyBlob, &dwBlobLen))
{
printf("Contents have been written to the BLOB. \n");
hkeyHandle = CreateFile("D:\\pubkey.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hkeyHandle==INVALID_HANDLE_VALUE)
{
return FALSE;
}
SetFilePointer(hkeyHandle,0,0,FILE_BEGIN);
if(!WriteFile(hkeyHandle,pbKeyBlob,dwBlobLen,&dwkeyBytesWritten,NULL))
{
CloseHandle(hkeyHandle);
return FALSE;
}
printf("\nKey FILE CREATED\n\n");
}
else
{
MyHandleError("Error during CryptExportKey.");
}
//-------------------------------------------------------------------
// Create the hash object.
if(CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
printf("Hash object created. \n");
}
else
{
MyHandleError("Error during CryptCreateHash.");
}
// Compute the cryptographic hash of the buffer.
if(CryptHashData(hHash, pXMLData, dwBufferLen, 0))
{
printf("The data buffer has been hashed.\n");
}
else
{
MyHandleError("Error during CryptHashData.");
}
// Determine the size of the signature and allocate memory.
dwSigLen= 0;
if(CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSigLen))
{
printf("Signature length %d found.\n",dwSigLen);
}
else
{
MyHandleError("Error during CryptSignHash.");
}
// Allocate memory for the signature buffer.
if(pbSignature = (BYTE *)malloc(dwSigLen))
{
printf("Memory allocated for the signature.\n");
}
else
{
MyHandleError("Out of memory.");
}
// Sign the hash object.
if(CryptSignHash(hHash, AT_SIGNATURE, NULL, 0, pbSignature, &dwSigLen))
{
printf("pbSignature is the hash signature.\n");
printf("\n");
hsigHandle = CreateFile("D:\\signature.binary",GENERIC_WRITE,0,NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(hsigHandle==INVALID_HANDLE_VALUE)
{
return FALSE;
}
SetFilePointer(hsigHandle,0,0,FILE_BEGIN);
if(!WriteFile(hsigHandle,pbSignature, dwSigLen,&dwBytesWritten,NULL))
{
CloseHandle(hsigHandle);
return FALSE;
}
printf("SIGNATURE FILE CREATED\n\n");
}
else
{
MyHandleError("Error during CryptSignHash.");
}
// Destroy the hash object.
if(hHash)
{
if (CryptReleaseContext(hProv,0))
{
printf("The handle has been released.\n");
}
else
{
printf("The handle could not be released.\n");
}
}
printf("The hash object has been destroyed.\n");
printf("The signing phase of this program is completed.\n\n");
}// End of main
void MyHandleError(char *s)
{
fprintf(stderr,"An error occurred in running the program. \n");
fprintf(stderr,"%s\n",s);
fprintf(stderr, "Error number %x.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
exit(1);
} // End of MyHandleError
The above code successfully signs the XML file and I can store the key and signature on the hard disk.
2. Now I am writing a separate code for the verification of the created signature whioch would run on separte PC which I am intending to write in this manner.
---
HANDLE hFileHandle = CreateFile("D:\\Priyanka\\PLUTUS\\Citibank\\CitiCode\\CitiInit\\SampleConfig.xml",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwFileSize= GetFileSize(hFileHandle,NULL);
BYTE* pXMLData = new BYTE[dwFileSize];
DWORD dwBufferLen = strlen((char *)pXMLData)+1;
HANDLE hsigHandle = CreateFile("D:\\signature.binary",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwSigFileSize= GetFileSize(hsigHandle,NULL);
BYTE* pbSignature = new BYTE[dwSigFileSize];
DWORD dwSigLen = strlen((char *)pbSignature)+1;
HANDLE hkeyHandle = CreateFile("D:\\pubkey.txt",GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwKeyFileSize= GetFileSize(hkeyHandle,NULL);
BYTE* pbKeyBlob = new BYTE[dwKeyFileSize];
DWORD dwBlobLen = strlen((char *)pbKeyBlob)+1;
Then I will read the contents of the XML file, Public key file and the signature file and store the data in pXMLData, pbKeyBlob and pbSignature.
Now I am planning to do the verificatiion in this manner that
a)if(CryptImportKey(hProv, pbKeyBlob, dwBlobLen, 0, 0, &hPubKey))
{
printf("The key has been imported.\n");
}
else
{
MyHandleError("Public key import failed.");
}
if(CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
printf("The hash object has been recreated. \n");
}
else
{
MyHandleError("Error during CryptCreateHash.");
}
if(CryptHashData(hHash, pbBuffer, dwBufferLen, 0))
{
printf("The new has been created.\n");
}
else
{
MyHandleError("Error during CryptHashData.");
}
if(CryptVerifySignature(hHash, pbSignature, dwSigLen, hPubKey, NULL,0))
{
printf("The signature has been verified.\n");
}
else
{
printf("Signature not validated!\n");
}
free(pbSignature);
CryptDestroyHash(hHash);
here I want to know tht whether I would have to acquire the CSP context again in the 2nd part in the verification code with the similar parameters which I used at the time of creatin gthe signature or would have to use the same handle hProv which was create in the first part? I think that I should acquire the CSP context again and when I am doing that and then does the CryptImportKey it gives me the NTE_BAD_DATA error (from the GetLastError()).
Please help me how should I verify it and tell me is the way of creating the signature and the kepBLOB correct or not?
Please reply soon as I am stuck now ...
Thnx
|
|
|
|
|
Hi All!
i have some problem in attatching a SOCKET handle to a CSocket object.
I'm creating a Socket on Server side, and accepting the conncetion using a
temperory socket object, and retrieving the SOCKET handle by using
CSocket::Detach(), below is the code i used...
At Server: in the main thread.
CListeningSocket ServerSocket;// class CListeningSocket : public CSocket
CSocket TempSocket;
ServerSocket.Create(port);
ServerSocket.Listen();
ServerSocket.Accept(TempSocket);
SOCKET hSocket = TempSocket.Detach();
and i'm passing this handle to a Thread. (trying to create a separate thread for each client)
( this techinque is mentioned in one of the articles from Microsoft
the link is [url]http://support.microsoft.com/kb/175668[/url] )
In the CSocketThread: i'll receive the SOCKET handle as parameter
(even tried taking the SOCKET handle as a member variable, using a member function to set the m_hSocket)
CClientSocket *pSocketForClient = new CClientSocket;
SOCKET hSocket = (SOCKET)Param;
pSocketForClient->Attatch(hSocket); // Problem is here
The problem is while trying to attatch its trowing some exception. I tried using ::GetLastError() to find
the error code, but its returning ZERO.
Even i tried to catch exception but, i'm unable to catch using catch(CException e).
i modified the code as
try {
pSocketForClient->Attatch(hSocket);
}
catch(CException e)
{ // some code to find exception
}
but its not catching the exception. i'm able to catch the exception by catch(...)
but as u know, it is of no use to find the error
Client code:
CSocket Socket;
Socket.Create();
Socket.Connect(ip, port);
PLAESE HELP ME
ramana.
|
|
|
|
|
I would suggest you to check if the SOCKET handle, obtained by Accept and then passed to your thread, is valid, i.e. is not "-1". Then put a breakpoint at Attach line and then continue execution using Step Into (F11). You should see the MFC source code and found the cause of the exception.
In order to catch the exception, try a different declaration:
try
{
. . .
}
catch( const CException * e)
{
. . .
}
catch( const CException & e)
{
. . .
}
I hope this helps.
|
|
|
|
|
Is this in an MFC app (yes, I know you're using an MFC class CSocket)? Is it a console/service
application or a GUI application?
|
|
|
|
|
its a GUI based application
|
|
|
|
|
I would start by stepping into your CClientSocket::Attatch() function in the debugger
and you'll see what line the exception occurs on. Put a breakpoint on this line
pSocketForClient->Attatch(hSocket); // Problem is here
and use F11 key to step into the Attatch() function. There' s probably a pointer issue of
some kind. If you post the code for Attatch() maybe I can help a little
Mark
|
|
|
|
|
Hi Mark
I dint override the Attatch() member function of CAsyncSocket class.
I stepped in to the framework code of MFC.
i'm giving the details, where it is going wrong.
File : MAP_PP.CPP
Function : void* CMapPtrToPtr::GetValueAt(void* key) const
Line: 179
Code: if (m_pHashTable == NULL)
return NULL;
because of the abov code, some runtime error is occuring.
Pls suggest me, where it may go wrong?
ramana.
|
|
|
|
|
Do you have something like this in your app class' InitInstance() ?
if (!AfxSocketInit())
{
::SetCursor(::LoadCursor(0, IDC_ARROW));
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
|
|
|
|
|
ya. i have the code in InitInstance() method of APP class
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
|
|
|
|
|
hmmm should work.
The only thing I can think of at this point is that your thread that you pass the socket to
needs to have a message loop because the CSocket/CAsyncSocket classes use a window for socket
messages. I'm not sure if this is related to the exception you are getting though
When you stepped into the Attach function, how far did it get? Specifically, how far into the
CAsyncSocket::AttachHandle() did it go? Did the CreateEx() for the window succeed?
We'll figure this out
|
|
|
|
|
I'm using VC7.1. And I want to disable the default behaviour of the Ecs key for a print preview view.
Can Anybody help me? Thanks in advance.
Max
|
|
|
|
|
I have added an OnKeyDown() message handler for the CPreviewView derived class in the project, but it has no chance to be invoked.
|
|
|
|
|
Hi,
Maybe I had run across the similar problem like yours,i mean,the one OnKeyDown() can't get called.
I think probably it's because the view to which your OnKeyDown() belongs doesn't have the input focus,so it seems you should let the view owns the input focus in someway.
Hope it could be of some help,good luck!
|
|
|
|
|
Thanks, graceleee.
But I cannot figure out how to get my view have the input focus.
|
|
|
|
|
Can a game controller be used with VC++? I haven't done anything on this, but was wondering if a controller could be used as an input device in a regular VC++ application in place of a mouse. Since there are more buttons on a controller than a mouse, can they be detected in a program? Does anyone recommend any particular controller if I wanted to work with this? Any references on this? Thanks.
|
|
|
|
|
It's detected by DirectX. Install DX and you can use it.
Christian Graus - C++ MVP
|
|
|
|
|
In my server application,I use Memory pool to malloc
memory in heap and use stack variable whenever possible,
During test,we found no memory leak from the report of memory
pool. However I did find one strange problem which puzzles me
for a long time:
the physical memory which server applications uses varies too
much, when the program starts it is 12MB,however in 5 minutes
maybe it goes to 50MB or so,Then at some time it drop 35MB suddenly
to 15M.
I use Microsoft WMPPlayer Active control ,it did have some
effects on the problem above,but even I remove it ,the problem also
not solved ,only the peak memory use would be brought down to 40MB.
I am wondering if the physical memory page problem ,or any thing
related to the Microsoft Win2000 professional system's memory management.
I try to find some information on Internet ,but none is helpful.So
I beg for any suggestions or sharing experience here .Thank you in advance.
|
|
|
|
|
How do you tell how much is being used ?
Christian Graus - C++ MVP
|
|
|
|
|
I get the memory my application used from the Windows "Task Manager",
which has "Memory usage","Peak Memory Usage" column and etc
|
|
|
|
|
TM simply tells you the amount of your application's address space that is in use. This is quite different from the amount of that address space your program is actually using.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
How can a user-mode client achieve direct access to kernel streaming(KS) filter in video capture? I know there is a sample from microsoft using KS in audio rendering and capture. Is KS feasible in video capture? Any suggesion and samples will be appreciated. Thanks in advance.
|
|
|
|
|
Hi,this is my first post here.I have a problem troubling me for days.
How to popup a menu after i strike a previously associated key,just like direct mouse click on the menu?
Say,in app-wizard created doc/view application,i associated 'File' menu with key 1 and 'View' with 2. I want to popup the File menu(all first level submenus under it 'popup') at the stroke of key 1 and View menu at key 2.
I tried handling WM_KEYDOWN message in view and in OnKeyDown() set the relevant menu item to MFS_HILITE(because i couldn't find other more suitable flags like MFS_POPUP or MFS_ACTIVATE etc).But it only hilite the File item itself instead of poppinp up submenus under it.Here is my code
<br />
void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)<br />
{<br />
if(nChar >= 0x31 && nChar <= 0x39) {
int nPosition = nChar - 0x30 -1;<br />
CMenu *pMenu = GetParentFarme()->GetMenu();<br />
MENUITEMINFO mii;<br />
ZeroMemory(&mii, sizeof(MENUITEMINFO));<br />
mii.cbSize = sizeof(MENUITEMINFO);<br />
mii.fMask = MIIM_STATE;<br />
mii.fState = MFS_HILITE;<br />
<br />
pMenu->SetMenuItemInfo(nPosition,&mii,1);<br />
GetParentFrame()->DrawMenuBar();<br />
}<br />
else {<br />
...<br />
}<br />
}<br />
I also tried the TrackPopupMenu(),but it doesn't seem the right direction.
I have seached web but couldn't get an answer like before i did.Could anyone help me out? Thanks a lot!
|
|
|
|
|
Hi all!
I have a problem of copying and pasting on my SDI application. My view is derived from CFormView. I created some edit controls,... on it but when i run my application, i can't use Ctrl+C or Ctrl+V to copy or paste . Who can help me
Thank in advance!
Rgs!
|
|
|
|
|
I am trying to write a simple application that is capable of zipping files. To do this I am using ‘LiteZip’.
I am writing with Visual C++ 2005.
The code which generates the error is:
#include "litezip.h"
HZIP hz;
ZipCreateFile(&hz,"FileName.zip",0);
The error message is:
error C2664: 'ZipCreateFileW' : cannot convert parameter 2 from 'const char [9]' to 'const WCHAR *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
From what I understand, I need to convert a CHAR to WCHAR.
This is done by…. ??
Regards
Pete
|
|
|
|
|
Since you have Unicode enabled, try:
HZIP hz;
ZipCreateFile(&hz,L"FileName.zip",0);
|
|
|
|
|