How to encode a SSH private key and then Decode it
Asked Answered
D

2

10

I am passing private-key as a gitlab environment variable. But when I echo it, it is altered, it is not really in the format of:

-----BEGIN RSA PRIVATE KEY----- 
xxxxxxx 
-----END RSA PRIVATE KEY-----

So I think the solution must be to encode it again and then decode it with base64

How can I do this with bash shell?

What I tried is:

encode ./private-key 
bash6d -d ./private-key

I have the doubt that this is not the way. Can someone please help me?

Dalmatia answered 15/3, 2020 at 16:12 Comment(0)
B
13

I prefer using openssl:

openssl enc -base64 -in my.key -out my.key.base64

Then, to decode it:

openssl enc -d -base64 -in my.key.base64 -out my.key

You can leave out the -out part if you want to see the resut on stdout.
And you can use -a instead of -base64 (same option, shorter)

To test it:

openssl enc -a -in my.key | openssl enc -a -d
Buttaro answered 17/3, 2020 at 5:25 Comment(0)
T
5

May be a delayed response, but hope it helps others. I had a similar situation of encoding the ssh private key, embed into kubernetes secret and using that in the mounted file inside the POD. Did as like below.

If you are using the linux environment, can encode the data with below command.

$ cat private_key.pem|base64
{Encoded_Data}

This output copied into secret and when mounted, I can find the decoded data was already with the same as it was encoded. But, in case if you want to decode this data, you can do with below command.

$cat encoded_private_key.pem|base64 --decode
Toccaratoccata answered 22/9, 2021 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.