CRYPT_DATA_BLOB data type to CHAR - Page 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 38

Thread: CRYPT_DATA_BLOB data type to CHAR

  1. #16
    Join Date
    Jul 2008
    Posts
    69
    Some time ago I used the following way:

    Code:
      .....'
      .....
      .....
    
    
       SignMessage(&SignedMessage, pbMessage);
    				
    				
       memcpy(strpbPluscbT , &SignedMessage , sizeof(SignedMessage));
    		
    //////////////strpbPluscbT sent/received/////////////////////
    				
       memcpy(&SignedMessageR , strpbPluscbT  , strlen(strpbPluscbT));
    				           
      VerifySignedMessage(&SignedMessageR, &DecodedMessage);
    
      .....
      .....
      .....
    It dont work.

    If instead I initialize SignedMessageR using memcpy

    Code:
    memcpy(&SignedMessageR , SignedMessageT  , sizeof(SignedMessageT));
    it works. (by working I mean the VerifySignedMessage() returns the correct decoded message.

    I am in greate trouble. Please if any could help me out. I you need the whole code, i will send.

    regards,

  2. #17
    Join Date
    Dec 2003
    Posts
    3,366
    Yes, doing memcpy on the struct is probably the best approach so long as the struct is a simple one with no methods. Just be aware that memcpy + classes or complex structs can fail. For more complex structs and classes you have to do it variable by variable -- often you would have the class return its own serialized byte stream & accept one into a special constructor if you need this cabability.

    So, is it working now, with the full struct memcpy? If so leave it that way, variable by variable was just an example in case you needed to do it on a type that requires it, even if you didnt get that approach to work at least you know its there for you.

    You can automate the process as well, by simply making a union of the data portion of your object with a byte array (of sufficient size of course) and using that byte array. This will save all the memcpy mess.
    This is the recommended approach for a more complex data structure, like a "real" class (methods and inheritence and such).

  3. #18
    Join Date
    Jul 2008
    Posts
    69
    No. the memcpy

    memcpy(&SignedMessageR , SignedMessageT , sizeof(SignedMessageT));

    works if SignMessage() and VerifySignedMessage() procedures are in the same program. Actually, i need to send the SignedMessage structure to receiver, where the the VerifySignedMessage() procedure verifies the signature. For sending I am using Sockets procedure send/recv, which accepts strings only. The VerifySignedMessage() needs EXACTLY the same structure, generated by SignMessage() at transmeter. but unluckily, send() and recv() procedures cant send/recv structures. To be compatible with send()/recv() procedures of sockets, I have to generate a string of that structure, and then re-generate the structure at receiver. I did it in many ways, and cofirmed that yes both of the structures wer 100% same, but ...., the procedure VerifySignedMessage() is still not happy.

  4. #19
    Join Date
    Dec 2003
    Posts
    3,366
    I am not sure what is causing that -- if they are the same, it should be fine.

    I guess I need to know how your verify works then? I will look at what you have again from that angle, I was trying to find a problem with just getting it over the wire and all that looked ok to me.

  5. #20
    Join Date
    Dec 2003
    Posts
    3,366
    I tried to find something wrong using this as a reference:

    http://msdn.microsoft.com/en-us/libr...72(VS.85).aspx

    but I just do not see anything. Maybe you can make their example work?
    It could be that you do not have the same public/private key set up on both computers, too? If both structures are the same, and you set up the encrypt/decrypt properly, it should work, so if both structures are the same, the only thing that can be wrong is the encrypt keys OR a mistake in how you used the library (and I do not see one, seems simple enough!).

  6. #21
    Join Date
    Jul 2008
    Posts
    69
    I have used many examples for signing and verifying, especially, from MSDN.

    the code is given below. I am trying to include PROTOTYPE channel in main().

    Code:
    // SignVerifyuserinputMsg.cpp : Defines the entry point for the console application.
    // Sign and verifies message signature. 
    
    #include "stdafx.h"
    #include <stdio.h> 
    #include <conio.h>
    #include <tchar.h>
    #include <windows.h>
    #include <wincrypt.h>
    
    typedef unsigned char BYTE; 
    
    #pragma comment (lib, "Crypt32")
    #define MY_ENCODING_TYPE  (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
    #define SIGNER_NAME L"test"
    #define CERT_STORE_NAME  L"MY"
    
    struct MyBlob 
    {
    
    	DWORD cbData;
    	BYTE* pbData;
    };
    
    
    void MyHandleError(LPTSTR psz);
    bool SignMessage(MyBlob *pSignedMessageBlob, unsigned char* pbMessage);
    bool VerifySignedMessage(MyBlob *pSignedMessageBlob , MyBlob *pDecodedMessageBlob);
    
    
    int main() 
    { 
    		MyBlob SignedMessage;
    		MyBlob SignedMessageR;
    		MyBlob DecodedMessage;
    
    
            char MsgToBeSigned[50]; 
    		
    
            BYTE* pbMessage; 
    		bool truefalse;
    
            printf(" \n Enter a short Message without spaces..."); 
            scanf("%s", MsgToBeSigned); 
            pbMessage = (BYTE*)(MsgToBeSigned); 
            
    
            SignMessage(&SignedMessage, pbMessage);
    
    		/////////////////////////////
    		///INCLUDE CHANNEL HERE/////
    		////////////////////////////
    			           
    		VerifySignedMessage(&SignedMessage, &DecodedMessage);
    
    		printf(" \n The Decoded Message is : %s", (char*)DecodedMessage.pbData);
    
    		
            free(SignedMessage.pbData);
       
    		getchar(); 
            getchar(); 
            return 0; 
    } 
    
    void MyHandleError(LPTSTR psz)
    {
        _ftprintf(stderr, TEXT("An error occurred in the program. \n"));
        _ftprintf(stderr, TEXT("%s\n"), psz);
        _ftprintf(stderr, TEXT("Error number %x.\n"), GetLastError());
        _ftprintf(stderr, TEXT("Program terminating. \n"));
    } // End of MyHandleError
    
    //----------------------------------------------------------------------------
    //------------ SignMessage ---------------------------------------------------
    
    bool SignMessage(MyBlob *pSignedMessageBlob, unsigned char* pbMessage)
    {
        bool fReturn = false;
        DWORD cbMessage;
        HCERTSTORE hCertStore = NULL;   
        PCCERT_CONTEXT pSignerCert; 
        CRYPT_SIGN_MESSAGE_PARA  SigParams;
        DWORD cbSignedMessageBlob;
        BYTE  *pbSignedMessageBlob = NULL;
    
        // Initialize the output pointer.
        pSignedMessageBlob->cbData = 0;
        pSignedMessageBlob->pbData = NULL;
    
        // The message to be signed.
        // Usually, the message exists somewhere and a pointer is
        // passed to the application.
        
        // Calculate the size of message. To include the 
        // terminating null character, the length is one more byte 
        // than the length returned by the strlen function.
        cbMessage = (lstrlen((TCHAR*) pbMessage) + 1) * sizeof(TCHAR);
    
        // Create the MessageArray and the MessageSizeArray.
        const BYTE* MessageArray[] = {pbMessage};
        DWORD_PTR MessageSizeArray[1];
        MessageSizeArray[0] = cbMessage;
    
        //  Begin processing. 
    
    
    	printf("The message to be signed is :  %s\n",(char*)pbMessage);
    
        // Open the certificate store.
        if ( !( hCertStore = CertOpenStore( 
           CERT_STORE_PROV_SYSTEM,
           0,
           NULL,
           CERT_SYSTEM_STORE_CURRENT_USER,
           CERT_STORE_NAME)))
        {
             MyHandleError(TEXT("The MY store could not be opened."));
             goto exit_SignMessage;
        }
    
        // Get a pointer to the signer's certificate.
        // This certificate must have access to the signer's private key.
        if(pSignerCert = CertFindCertificateInStore(
           hCertStore,
           MY_ENCODING_TYPE,
           0,
           CERT_FIND_SUBJECT_STR,
           SIGNER_NAME,
           NULL))
        {
           _tprintf(TEXT("The signer's certificate was found.\n"));
        }
        else
        {
            MyHandleError( TEXT("Signer certificate not found."));
            goto exit_SignMessage;
        }
    
        // Initialize the signature structure.
        SigParams.cbSize = sizeof(CRYPT_SIGN_MESSAGE_PARA);
        SigParams.dwMsgEncodingType = MY_ENCODING_TYPE;
        SigParams.pSigningCert = pSignerCert;
        SigParams.HashAlgorithm.pszObjId = szOID_RSA_SHA1RSA;
        SigParams.HashAlgorithm.Parameters.cbData = NULL;
        SigParams.cMsgCert = 1;
        SigParams.rgpMsgCert = &pSignerCert;
        SigParams.cAuthAttr = 0;
        SigParams.dwInnerContentType = 0;
        SigParams.cMsgCrl = 0;
        SigParams.cUnauthAttr = 0;
        SigParams.dwFlags = 0;
        SigParams.pvHashAuxInfo = NULL;
        SigParams.rgAuthAttr = NULL;
    
        // First, get the size of the signed BLOB.
        if(CryptSignMessage(
            &SigParams,
            FALSE,
            1,
            MessageArray,
            MessageSizeArray,
            NULL,
            &cbSignedMessageBlob))
        {
            _tprintf(TEXT("%d bytes needed for the encoded BLOB.\n"),
                cbSignedMessageBlob);
        }
        else
        {
            MyHandleError(TEXT("Getting signed BLOB size failed"));
            goto exit_SignMessage;
        }
    
        // Allocate memory for the signed BLOB.
        if(!(pbSignedMessageBlob = 
           (BYTE*)malloc(cbSignedMessageBlob)))
        {
            MyHandleError(
                TEXT("Memory allocation error while signing."));
            goto exit_SignMessage;
        }
    
        // Get the signed message BLOB.
        if(CryptSignMessage(
              &SigParams,
              FALSE,
              1,
              MessageArray,
              MessageSizeArray,
              pbSignedMessageBlob,
              &cbSignedMessageBlob))
        {
            _tprintf(TEXT("The message was signed successfully. \n"));
    
            // pbSignedMessageBlob now contains the signed BLOB.
            fReturn = true;
        }
        else
        {
            MyHandleError(TEXT("Error getting signed BLOB"));
            goto exit_SignMessage;
        }
    
    exit_SignMessage:
    
        // Clean up and free memory as needed.
        if(pSignerCert)
        {
            CertFreeCertificateContext(pSignerCert);
        }
        
        if(hCertStore)
        {
            CertCloseStore(hCertStore, CERT_CLOSE_STORE_CHECK_FLAG);
            hCertStore = NULL;
        }
    
        // Only free the signed message if a failure ocurred.
        if(!fReturn)
        {
            if(pbSignedMessageBlob)
            {
                free(pbSignedMessageBlob);
                pbSignedMessageBlob = NULL;
            }
        }
    
        if(pbSignedMessageBlob)
        {
            pSignedMessageBlob->cbData = cbSignedMessageBlob;
            pSignedMessageBlob->pbData = pbSignedMessageBlob;
        }
        
        return fReturn;
    }
     
      //-----------------------------------------------------------------------
      //---------- Verify Message Signature -----------------------------------
    
    bool VerifySignedMessage(
        MyBlob *pSignedMessageBlob, 
        MyBlob *pDecodedMessageBlob)
    {
        bool fReturn = false;
        DWORD cbDecodedMessageBlob;
        BYTE *pbDecodedMessageBlob = NULL;
        CRYPT_VERIFY_MESSAGE_PARA VerifyParams;
    
        // Initialize the output.
        pDecodedMessageBlob->cbData = 0;
        pDecodedMessageBlob->pbData = NULL;
    
        // Initialize the VerifyParams data structure.
        VerifyParams.cbSize = sizeof(CRYPT_VERIFY_MESSAGE_PARA);
        VerifyParams.dwMsgAndCertEncodingType = MY_ENCODING_TYPE;
        VerifyParams.hCryptProv = 0;
        VerifyParams.pfnGetSignerCertificate = NULL;
        VerifyParams.pvGetArg = NULL;
    
        // First, call CryptVerifyMessageSignature to get the length 
        // of the buffer needed to hold the decoded message.
        if(CryptVerifyMessageSignature(
            &VerifyParams,
            0,
            pSignedMessageBlob->pbData,
            pSignedMessageBlob->cbData,
            NULL,
            &cbDecodedMessageBlob,
            NULL))
        {
    		_tprintf(TEXT("Verification message SUCCEED. \n"));
            _tprintf(TEXT("%d bytes needed for the decoded message.\n"),
                cbDecodedMessageBlob);
    
        }
        else
        {
            _tprintf(TEXT("Verification message failed. \n"));
    		//goto exit_VerifySignedMessage;
        }
      
    	if(!(pbDecodedMessageBlob = 
           (BYTE*)malloc(cbDecodedMessageBlob)))
        {
            MyHandleError(
                TEXT("Memory allocation error allocating decode BLOB."));
            goto exit_VerifySignedMessage;
        }
    
    	if(CryptVerifyMessageSignature(
            &VerifyParams,
            0,
            pSignedMessageBlob->pbData,
            pSignedMessageBlob->cbData,
            pbDecodedMessageBlob,
            &cbDecodedMessageBlob,
            NULL))
        {
            //printf("The verified message is: %s\n",(char*)pbDecodedMessageBlob);
               
    
            fReturn = true;
        }
        else
        {
            _tprintf(TEXT("Verification message failed. \n"));
        }
    	
       exit_VerifySignedMessage:
        // If something failed and the decoded message buffer was 
        // allocated, free it.
        if(!fReturn)
        {
            if(pbDecodedMessageBlob)
            {
                free(pbDecodedMessageBlob);
                pbDecodedMessageBlob = NULL;
            }
        }
    
        // If the decoded message buffer is still around, it means the 
        // function was successful. Copy the pointer and size into the 
        // output parameter.
        if(pbDecodedMessageBlob)
        {
            pDecodedMessageBlob->cbData = cbDecodedMessageBlob;
            pDecodedMessageBlob->pbData = pbDecodedMessageBlob;
        }
    
        
        return fReturn;
    }

  7. #22
    Join Date
    Jul 2008
    Posts
    69
    Yes, I am using the same program, for which you provided the link. Here both of the procedures SignMessage() and VerifySignedMessage() are in the same program, where it works correctly. But the problem is how to transfer the structure? Here is the problem.

    regards,

  8. #23
    Join Date
    Jul 2008
    Posts
    69
    I actually used sockets and separated both of the procedures in different programs. then tried to communication between them using sockets.

    But for simplicity, I am currently trying to convert the original structure returned by SignMessage() to string (as I will have to do it in real client/server applicatioin) then back to structure in the same program. So there is no issue of public/private key, stores etc. In case when both of the procedures are in one program, and if i dont change the struct to string and vice versa, then it works correctly, which will not be the way in real cliet/server.

  9. #24
    Join Date
    Dec 2003
    Posts
    3,366
    Ok, let me make sure that I 100% understand where you are now and what you need to do so we get this done.

    The problem:
    1) You convert the structure to a binary (byte array).
    2) you "sign" the binary for verification or encryption (unclear what the result of sign function is?)
    3) you transmit the result from sign to another computer
    4) that computer verifys / decrypts the binary
    5) the result of verify is put back into structure format and used.

    Where you are:
    you removed the 2 computer setup and are trying a stub program that just does these things locally without transmission. It does not work, you can verify and sign without converting to binary and that works. You can also convert to binary and back without verify and sign, and that works too. But when you do both steps, it is broken. Is that correct?

    Sorry to keep asking but until I fully understand it I am not a lot of help. Once I get it though, I will help you find an answer!

  10. #25
    Join Date
    Jul 2008
    Posts
    69
    NO. some of your points are correct but..., ok i will explain the whole story.

    Sorry, its lengthy, but hope you will understand. it must have spelling mistakes. so dont be confused.

    I am trying to develop a simple application where the server authenticate the client. I assume that the server has sent a challeng message "Hello" to client. the cleint signs it using its private, and sends it to server, where server, using public key of cleint, verifies (decrypts) the signature by comparing the resultant message with the one stored at server. i.e. "Hello".

    I found a program which is availble at the link u provided. this program consists of SignMessage() which accepts the message (e.g. "Hello" I assume it has sent by server) in type usigned char (BYTE) and the address of a structure SignedMessage of type MyBlob. SignMessage(), using the private key from the client's certificate ( i have created X.509 certificate by makecert tool in the same direcotry), creates a digital signautre from the challenge message "Hello", store it in the SignedMessage structure element pbData, and the number of bytes of signature is stored in SignedMessage cbData (DWORD). So I conclude, SignMessage() procedure. It gets the challenge message as BYTE, and stores the signature and its count of bytes in the structure SignedMessage (pbData and cbData).

    Now This program calls another procedure VerifySignedMessage(), which accepts the structure SignedMessage, and the address of new structure DecodedMessage of type MyBlob (all structures are of the same kind as they hold the same data). VerifySignedMessage() decrypts the message using the public key of cleint, and stores the decrypted message in DecodedMessage.pbData, and its count of bytes in DecodedMessage.cbData. If I convert DecodedMessage.pbData to string and compare it with the challenge message "Hello", its same, means verified.

    This was the story of program, which dont use any transfer between two computers.
    Now, I got two programs, where one is client and other is server. They both communication with one another using socket programing procedures send() recv(). As i intend to have a client/server authentication, so i separated the part which signs the challenge message of first program and combined it with the client program. ( actually there were three programs, 1) does signing/verifying, 2) is a client which send a "hello" messag to server, and 3) server which accepts the "hello" message from server. ).
    similarly, i combined the verification portion of first program with server program. so i conclude now: I have two big programs, Cleint and Server.

    Client gets a challenge message "Hello", sign it, stores the signature in a structure (pbData, cbData). I converted the elements of strucutre (of course its elements pbData and cbData) to strings, combine them in one string, transferred it to server using send(). received at server, separated the elements back to structure elements (pbData and cbData). I have defined a structure variable SignedMessageR with the same structure MyBlob, same elements (pbData and cbData). so from the received message, i am trying to initialize the these elements. Its necessary because, the VerifySignedMessage() procedure needs the strucutre. Uing this strucutre, it decrypts the signature using the public key of client and put the decoded message in pbData of a new strucutre DecodedMessage of type MyBlob. I am trying to use pbData of this DecodedMessage to compare it with the message "Hello". it equal means verified.

    These client and server programs are on the same machines.
    While I was practicing, When I initialized the strucutre SignedMessageR from the received string, and passed it to VerifySignedMessage(), the signaure cant be verified. Although, I compared, the received structure, with the one, sent by the client. They both were exactly same.

    To make our program simple, I removed the cleint server programs, and also i removed sockets. I tried to convert strucutre SignedMessage generated by SignMessage(), which hold the message signature, to strings, because this what I will have to do when i want transfer it server. To make it copatible to be transferred to server using send(), I did all the transformation, which I would done if i was using sockets to communicate between cleint and server.

    so i summerize;

    1) the SignMessage() gets message as BYTE and the address of structure SignedMessage.
    2) SignMessage() stores the signature in SignedMessage.bpData and number of byteS of signature in SignedMessage.cbData.

    3) I convertED cbData (DWORD) to int, then to string1. pbData (BYTE) to string2. cancatenated these in string3.
    4) (There is no channel involved, otherwise I would send it via sockets to server), so instead I assumed I send the string3 to server and received there.
    5) I separated this string3 in its ingredients string1 and string2. convert back string1 to DWORD, and string to BYTE and initialized a new strucutre valiable SignedMessageR of type MyBlob, by using
    SignedMessageR.cbData = ....
    SignedMessageR.pbData = (BYTE)string2

    Now I have generated a new structure SignedMessageR. It should be similar to SignedMessage.
    to confirm it, I compared this structure with SignedMessage. In all respect, they both are same.
    6) I transfered this structure SignedMessageR and the address of a new third structure variable DecodedMessage of type MyBlob to VerifySignedMessage(). Which stores the decrypted message in DecodedMessage.pbData.
    7) I converted DecodedMessage.pbData to string, compared it with challenge message "Hello", They are different. means NOT VERIFIED.

    if i skip steps 3 to 6, replace these steps with

    step: I transfered the structure SignedMessage to VerifySignedMessage(), which stores teh decrypted message in DecodedMessage.pdData.
    then step 7.
    It works means VERIFIED.

    now I am confused, what is wrong with the transfermation? Why is VerifySignedMessage() procedure not happy, if i create exacltly equal structure as SignedMessage and pass to it?

    regards,

  11. #26
    Join Date
    Jul 2008
    Posts
    69
    NO. some of your points are correct but..., ok i will explain the whole story.

    Sorry, its lengthy, but hope you will understand. it must have spelling mistakes. so dont be confused.

    I am trying to develop a simple application where the server authenticate the client. I assume that the server has sent a challeng message "Hello" to client. the cleint signs it using its private, and sends it to server, where server, using public key of cleint, verifies (decrypts) the signature by comparing the resultant message with the one stored at server. i.e. "Hello".

    I found a program which is availble at the link u provided. this program consists of SignMessage() which accepts the message (e.g. "Hello" I assume it has sent by server) in type usigned char (BYTE) and the address of a structure SignedMessage of type MyBlob. SignMessage(), using the private key from the client's certificate ( i have created X.509 certificate by makecert tool in the same direcotry), creates a digital signautre from the challenge message "Hello", store it in the SignedMessage structure element pbData, and the number of bytes of signature is stored in SignedMessage cbData (DWORD). So I conclude, SignMessage() procedure. It gets the challenge message as BYTE, and stores the signature and its count of bytes in the structure SignedMessage (pbData and cbData).

    Now This program calls another procedure VerifySignedMessage(), which accepts the structure SignedMessage, and the address of new structure DecodedMessage of type MyBlob (all structures are of the same kind as they hold the same data). VerifySignedMessage() decrypts the message using the public key of cleint, and stores the decrypted message in DecodedMessage.pbData, and its count of bytes in DecodedMessage.cbData. If I convert DecodedMessage.pbData to string and compare it with the challenge message "Hello", its same, means verified.

    This was the story of program, which dont use any transfer between two computers.
    Now, I got two programs, where one is client and other is server. They both communication with one another using socket programing procedures send() recv(). As i intend to have a client/server authentication, so i separated the part which signs the challenge message of first program and combined it with the client program. ( actually there were three programs, 1) does signing/verifying, 2) is a client which send a "hello" messag to server, and 3) server which accepts the "hello" message from server. ).
    similarly, i combined the verification portion of first program with server program. so i conclude now: I have two big programs, Cleint and Server.

    Client gets a challenge message "Hello", sign it, stores the signature in a structure (pbData, cbData). I converted the elements of strucutre (of course its elements pbData and cbData) to strings, combine them in one string, transferred it to server using send(). received at server, separated the elements back to structure elements (pbData and cbData). I have defined a structure variable SignedMessageR with the same structure MyBlob, same elements (pbData and cbData). so from the received message, i am trying to initialize the these elements. Its necessary because, the VerifySignedMessage() procedure needs the strucutre. Uing this strucutre, it decrypts the signature using the public key of client and put the decoded message in pbData of a new strucutre DecodedMessage of type MyBlob. I am trying to use pbData of this DecodedMessage to compare it with the message "Hello". it equal means verified.

    These client and server programs are on the same machines.
    While I was practicing, When I initialized the strucutre SignedMessageR from the received string, and passed it to VerifySignedMessage(), the signaure cant be verified. Although, I compared, the received structure, with the one, sent by the client. They both were exactly same.

    To make our program simple, I removed the cleint server programs, and also i removed sockets. I tried to convert strucutre SignedMessage generated by SignMessage(), which hold the message signature, to strings, because this what I will have to do when i want transfer it server. To make it copatible to be transferred to server using send(), I did all the transformation, which I would done if i was using sockets to communicate between cleint and server.

    so i summerize;

    1) the SignMessage() gets message as BYTE and the address of structure SignedMessage.
    2) SignMessage() stores the signature in SignedMessage.bpData and number of byteS of signature in SignedMessage.cbData.

    3) I convertED cbData (DWORD) to int, then to string1. pbData (BYTE) to string2. cancatenated these in string3.
    4) (There is no channel involved, otherwise I would send it via sockets to server), so instead I assumed I send the string3 to server and received there.
    5) I separated this string3 in its ingredients string1 and string2. convert back string1 to DWORD, and string to BYTE and initialized a new strucutre valiable SignedMessageR of type MyBlob, by using
    SignedMessageR.cbData = ....
    SignedMessageR.pbData = (BYTE)string2

    Now I have generated a new structure SignedMessageR. It should be similar to SignedMessage.
    to confirm it, I compared this structure with SignedMessage. In all respect, they both are same.
    6) I transfered this structure SignedMessageR and the address of a new third structure variable DecodedMessage of type MyBlob to VerifySignedMessage(). Which stores the decrypted message in DecodedMessage.pbData.
    7) I converted DecodedMessage.pbData to string, compared it with challenge message "Hello", They are different. means NOT VERIFIED.

    if i skip steps 3 to 6, replace these steps with

    step: I transfered the structure SignedMessage to VerifySignedMessage(), which stores teh decrypted message in DecodedMessage.pdData.
    then step 7.
    It works means VERIFIED.

    now I am confused, what is wrong with the transfermation? Why is VerifySignedMessage() procedure not happy, if i create exacltly equal structure as SignedMessage and pass to it?

    regards,

  12. #27
    Join Date
    Dec 2003
    Posts
    3,366
    That is a mystery to me too.

    But we can deal with it.
    First, lets make sure the structs are exactly the same.

    Try a memcmp(&signed_struct, &built_up_from_data, sizeof(structname));

    If that is zero, they are the same.

    If they *are* still the same, we are going to try some other stuff.

    try
    built_up = from_signed; //copy the result of the sign function into another struct, and see if verify will work on the copy?


    Finally, you may have to open the debugger and watch the struct and the memory region around it (maybe take a pointer to &struct - 10, and watch from that location to end of struct + 10 as you step through the program). Watch for anything odd when the sign function is used, you are looking for changes adjacent to the struct or inside it in an unexpected location.


    Let me know how these tests go?

  13. #28
    Join Date
    Jul 2008
    Posts
    69
    GOT NEW RESULT.

    using memcmp() result that they are not the same. But my comparision say they are equal. see the way i am comparing using strcmp() and the way of using memcmp().
    Code:
    ////////////////COMPARE USING MY WAY OF strcmp()///////////////////////////
    if(strcmp((char*)SignedMessage.pbData , (char*)SignedMessageR.pbData) == 0 && ((int)SignedMessage.cbData == (int)SignedMessageR.cbData))
    			
          printf (" \n Structures are same....\n\n\n");
    else
          printf (" \n Structures are different...."); 
    
    //////////COMPARE USING MEMCMP()//////////////////////////
    
    if (memcmp(&SignedMessage, &SignedMessageR, sizeof(SignedMessage)) == 0)
         printf("\n\n Still same\n");
    else
         printf("\n NO They are different\n\n\n\n ");

    Now i am sure that there is some problem with the conversion of the whole structure to strings, then back to their correspondind elements, and finally back to a new structure. the new and origional structures looks same but internally they are different, so thats why, VerifySignedMessage() is not happy. The way of creating the new strucutre via converting to strings then back to structure is correct as far as i know, but ....., anyway, below is the whole transformation of structure SignedMessage to string, then back to a new structure SignedMessageR. see it. May be if you point out the mistake in conversion, and if you understand the philosophy of my comparision.

    Code:
    char MsgToBeSigned[] = "Hello";
    pbMessage = (BYTE*)(MsgToBeSigned); 
            
        SignMessage(&SignedMessage, pbMessage);
    
    		
    //////////// Converts DWORD cbData to string //////////////////
    intcbDataT = (int)SignedMessage.cbData;
    
    //converts integer intcbDataT to string strcbDataT 
     itoa(intcbDataT, strcbDataT,10);  
    		
     // converts BYTE pbData to string strpbDataT
    strcpy(strpbDataT, (char*)SignedMessage.pbData); 
    		
    memcpy_s(strpbPluscbT, 100 , strcbDataT, 3);
    memcpy_s(strpbPluscbT + 3, 100 , strpbDataT, strlen(strpbDataT));
    		
    //////////////// strpbPluscbT through CHANNEL...//////////////////
    	SignedMessageR.cbData = 0;
    	SignedMessageR.pbData = NULL;
                 strcpy( strpbPluscbR , strpbPluscbT);
    //////////////////////////////////////////////////////////////////
    ////// strpbPluscbR Sent through Channel////////////////////
    /////////////////////////////////////////////////////////////////
    		
    memcpy_s(strcbDataR , 5 , strpbPluscbR, 3 );  //Separates the strcdData
    
    // separat strpbDataR				
    memcpy_s(strpbDataR , 50 ,strpbPluscbR + 3 , (strlen(strpbPluscbR) - 3));				
    //converts string strcbDataR to int intcbDataR		
    intcbDataR = atoi( strcbDataR ); 		
    		
    ///////////////Initilize NEW Structunre now /////////////
    		
    	SignedMessageR.pbData = (BYTE*)strpbDataR;
    	SignedMessageR.cbData = (DWORD)intcbDataR; 
    		
    		
    ////////////////COMPARE USING MY WAY OF strcmp()///////////////////////////
    if(strcmp((char*)SignedMessage.pbData , (char*)SignedMessageR.pbData) == 0 && ((int)SignedMessage.cbData == (int)SignedMessageR.cbData))
    			
    printf (" \n Structures are same....\n\n\n");
    else
    printf (" \n Structures are different...."); 
    
    ///////////COMPARE USING MEMCMP()///////////////////////////
    if (memcmp(&SignedMessage, &SignedMessageR, sizeof(SignedMessage)) == 0)
    printf("\n\n Still same\n");
    else
    printf("\n NO They are different\n\n\n\n ");
    
    
    		
    VerifySignedMessage(&SignedMessage, &DecodedMessage);
    printf(" \nThe decoded message is ...%s \n\n\n", (char*)DecodedMessage.pbData);
    In the above last secod line
    VerifySignedMessage(&SignedMessage, &DecodedMessage);
    if i use SignedMessage, the resultant decodedmessage is "Hello", but if i replace it with SignedMessageR, its an unreadable message.

  14. #29
    Join Date
    Dec 2003
    Posts
    3,366
    ok, next step:

    Just copy the structs directly over with memcpy, remove ALL use of strcpy from it.

    Just do memcpy( &dest_struct, &source_struct, sizeof(struct));
    Now do your memcmp and they had better match. If they do, pop the destination into verify and it should work. If it does, its solved, just do it that way.


    -----------------
    On top of that, for this type of struct (only has data, no functions) you can probably do this directly:

    send( (char *)(&signed_result_struct), sizeof(struct_type));
    and recieve it the same way
    receive( (char*)(&destination_struct), sizeof(struct_type));

    ------------------

    Finally, the problem was probably strcpy all along. This function stops copying at the first 0 it finds (not '0', value 0x00 hex) and very likely, the data was not actually in string format & so you were getting partial results. If these changes all work, thats what it must have been. That was my bad, I thought you knew whether your data was printable (a string) or binary (bytes that happen to be stored in chars because in c++, char is a byte, but NOT textual data).

  15. #30
    Join Date
    Jul 2008
    Posts
    69
    When I initialize the new structure using memcpy like

    memcpy( &SignedMessageR, &SignedMessage, sizeof(SignedMessage));

    the comparison was correct. means both structure were same. Now when i came to second use, as there are send() receive(), so i came to the main original program, where i have implemented socket programming. There are two programs (on the same machines), the client and server.

    At the client program, i called the SignMessage() procedure, and then sent the structure holding the signature to server by using

    send(theSocket, (char*)(&SignedMessage), sizeof(SignedMessage), 0);

    where "theSocket" is corresponding socket.

    At receiver, I have declared similar structure MyBlob, with three instances

    MyBlob SignedMessageR;
    MyBlob SignedMessage;
    MyBlob DecodedMessage;

    the receiver receives as:

    recv(client , (char*)(&SignedMessageR), sizeof(SignedMessageR) , 0);

    I directly transferred this structure to VerifySignedMessage() using:

    VerifySignedMessage(&SignedMessageR, &DecodedMessage);

    But it did not work. the decoded message is not "Hello".

    To confirm, whether, the structure i received is same to the original one, I called SignMessage() procedure in the server program as well like:

    char MsgToBeSigned[] = "Hello";
    MessageAsByte = (BYTE*)(MsgToBeSigned);
    SignMessage(&SignedMessage, MessageAsByte);

    This creates a signature structure similar to the one sent be cleint program.

    Now i compared this SignedMessage with SignedMessageR in server program, using memcmp() but unluckily they were different.

    What I am thinking is that send() dont sent the strucutre as it is, or the recv() dont receive it as was sent.

Similar Threads

  1. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 11:20 PM
  2. Replies: 9
    Last Post: 07-28-2005, 09:40 PM
  3. Replies: 0
    Last Post: 10-11-2002, 11:52 AM
  4. Altering created Distinct Data Type
    By Sindhu in forum Database
    Replies: 0
    Last Post: 12-06-2001, 12:28 PM
  5. Numeric data type
    By Vhost in forum Database
    Replies: 1
    Last Post: 03-28-2001, 09:38 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center