CRYPT_DATA_BLOB data type to CHAR - Page 3


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 3 of 3 FirstFirst 123
Results 31 to 38 of 38

Thread: CRYPT_DATA_BLOB data type to CHAR

  1. #31
    Join Date
    Dec 2003
    Posts
    3,366
    all I can think of tonight is to run the programs on different machines if you can. Sometimes the socket setup works on a single machine, other times it will not, and I do not have the rules that control this memorized. Because you tried both programs on the same machine, you may have received nothing at all & tested garbage --- you could verify that by setting your recieve buffer to zero memset(rxbuffer, 0, sizeof(struct)) and then look at it after your recieve command. If it is still all zeros you did not get anything?

    Alternately you can use printf with %x to print the hex of each byte on each side to see what you sent and what you got. This is probably a good idea, but I think the real issue is probably need to use 2 computers so try that first and if its still not working, then add debugging to visually compare the two buffers.

  2. #32
    Join Date
    Nov 2003
    Posts
    4,118
    look for two common suspects:
    1)using sizeof() with an array name (which in certain cases decyas into a pointer, so the sizeof expression evaluates as 4, not the size of the array
    2) using any of the C-string functions (strlen, strcpy) to process a buffer of bytes that potentially contains binary zeroes here and there.
    Another problem: passing a pointer to a pointer when only a pointer is required. Normally, the compiler catches this but not when you use explicit casts all over the program.
    Danny Kalev

  3. #33
    Join Date
    Jul 2008
    Posts
    69
    A very interesting situation i am facing now. I reduced the problem of structure sending via sockets to sending strings. When I did signing and verifying in one program, where I just converted the origional structure holding signature to a string then back to a new structure, and pass this new structure to verifysignmessage(), it worked. the new and the original structures are same. the variable strRecvd is "char strRecvd[9]". the code is given below.
    Code:
    pbMessage = (BYTE*)(MsgToBeSigned); 
            
      SignMessage(&SignedMessage, pbMessage);
    
    memcpy(strRecvd , (char*)(&SignedMessage) , sizeof(SignedMessage));
    
    memcpy(&SignedMessageR , strRecvd , sizeof(SignedMessage));
    
    				
    ///////////COMPARE USING MEMCMP()///////////////////////////
    if (memcmp(&SignedMessage, &SignedMessageR, sizeof(SignedMessage)) == 0)
    printf("\n\n Both structures are SAME \n\n");
    else
    printf("\n NO They are different\n\n\n ");
    		
    		           
    VerifySignedMessage(&SignedMessageR, &DecodedMessage);
    So Now I thought, to separate SignMessage() and VerifySignedMessage() procedures in Client and Server programs. it will be successful if only i transfer the string strRecvd to receiver. so in client i did the following
    Code:
    MessageAsByte = (BYTE*)(MsgToBeSigned); 
    SignMessage(&SignedMessage, MessageAsByte);	
    memcpy(strRecvd , &SignedMessage , sizeof(SignedMessage));
    
    memcpy(strRecvd , &SignedMessage , sizeof(SignedMessage));
    					
     send(theSocket, strRecvd, 8, 0); //I sent the 8 bytes of the array
    At receiver, I generated the signature SingedMessage by calling SignMessage() just to generate a similar signature sent by client for verification purposes.

    i did as the following:
    Code:
    //creates SignedMessage signature for verification purposes
    MessageAsByte = (BYTE*)(MsgToBeSigned); 
    SignMessage(&SignedMessage, MessageAsByte);	
    memcpy(strRecvdTemp , (char*)(&SignedMessage) , 8);
    
    
    recv(client , strRecvd , 8 , 0);
    memcpy(&SingedMessageR , strRecvd , 8); //initialize a new structure at receiver from received string.
    Now I compared these newly generated Structure SignedMessageR and the one generated here at server for verification purposes SignedMessage by
    Code:
    memcmp(&SignedMessage, &SignedMessageR, sizeof(SignedMessage))
    they were not same. The first four bytes are same.

    I converted the SignedMessage to string and compared with strRecvd (received via sockets), but they were also similar in first 4 bytes.

    I then transferred the newly generated structure SignedMessageR to VerifySignedMessage(), it was NOT verified. If I use SignedMessage, it is verified.

    As far as i confirmed, if i send the structure by converting to string at Cleint, then sent, and back convert to strucutre, the first four bytes are correctly transferred but not the rest.

    To check whether, if there is any problem, I transferred some text, for testing, it worked. It transferred the whole string correctly.

    I have finished almost the whole task, but i am stuck only in this small, but very strange problem.

    regards,

  4. #34
    Join Date
    Nov 2003
    Posts
    4,118
    You lost me about a dozen posts ago, so can you please tell what the types of SignedMessage are SignedMessageR?
    Also, did you check the values of these objects before sending/receiving them?
    Danny Kalev

  5. #35
    Join Date
    Jul 2008
    Posts
    69
    Yes. I confirmed the values at both ends. SignedMessage and SignedMessageR are structures of same kind. The same structure is difined at both client and server;

    // MyBlob is defined at both Client and Server
    MyBlob
    {
    DWORD cbData;
    unsigned char pbData;
    };

    MyBlob SignedMessage; // for cleint // sometime at server as well for testing purposes

    MyBlob SignedMessageR; // At server only to hold the received signature

    char strRecvd[9]=""; //same stiring at both ends.



    if i dont involve sockets (send(), recv()) and simply change SignedMessage() to string and then back to structure, and pass it to VerifySignedMessage(), the signature is verified. but if separate the client and server, and involve send()/recv() and send the string (obtained after converting the structure SignedMessage to string) to server, convert it back to a strcutre of the same kind (as i did when both client/server were in one program, withouth involving sockets), and pass it to VerifySignedMessage(), it was NOT VERIFIED. mean, the string i sent was not the same i received.

    (the sizeof(SingedMessage) is 8, so I am using 8 in send() recv(). I have tried every possibility here. I change this value to different values)

    Code:
    SignMessage(&SignedMessage, MessageAsByte);
    memcpy(strRecvd , (char*)(&SignedMessage) , sizeof(SignedMessage));
     
    send(theSocket, strRecvd, 8, 0); //I sent the 8 bytes of the array
            
             // From Client//
     //             //////
     //             //////
     //             //////
     //           \\\\/////
     //            \\\////
     //             \\///
    
          // To Server///
    
    recv(client , strRecvd , 8 , 0);
    memcpy(&SignedMessageR , strRecvd , sizeof(SignedMessageR));
               
    VerifySignedMessage(&SignedMessageR, &DecodedMessage); // Not verified
    Simply, if I remove, the sockets (if client and server are in the same program), the result is ok. like see below.

    Code:
    SignMessage(&SignedMessage, MessageAsByte);
    memcpy(strRecvd , (char*)(&SignedMessage) , sizeof(SignedMessage));
     
    
    memcpy(&SignedMessageR , strRecvd , sizeof(SignedMessageR));          
    VerifySignedMessage(&SignedMessageR, &DecodedMessage); // Verified

  6. #36
    Join Date
    Jul 2008
    Posts
    69
    SORRY! In the structure definition MyBlob, pbData is of type "unsigned char*", not "unsigned char". This holds the digital signature of the message, and cbData holdS the number of bytes of signature.

  7. #37
    Join Date
    Jul 2008
    Posts
    69

    Base64 Encoding

    Aa far as I think now the problem than only four bytes are sent correctly, while not the rest is that, strings dont deal 0x00. i.e. if there is a 0 inside in middle of string holding signature will be ignored. so for this Base64 encoding is a sulution.

    can anybody tell how to encode a string? I know the concept of Base64 enocding, but cant do it in C++.

    regards,

  8. #38
    Join Date
    Nov 2003
    Posts
    4,118
    First of all, the following line doesn't do what you think it does:

    Code:
    char strRecvd[9]="";//wrong!
    It doesn't initialize all bytes to zeroes, it merely assigns the first two bytes.
    You should use instead:
    Code:
    MyBlob SignedMessage={0};  
    
    MyBlob SignedMessageR; ={0} ;
    
    char strRecvd[8]={0}; //correct initialization. don't use 9 bytes!
    Don't use an odd value for the byte array because it could lead to nasty alignment bugs. Use 8, 12 etc (any number divisible by 4, that is).
    Also, what's the purpose of calling memcpy? In other words, what are you expecting the result to be? You're copying the struct (whose values are uninitialized) to strRec assuming that the sizes of the string and the struct are the same. I'm not sure that this is truly the case.
    Finally, where do you assign pbData? You know you can't just write to that pointer without pointing it to a valid buffer, right? So, a proper initialization should look like this:
    Code:
    MyBlob SignedMessageR; ={8, new char[8]} ;
    strcpy(SignedMessageR.pbData, "1234567"};
    Don't forget to delete[] SignedMessageR.pbData later!

    Secondly, make sure that your MyBlob objects are properly initialized. This means: zero initializing them as I've shown above. Only later should you assign the correct DWORD size to the first member and then making sure that the second member points to a valid unsigned char *. That is, the pointer pbData should point to a valid address that is the beginning of an array of bytes with exactly the same size as cbData.

    BTW, are you sure that the MyBlob struct compiled on both ends (the client and the server) has exactly the same binary layout? In other words, is sizeof(MyBlob) the same on both sides?
    Last edited by Danny; 08-30-2008 at 03:08 PM.
    Danny Kalev

Similar Threads

  1. Gradebook program
    By [gx]Shadow in forum Java
    Replies: 5
    Last Post: 10-25-2006, 10:20 PM
  2. Replies: 9
    Last Post: 07-28-2005, 08:40 PM
  3. Replies: 0
    Last Post: 10-11-2002, 10:52 AM
  4. Altering created Distinct Data Type
    By Sindhu in forum Database
    Replies: 0
    Last Post: 12-06-2001, 11:28 AM
  5. Numeric data type
    By Vhost in forum Database
    Replies: 1
    Last Post: 03-28-2001, 08: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