Thread: How to set RSA key in openssl

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

    How to set RSA key in openssl

    Hi all,

    I want to assign the Hex string to the RSA *rsa, how can I do it, many thanks!!

    the Hex String is in 1024bit.
    for example:
    67BD857FB1FDA1AF92FBE2AD43BD82D2
    68BC129DBA16176BB4244745831EF09D
    46A8182C7546BA6B55A2EC9E86DE036E
    91070CD4C1CE1DDFC85F93B3D58EEB1D
    B131615A962721E1AD69C866DB88178C
    AF2310A44E8942B3020235327334F834
    9315E49FFD023F0E2F769AA805D23CEB
    1C2716F331FDACB7F0BA68D7EBD70F48




    Code:
    int main(int argc, char** argv) {
        RSA* rsa;
        unsigned char* input_string;
        unsigned char* encrypt_string;
        unsigned char* decrypt_string;
        int i;
     
        // check usage
        if (argc != 2) {
            fprintf(stderr, "%s <plain text>\n", argv[0]);
            exit(-1);
        }
     
        // set the input string
        input_string = (unsigned char*)calloc(strlen(argv[1]) + 1,
                sizeof(unsigned char));
        if (input_string == NULL) {
            fprintf(stderr, "Unable to allocate memory for input_string\n");
            exit(-1);
        }
        strncpy((char*)input_string, argv[1], strlen(argv[1]));
     
        // Generate RSA parameters with 1024 bits (using exponent 3)
        rsa = RSA_generate_key(1024, 3, NULL, NULL); // <----- 
        
     
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Use scanf with the %x format specifier, it reads hexadecimal numbers. You may want something like %2hhx. The 2 grabs only 2 hex digits at a time, and the "hh" and read them into an unsigned char instead of an unsigned int.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    can I coding like this:

    Code:
    unsigned char* key="67BD857FB1FDA1AF92FBE2AD43BD82D268BC129DBA16176BB4244745831EF09D46A8182C7546BA6B55A2EC9E86DE036E91070CD4C1CE1DDFC85F93B3D8EEB1DB131615A962721E1AD69C866DB88178CAF2310A44E8942B3020235327334F8349315E49FFD023F0E2F769AA805D23CEB1C2716F331FDACB7F0BA68D7EBD70F48";
    
    //rsa = RSA_generate_key(1024, 3, NULL, NULL); // <----- 
    rsa= key;

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    what data type does your rsa key need to be in?
    if it needs to be a string, then your unsigned char *key = ... would work. then it would be hard coded in your program if thats what you want..
    if it needs to be an array of binary bytes and you are reading it in from an ascii file or stdin, then what anduril said.
    if it needs to be an array of binary bytes and you want to hard code it, then do something like
    Code:
    unsigned char key[] = {0x67,0xbd,0x85...};

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    I don't know the data type of "rsa"

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by homoon View Post
    I don't know the data type of "rsa"
    Well, it is type "pointer to RSA". I don't know what type RSA is however. It is either something you defined, or something from a third party library that you are using. If it's from a library, you need to look up the documentation for that library.

    What do you need input_string for if you're going to generate a key anyway?

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    Thx andul462, I dont need to generate the RSA Key in random,
    //rsa = RSA_generate_key(1024, 3, NULL, NULL);

    therefore, I don't need the RSA_generate_key function. I just want to assign the Hex String to the rsa variable, but I don't know how to manual assign it.
    Last edited by homoon; 04-27-2012 at 11:10 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well neither do we. As I said, you either wrote the RSA stuff yourself (not likely, since you don't know how to use it), or it's some third party. We don't know what that third party library is. There's not one and only one RSA/crypto library, so we can't help you. You need to figure out which library you're using and read the documentation. Maybe it provides a function like RSA_key_from_hex_string. Maybe not. Maybe it provides some other function, like RSA_key_from_base64, so you need to convert your hex string to base64 or some other format before using it. But we can't help, since we don't know what library you're using.

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    The OP is likely making use of the openssl lib. To the OP, have you read up on : OpenSSL: Documents, RSA_generate_key(3)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openssl decryption
    By kakashi316 in forum C Programming
    Replies: 1
    Last Post: 03-24-2012, 11:02 AM
  2. openssl/RAND_bytes
    By kakashi316 in forum C Programming
    Replies: 5
    Last Post: 03-21-2012, 09:37 AM
  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