title: Separating public and private keys from RSA keypair variable
body: <p>As the title says, I have some code that generates a pair of RSA keys. I want to split them apart and use them individually to encrypt/decrypt, rather than use the variable "keypair" to encrypt, and decrypt.</p>
  
  <p>I am working to transfer data across a network, and want to encrypt it using simple RSA encryption. Therefore i want to send the public key over to a different user, so he can use it to encrypt some data, and then send it back to me.</p>
  
  <p>Here is the code that generates the keys:</p>
  
  <pre><code>//Generate key pair
      RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
  </code></pre>
  
  <p>I now want to separate the public from the private key, so i can use them independently to encrypt and decrypt data. How can i do that?</p>
  
  <p>I've got some code that takes the "keypair" and extracts some information into some "BIO" variables, although i am not sure how that would help me:</p>
  
  <pre><code>// To get the C-string PEM form:
  BIO *pri = BIO_new(BIO_s_mem());
  BIO *pub = BIO_new(BIO_s_mem());
  
  PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
  PEM_write_bio_RSAPublicKey(pub, keypair);
  
  pri_len = BIO_pending(pri);
  pub_len = BIO_pending(pub);
  
  pri_key = (char*)malloc(pri_len + 1);
  pub_key = (char*)malloc(pub_len + 1);
  
  BIO_read(pri, pri_key, pri_len);
  BIO_read(pub, pub_key, pub_len);
  
  pri_key[pri_len] = '\0';
  pub_key[pub_len] = '\0';
  
  #ifdef PRINT_KEYS
      printf("\n%s\n%s\n", pri_key, pub_key);
  #endif
  printf("done.\n");
  </code></pre>
  
  <p>This code works, since i've tested it in visual studio 2012.
  Any ideas on how to separate the keys, then maybe put them back together in a "keypair" or maybe how to use them separately to encrypt/decrypt some string variables?</p>
  
  <p>Thank you</p>
  
  <p>(FULL CODE)</p>
  
  <pre><code>#include &lt;openssl/rsa.h&gt;
  #include &lt;openssl/pem.h&gt;
  #include &lt;openssl/err.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define KEY_LENGTH  2048
  #define PUB_EXP     3
  #define PRINT_KEYS
  #define WRITE_TO_FILE
  
  int main() {
      size_t pri_len;            // Length of private key
      size_t pub_len;            // Length of public key
      char   *pri_key;           // Private key
      char   *pub_key;           // Public key
      char   msg[KEY_LENGTH/8];  // Message to encrypt
      char   *encrypt = NULL;    // Encrypted message
      char   *decrypt = NULL;    // Decrypted message
      char   *err;               // Buffer for any error messages
  //Generate key pair
      RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
  
  // To get the C-string PEM form:
      BIO *pri = BIO_new(BIO_s_mem());
      BIO *pub = BIO_new(BIO_s_mem());
  
      PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
      PEM_write_bio_RSAPublicKey(pub, keypair);
  
      pri_len = BIO_pending(pri);
      pub_len = BIO_pending(pub);
  
      pri_key = (char*)malloc(pri_len + 1);
      pub_key = (char*)malloc(pub_len + 1);
  
      BIO_read(pri, pri_key, pri_len);
      BIO_read(pub, pub_key, pub_len);
  
      pri_key[pri_len] = '\0';
      pub_key[pub_len] = '\0';
  
      #ifdef PRINT_KEYS
          printf("\n%s\n%s\n", pri_key, pub_key);
      #endif
      printf("done.\n");
  
  // Get the message to encrypt
      printf("Message to encrypt: ");
      fgets(msg, KEY_LENGTH-1, stdin);
      msg[strlen(msg)-1] = '\0';
  
  // Encrypt the message
      encrypt = (char*)malloc(RSA_size(keypair));
      int encrypt_len;
      err = (char*)malloc(130);
      if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt, keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {
          ERR_load_crypto_strings();
          ERR_error_string(ERR_get_error(), err);
          fprintf(stderr, "Error encrypting message: %s\n", err);
          goto free_stuff;
      }
  
  // Decrypt it
  decrypt = (char*)malloc(encrypt_len);
  if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, keypair, RSA_PKCS1_OAEP_PADDING) == -1) {
      ERR_load_crypto_strings();
      ERR_error_string(ERR_get_error(), err);
      fprintf(stderr, "Error decrypting message: %s\n", err);
      goto free_stuff;
  }
  printf("Decrypted message: %s\n", decrypt);
  
  getchar();
  
  //printf("%s", pub_key);
  
  free_stuff:
  RSA_free(keypair);
  BIO_free_all(pub);
  BIO_free_all(pri);
  free(pri_key);
  free(pub_key);
  free(encrypt);
  free(decrypt);
  free(err);
  }
  </code></pre>
  
  <p>Found this code here : <a href="https://shanetully.com/2012/04/simple-public-key-encryption-with-rsa-and-openssl/" rel="nofollow">https://shanetully.com/2012/04/simple-public-key-encryption-with-rsa-and-openssl/</a></p>