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?
Public key encryption is also called asymmetric encryption, because the same key cannot be used to encrypt and decrypt the message.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With