6

i have ssh-key, something like this

-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----

Of course this key does not work. When i am doing manual things, something, like this

-----BEGIN RSA PRIVATE KEY----- 
my_super_secret_password 
-----END RSA PRIVATE KEY-----

It works. When i am deleting this -----BEGIN RSA PRIVATE KEY----- and this -----END RSA PRIVATE KEY-----, my ssh-key does not work.

So, the question. How, i can make automatically via some command, like sed or awk, or any other command, how can i make from this string

-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----

these three strings

-----BEGIN RSA PRIVATE KEY----- 
my_super_secret_password 
-----END RSA PRIVATE KEY-----

Thanks for your help. If you know any other answer on this question, i am glad to hear you. The reason, why i need it, because i have secret keys storage in AWS Secret Manager. So, this manager stores keys only in one line.

Piduna
  • 541
  • 4
  • 12
  • 25
  • Possible duplicate of [openssh - Adding an ssh key from putty to authorized\_keys](https://serverfault.com/questions/797044/openssh-adding-an-ssh-key-from-putty-to-authorized-keys) – Craig Watson Aug 07 '19 at 20:33

2 Answers2

16

I would personally base64-encode the key, store it, then base64 decode it when you need it.

Encode:

echo "-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----" | openssl base64 | tr -d '\n'

Decode:

echo "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLSBteV9zdXBlcl9zZWNyZXRfcGFzc3dvcmQgLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0K" | openssl base64 -A -d
Craig Watson
  • 9,575
  • 3
  • 32
  • 47
1

Something like this, quick and hacky.

echo "-----BEGIN RSA PRIVATE KEY----- my_super_secret_password -----END RSA PRIVATE KEY-----" | sed 's/\(KEY----- \)/\1\n/' | sed 's/\(-----END\)/\n\1/'
Sirex
  • 5,499
  • 2
  • 33
  • 54