CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    5

    help with CAPICOM verification of signature

    I'm developing an app which requires me to sign and verify data using CAPICOM module. for example: If i have a text file with content: "This is a string", the app will allow me to select a certificate with a private key and sign the message (creating a hash encoded in base 64) and put it in another text file. this bit works. the problem i'm having is that when i go to verify the hash, it keeps failing. how i structure my code - open the hash text file, put all of the hashed data in a _bstr_t variable (as required by the verify function) and then call the verify function on it. This part keeps failing for some reason. I keep getting a "Unhandled exception" on the verify function call.

    This approach works in java, but is somehow failing in c++. Any help on this would be greatly appreciated.

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: help with CAPICOM verification of signature

    Not having powers of telepathy, a small code example would help quite a bit
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Jun 2009
    Posts
    5

    Re: help with CAPICOM verification of signature

    oops my bad.

    code for opening and signing the data in file (a bit of mfc):

    CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
    fOpenDlg.m_pOFN->lpstrTitle= _T("Open File to Sign...");
    CString fName
    if(fOpenDlg.DoModal() ==IDOK)
    {
    fName = fOpenDlg.GetFileName();

    }
    long int size;
    char *buf;
    ifstream in(fName,ios::in|ios::ate);
    if(!in.is_open()){AfxMessageBox(_T("File Not Found"));}
    else{
    size = in.tellg();
    buf = new char[size-1 ];
    buf[size]=0;
    in.seekg(0,ios::beg);
    in.read(buf,size);
    in.close();
    cipherText = buf;
    name = (fName+"");
    }

    //^File opened, data copied into buffer, and then held into _bstr_t cipherText
    ICertificate2Ptr cert=NULL;
    cert = (ICertificate2Ptr) pDisp.pdispVal;
    CAPICOM::ISignerPtr signer(__uuidof(Signer));
    CAPICOM::ISignedDataPtr sigData(__uuidof(SignedData));
    signer->PutCertificate(cert);
    sigData->Content = cipherText;
    sigStr = sigData->Sign(signer,true,CAPICOM_ENCODE_BASE64);
    name.append(".encrypted");
    out.open(name.c_str(),ios:ut/*|ios::binary*/);
    out<<sigStr;
    out.close();
    //^This creates an output file called fName.encrypted


    For verification:
    *again file is opened in similar fasion, the hashed data is copied into a buffer and then into a _bstr_t variable called temp*

    CAPICOM::ISignerPtr signer(__uuidof(Signer));
    CAPICOM::ISignedDataPtr sigData(__uuidof(SignedData));
    sigData->Content = temp;
    _bstr_t temp2;
    temp2 = sigData->Verify(temp,true,CAPICOM_VERIFY_SIGNATURE_ONLY);
    //^Unhandled exception generated here
    *Once it should've done verification it should place the unhashed signature into an output file of .txt extension which it does in java but not c++ ... *

    name.append(".txt");
    out.open(name.c_str(),ios:ut);
    out<<temp2;
    out.close();


    further note: changing Verify to raw_Verify, does get past the Verify(...) line, but then places the same hash into the newly formed .txt file, whereas I want the dehashed data (as it does in java codes with this much of code...)

    thanks for taking the effort

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: help with CAPICOM verification of signature

    It would look even better with code tags
    Code:
    CFileDialog fOpenDlg(true,NULL,NULL,NULL,_T("All Files (*.*)|*.*||"),this);
    fOpenDlg.m_pOFN->lpstrTitle= _T("Open File to Sign...");
    CString fName
    if(fOpenDlg.DoModal() ==IDOK)
        {
            fName = fOpenDlg.GetFileName();
            
        }
    long int size;
        char *buf;
        ifstream in(fName,ios::in|ios::ate);
        if(!in.is_open()){AfxMessageBox(_T("File Not Found"));}
        else{
            size = in.tellg();
            buf = new char[size-1 ];
            buf[size]=0;
            in.seekg(0,ios::beg);
            in.read(buf,size);
            in.close();    
            cipherText = buf;
            name = (fName+"");
        }
    
    //^File opened, data copied into buffer, and then held into _bstr_t cipherText
    ICertificate2Ptr cert=NULL;
    cert = (ICertificate2Ptr) pDisp.pdispVal;
    CAPICOM::ISignerPtr signer(__uuidof(Signer));
    CAPICOM::ISignedDataPtr sigData(__uuidof(SignedData));
    signer->PutCertificate(cert);
    sigData->Content = cipherText;
    sigStr = sigData->Sign(signer,true,CAPICOM_ENCODE_BASE64);
    name.append(".encrypted");
    out.open(name.c_str(),ios::out/*|ios::binary*/);
    out<<sigStr;
    out.close();
    //^This creates an output file called fName.encrypted
    
    
    For verification: 
    *again file is opened in similar fasion, the hashed data is copied into a buffer and then into a _bstr_t variable called temp*
    
    CAPICOM::ISignerPtr signer(__uuidof(Signer));
    CAPICOM::ISignedDataPtr sigData(__uuidof(SignedData));
    sigData->Content = temp;
    _bstr_t temp2;
    temp2 = sigData->Verify(temp,true,CAPICOM_VERIFY_SIGNATURE_ONLY);
    //^Unhandled exception generated here
    *Once it should've done verification it should place the unhashed signature into an output file of .txt extension which it does in java but not c++ ... *
    
    name.append(".txt");
    out.open(name.c_str(),ios::out);
    out<<temp2;
    out.close();
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: help with CAPICOM verification of signature

    Have you tried reading a C++ produced encrypted file back into Java and vice-versa? It would determine if you had created a valid file format.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jun 2009
    Posts
    5

    Re: help with CAPICOM verification of signature

    the file format doesn't matter, because I can append it to be whatever format i want (i think that's what you mean? the .encrypted part). the verification basically depends on the content of the file, which is read in. now i did read things online that suggested passing in the string backwards, converting to byte array and such, but since it's already in unicode the content should be i nthe correct form (i believe)

  7. #7
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: help with CAPICOM verification of signature

    I was wondering whether you had checked that the file conformed correctly to *your* format.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #8
    Join Date
    Jun 2009
    Posts
    5

    Re: help with CAPICOM verification of signature

    ah thats what you meant. i'll check that in a bit as the developed java app is locked on another colleague's account.

    edit: created the hash from the vb code. it looks completely different. I asked him how he stores the hash (looks like when you open a dll in notepad) back as normal text. he said he writes it to the destination (in the vb code) as bytes. so i guess my question now is: how do i take data and write it as bytes in C++, and lets see how it goes from there...
    Last edited by namesless; June 26th, 2009 at 07:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured