I am very not experienced with certs and keys. I have to store a private key for a third party API in my database. I have a key to encrypt that. This is what I did in PHP
$private_key = "----BEGIN PRIVATE KEY---\nABCDBLAH\n---END PRIVATE KEY----\n"
// The encryption and storing part - 1
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($data, 'aes-256-cbc', 'mykey', 0, $iv);
$encrypted_data = $encrypted .':'base64_encode($iv);
//store in the database
//retrieve later
$parts = explode(':',$encrypted_data);
$private_key= openssl_decrypt($parts[0], 'aes-256-cbc', 'mykey', 0, base64_decode($parts[1]));
// 2
Use in the API.
If I ignore the part between 1 and 2 for storing and retrieving the key from the database, the API and everything works correct which means there is something wrong in storing and retrieving the key. What I am I doing wrong here? If this is not the right way, what way should I use? Please help me.
Note: /n part in the private key. If I don't encrypt, /n doesn't matter and the API works.