Get RSA keys in a "simple" form

412 views Asked by At

How can I get keys generated by OpenSSL in RAW form? I mean I can't decode my encoded messages in any of online tools. What actions should I do to distribute my keys to other clients (in other apps and web-apps) in proper forms? My generation code is:

void VS_CarrierNet::generateKeys()
{
    m_rsa = RSA_generate_key(KEY_LENGTH, PUB_EXP, nullptr, nullptr);

    // 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, m_rsa, nullptr, nullptr, 0, nullptr, nullptr);
    PEM_write_bio_RSAPublicKey(pub, m_rsa);

    int pri_len = BIO_pending(pri);
    int pub_len = BIO_pending(pub);

    m_privateKey = new char[pri_len + 1];
    m_publicKey = new char[pub_len + 1];

    BIO_read(pri, m_privateKey, pri_len);
    BIO_read(pub, m_publicKey, pub_len);

    m_privateKey[pri_len] = '\0';
    m_publicKey[pub_len] = '\0';

    BIO_free_all(pub);
    BIO_free_all(pri);

    std::cout << "\n\n\n" << m_privateKey << "\n\n\n";
}

The example output is:

-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA1ZI9ucjBwMH2uePmln/xF3mjEIz0WcDQWeI9qZVDXckaFQhr vxDy7GKIuxqEOgcd4zWAGPsyn72i7v2bGxPHXcDdtSqImYk3p2oljBuuQCsbxAxR JXH54D6qUWBMsA5+4K9JVxd//RAhpE/pnXADP6z0O4IP+84m3PtBJjnmaIAOdnx0 iM61E1f7QnuaFWhG/K1lljlXCP30yIanKdr4c2iyF+XSF6SqE7ZcLTmFZAR9v+Gh 13vpMTwEWGJCUhJJN3Y/83VABWS+HGx7R7nI6IzWwxCcpoLxA67x+m/0WwMuiqDe MXIEDL6HMR7L2eEG80ef8+uSyYVFiGGyH+3AUQIBAwKCAQEAjmF+e9sr1dake+1E ZFVLZPvCCwii5oCK5pbTxmOCPoYRY1ryf2Ch8uxbJ2cC0VoT7M5VZfzMan5sn1O8 vLfaPoCTzhxbEQYlGkbDsr0e1XISg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4dioxy3eTHGkzJBvTwfM/rBhY7rQ4X8ly8c/DvQ -----END RSA PRIVATE KEY-----

Should I distribute this key in such form or I need it without additional text and '\n'? I tried remove header, footer, '\n' characters and base64 decode. But this didn't help.

2

There are 2 answers

0
nneonneo On BEST ANSWER

This is the simple form - including the header and footer and extra newlines. Most certificate programs can handle this form just fine.

1
divanov On

Your private key is in PKCS#8 standard form, which is widely used.

You can decrypt RSA messages, for instance, with openssl

openssl rsautl -decrypt -inkey privkey.pem < ciphertex.bin > plaintext.bin

Here privkey.pem is a file containing your private key. ciphertex.bin is a file containing ecnrypted message and plaintext.bin will contain decrypted message.