1

i have to create an aes-256 key and store it in a .pem file. I am using RAND_bytes() to simply create a 256 bit random key. After this how can i save this to a pem file. I have looked at Reading and writing rsa keys to a pem file in C and openssl pem. But i am not working with RSA keys.

I suspect my task is much simpler like create pem from base64 but not much help.

PS: This key will be used to encrypt a self signed digital certificate.

EDIT: looking around more i found that i could use bio to covert to pem, something likePEM_write_bio_PrivateKey but it takes EVP_PKEY format as argument. So how can i convert char buf to EVP_PKEY. i found d2i_PublicKey but it involves RSA and am unsure how RSA would fit into picture here.

Community
  • 1
  • 1
Madhur Rawat
  • 468
  • 1
  • 7
  • 18
  • Given your requirements and looking at the ***BIG*** picture, you typically want to encrypt a file using an [Integrated Encryption Scheme](https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme). As a side note, a *"self signed digital certificate..."* is generally considered public information, and it usually does not have encryption applied to it. – jww Feb 28 '16 at 12:03

2 Answers2

1

AES key is just a random byte array. You can simple store the bytes in a file without any structure.

zakjan
  • 2,391
  • 1
  • 19
  • 29
-1

You can convert aes key to .pem by using PEMWriter class of bouncyCastle library.

http://www.bouncycastle.org/documentation.html

Darshan Patel.
  • 3,132
  • 1
  • 11
  • 10