First of all, there's no need for the EVP_PKEY*, as PEM_read_RSAPrivateKey() and
RSA_private_encrypt() both use RSA*s.

The reason you're getting the segmentation fault is that you haven't initialized
the output buffer, which must be RSA_size(rsa) bytes.

Please also consider using some form of padding unless you're absolutely
positive you can create a secure padding mechanism in your own application code,
because raw RSA signatures simply aren't secure.

I've thrown together a quick demo of RSA encrypt/decrypt using a pre-stored
digest below:

Regards,

//oscar

<snip>

#include <iostream>

#include "openssl/rsa.h"

using std::cerr;
using std::cout;

const int PADDING = RSA_PKCS1_PADDING;

const unsigned char DIGEST[] = "12345678901234567890";

const unsigned char PRIVATE_KEY[317]={
0x30,0x82,0x01,0x39,0x02,0x01,0x00,0x02,0x41,0x00,0xCA,0xA6,0xBB,0x67,0x50,0x5B,
0x2E,0x1A,0x52,0x30,0x29,0xBA,0x2B,0x21,0x87,0x63,0xD6,0x28,0x43,0x9D,0x82,0x2B,
0xBC,0x66,0x87,0x88,0xF6,0x08,0xB4,0x0B,0x4D,0x4D,0xF9,0xCD,0x8A,0x20,0x29,0x71,
0x0C,0x43,0x4B,0xF6,0xF4,0xDF,0x70,0x9C,0x74,0xA2,0x0F,0xC9,0xE9,0xDA,0x26,0x1E,
0x15,0x39,0x43,0x39,0x94,0xD7,0x01,0xD8,0x84,0xF9,0x02,0x03,0x01,0x00,0x01,0x02,
0x40,0x19,0x1A,0xF4,0xC6,0x84,0xCE,0xF6,0xF4,0x2D,0xD0,0x67,0xAA,0xF7,0xA2,0xEC,
0xB9,0xF2,0x93,0x04,0x65,0x63,0xF5,0xF5,0x1E,0x22,0x96,0x56,0x1C,0x97,0xE9,0x7A,
0x87,0x00,0xC0,0x47,0xBF,0x96,0x6B,0x82,0xD8,0x29,0xE7,0x9D,0x07,0xCC,0x27,0x83,
0x14,0xB3,0x50,0x5D,0x20,0xF2,0x30,0x2F,0xBA,0x9D,0x5C,0x86,0xA1,0x52,0xB2,0x9B,
0xED,0x02,0x21,0x00,0xFB,0x78,0xE6,0xFB,0xC3,0x43,0xBD,0xB1,0x9E,0xBB,0x4F,0x3A,
0x2D,0xAE,0x67,0x4F,0x2F,0xF1,0x13,0x73,0x60,0x3B,0xC2,0xC4,0x62,0xA8,0x67,0x05,
0x89,0xC4,0x56,0x97,0x02,0x21,0x00,0xCE,0x4C,0xCD,0x45,0x90,0x7D,0x0A,0x58,0xA5,
0x22,0xF5,0xD8,0xEF,0x54,0xDD,0xFD,0x31,0x39,0xF8,0x71,0xF4,0xDC,0x6D,0xAC,0x8E,
0xDD,0xAC,0x63,0x6A,0x65,0x82,0xEF,0x02,0x20,0x07,0x09,0x89,0x1D,0x3E,0x87,0xA4,
0xC5,0x55,0xB5,0xD4,0x2F,0xC2,0x9B,0x80,0xD4,0x9C,0x78,0xD8,0x9C,0x7F,0xF2,0xEC,
0x2E,0x66,0xBC,0x63,0xB5,0xA9,0xF7,0x4E,0x9D,0x02,0x20,0x59,0x30,0x10,0xEA,0x8B,
0x2D,0xD3,0xC4,0x41,0x18,0x5D,0x2A,0xFD,0x45,0x36,0x77,0x98,0xC9,0x94,0x24,0x48,
0x14,0xE8,0x01,0x5A,0xFE,0x70,0xE1,0xC6,0x04,0xBE,0xD7,0x02,0x20,0x6B,0x3D,0xF8,
0xB8,0xE5,0xA4,0x65,0x79,0xA8,0xBF,0x72,0x03,0xCC,0x59,0x4E,0xB8,0xB3,0xCA,0xF8,
0xBB,0xCF,0xC1,0x8F,0x75,0xA1,0x57,0x53,0x34,0x07,0x35,0x50,0x96,
};

int main(int argc, char* argv)
{
  const unsigned char *myKey = PRIVATE_KEY;

  RSA *privateKey = d2i_RSAPrivateKey(0, &myKey, sizeof(PRIVATE_KEY));

  int len = RSA_size(privateKey);

  unsigned char* enc = new unsigned char[len];

  int ret = RSA_private_encrypt(sizeof(DIGEST), DIGEST, enc, privateKey,
PADDING);
  if (-1 == ret)
  {
    cerr << "Error encrypting.\n";
    return -1;
  }

  int outlen = (RSA_size(privateKey) - 11);

  unsigned char* dec = new unsigned char[outlen];

  ret = RSA_public_decrypt(ret, enc, dec, privateKey, PADDING);
  if (-1 == ret)
  {
    cerr << "Error decrypting.\n";
  }

  ret = memcmp(DIGEST, dec, ret);
  if (0 != ret)
  {
    cerr << "Decrypted data differs from original plaintext.\n";
    return -1;
  }

  cout << "Success.\n";

  delete [] enc;
  delete [] dec;

  RSA_free(privateKey);

  return 0;
}
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]