---
description: Encryption
---

# Elfcryption

## Symmetric Encryption

Symmetric encryption is where we use the same key to encrypt and decrypt data.

We can use `gpg` to encrypt a file, using the command `gpg -c file.txt` and typing the password when prompted, creating the file `file.txt.gpg`. This file is encrypted and the data is all scrambled. GPG uses the AES algorithm to encrypt the files.

Once encrypted, to decrypt the file we use the command `gpg -d file.txt.gpg` and type the password (key) used to encrypt it.

To check the integrity of a file, we can see it hash value with `md5sum file.txt`. If the hash value is different from the one when the file was first created, it means the file was modified, if not it means it is the original file.

## Asymmetric Encryption

Asymmetric Encryption uses a public and a private key. If we encrypt data with someone else's public key, it can only be decrypted with that persons private key.

SSH key uses public and private keys. We generate a private key, and with that we have a public key generated also. The we place our public key onto the server, then when we want to SSH into a machine, we use our private key to authenticate ourselves as if the server can successfully decrypt our message with the public key.

So, if we use a public key to encrypt a message, it can only be decrypted with our private key. If we use a private key to encrypt a message, it can only be decrypted with our public key.

To generate a private key we use the following command (8912 creates the key 8912 bits long):

&#x20;   `openssl genrsa -aes256 -out private.key 8912`\


To generate a public key we use our previously generated private key:

&#x20;   `openssl rsa -in private.key -pubout -out public.key`\


Lets now encrypt a file (plaintext.txt) using our public key:

&#x20;   `openssl rsautl -encrypt -pubin -inkey public.key -in plaintext.txt -out encrypted.txt`\


Now, if we use our private key, we can decrypt the file and get the original message:

&#x20;   `openssl rsautl -decrypt -inkey private.key -in encrypted.txt -out plaintext.txt`

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7ZnkesuN3g_prZlrmq%2Fimage.png?alt=media\&token=a8771119-f772-4cce-bfd5-9f299053e10f)

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7ZnziW0W7XLJzs-QJ5%2Fimage.png?alt=media\&token=ef9e16e4-766c-4a8f-81da-03adf092b7bd)

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7Zo3QkM0-huukr4uJJ%2Fimage.png?alt=media\&token=fc81d582-ffca-4123-9e6f-73b3b7c62737)

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7ZoDZTEKzP7D5BdPr_%2Fimage.png?alt=media\&token=e72c3d20-cae4-47f1-a76c-b860c4df4697)

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7ZoHlO31a9qVYZS3d-%2Fimage.png?alt=media\&token=51c532b3-4f85-4345-81e1-b3203e7a731f)

![](https://3889206050-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M6DIEHtstePxj4NCmCC%2F-M7Z-49hT6pQDJ3iUA63%2F-M7ZoZExSUSUk-kzELZA%2Fimage.png?alt=media\&token=64e385c5-1465-4e1d-833e-a6781e46a61c)

