da_miao_zi
da_miao_zi

Reputation: 393

How to store a private key in string literal in C?

How to store a private key in string literal in C?

e.g.

-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBANuwDJCXuv/FszouyCcDNIn6++/8EtdVpzvLKpqLdK/fq6Y3bIjD
6fPVCNwANeJsSHKexi4qPkBpZ8/C0Ssnp48CAwEAAQJAHtDmIk42W/R2fJ3dU6Oe
dhBv0K0SU4RfTgLkugL/3j+GIQCiFXBkfO/eG6J8vQlOQaGbK7Zu6bSnCoCdiDmR
sQIhAPysf1AIbRoQGO6HYtAoTUOMlih7dyh4O3jKCqQUa60XAiEA3pRju0/0g2JK
bdSct/kqii1o+RydH79WCnKVgVvulEkCIFdbRADliOLT4erFv/H7nQrXliqU1ROW
zejq4VbtAHUtAiBRP5OkRYx5BUEsFGdc1MyNggQGo0ZL13ld+PPQM7HEWQIhAIU1
InlMgmqV1GzEXsAwRH5RH2zEqAe9mKkU7SZWT0rI
-----END RSA PRIVATE KEY-----
  1. Can I remove newlines in each line?
  2. What is the best method to define multiple lines string literal in C?

Upvotes: 1

Views: 701

Answers (1)

Blaze
Blaze

Reputation: 16876

Disregarding the security concerns associated with such a "solution", you could do this:

const char* key =
    "MIIBOgIBAAJBANuwDJCXuv/FszouyCcDNIn6++/8EtdVpzvLKpqLdK/fq6Y3bIjD"
    "6fPVCNwANeJsSHKexi4qPkBpZ8/C0Ssnp48CAwEAAQJAHtDmIk42W/R2fJ3dU6Oe"
    "dhBv0K0SU4RfTgLkugL/3j+GIQCiFXBkfO/eG6J8vQlOQaGbK7Zu6bSnCoCdiDmR"
    "sQIhAPysf1AIbRoQGO6HYtAoTUOMlih7dyh4O3jKCqQUa60XAiEA3pRju0/0g2JK"
    "bdSct/kqii1o+RydH79WCnKVgVvulEkCIFdbRADliOLT4erFv/H7nQrXliqU1ROW"
    "zejq4VbtAHUtAiBRP5OkRYx5BUEsFGdc1MyNggQGo0ZL13ld+PPQM7HEWQIhAIU1"
    "InlMgmqV1GzEXsAwRH5RH2zEqAe9mKkU7SZWT0rI";

The pointer key now points to the private key which is embedded in the executable. It doesn't include the newlines because those string literals are automatically concatenated at compile time, no matter what whitespace is between them.

It has been said already, but I'd like to reiterate that something like that shouldn't be done with any keys and data that matters.

Upvotes: 6

Related Questions