Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use my ssh-public-key to decrypt a file?

Tags:

I'm trying to find a way to decrypt an encrypted file on a 'virgin' EC2-instance. These EC-instances I use (Ubuntu Lucid) only hold my AWS-created public ssh-key. If can use this to decrypt a file, I can feed it encrypted files (for example a bash-script holding a password to my subversion-repository).

So, my question, can I use my ssh-key to encrypt/decrypt a file?

like image 747
bowlby Avatar asked May 11 '10 14:05

bowlby


People also ask

Can public key be used for decryption?

Public key encryption is also called asymmetric encryption, because the same key cannot be used to encrypt and decrypt the message.

What can someone do with public SSH key?

Anyone with a copy of the public key can encrypt data which can then only be read by the person who holds the corresponding private key. Once an SSH server receives a public key from a user and considers the key trustworthy, the server marks the key as authorized in its authorized_keys file.

Do you decrypt with public or private key?

Data encrypted with the public key can only be decrypted with the private key. Because of this use of two keys instead of one, public key cryptography is also known as asymmetric cryptography. It is widely used, especially for TLS/SSL, which makes HTTPS possible.

Can you encrypt a file with a public key?

Public keys are used for encryption. If someone wants to communicate sensitive information with you, you can send them your public key, which they can use to encrypt their messages or files before sending them to you. Private keys are used for decryption.


1 Answers

The file:

echo 'This is a sekret' >/tmp/msg.txt 

Export public key (in case you don't have it/lose it):

openssl rsa -in ~/private.pem -out /tmp/public.pub -outform PEM -pubout 

Encrypt file with public key (anyone can have this key):

openssl rsautl -encrypt -inkey /tmp/public.pub -pubin -in /tmp/msg.txt -out /tmp/file.enc 

Decrypt the file with private key (only you should have the private key):

openssl rsautl -decrypt -inkey ~/private.pem -in /tmp/file.enc -out /tmp/decrypted.txt 

The decoded message:

cat /tmp/decrypted.txt 
like image 147
Hedgehog Avatar answered Oct 02 '22 04:10

Hedgehog