Thread: How to gen 32bytes hex string by openssl

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    How to gen 32bytes hex string by openssl

    Hi all, How to gen 32bytes hex string by openssl, I try to use RAND_byte, which is not in hex format, many thanks!!

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What do you mean by "not in hex format"? Do you have some example code and output?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    for example:
    unsigned char key[49];
    RAND_bytes(key,48);
    printf("Key:%s\n",key);

    the outcome like this:
    Key:\360\330{\217\226\227.5\340!;\211b\302\353\233 \3640\304\346\337Z>\222\2257\227;\35560\225\262\25 0\334gyyX\340\377\277g#M\220\274\321

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That's just a matter of how it's being displayed. Try this:
    Code:
    #define KEY_SIZE 48
    int i;
    unsigned char key[KEY_SIZE];
    RAND_bytes(key, KEY_SIZE);
    for (i = 0; i < KEY_SIZE; i++)
        printf("%02X ", key[i]);
    EDIT:
    If you actually need a string representation of the hex digits you can use sprintf:
    Code:
    #define KEY_SIZE 48
    int i;
    unsigned char key[KEY_SIZE];
    char keystr[KEY_SIZE * 2 + 1];
    RAND_bytes(key, KEY_SIZE);
    for (i = 0; i < KEY_SIZE; i++)
        sprintf(keystr + 2*i, "%02X", key[i]);
    printf("%s\n", keystr);
    Last edited by oogabooga; 04-05-2012 at 11:51 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    Oh!!!!! You are great!!! thx a lot!!!!

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    thx oogabooga, I want to ask 1 more question, Can I change this result to Charater pointer? (Char array (HEX) to Char * (HEX))

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    An array is implictly treated as a pointer to its first element when you pass it to the function. You don't need to do anything.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openssl/RAND_bytes
    By kakashi316 in forum C Programming
    Replies: 5
    Last Post: 03-21-2012, 09:37 AM
  2. Openssl Pipes with C Help?
    By Sundown G in forum C Programming
    Replies: 3
    Last Post: 02-13-2012, 06:38 PM
  3. Need help with OpenSSL
    By Ricardo_R5 in forum C Programming
    Replies: 0
    Last Post: 05-07-2007, 06:18 PM
  4. Using OpenSSL with Dev-C++
    By Smiley10 in forum C Programming
    Replies: 2
    Last Post: 07-08-2006, 10:27 AM
  5. OpenSSL and Win32 SSL API :: SSL/TLS
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 03-10-2004, 07:46 PM