Asymmetric Cryptography in 4 minutes 🔒

Basic Principle

Example

~/secrets# ll
total 0
drwxr-xr-x 1 root root 512 Aug 11 12:49 ./
drwx — — — 1 root root 512 Aug 11 12:49 ../
-rw-r — r — 1 root root 53 Aug 11 12:49 secretHere.txt
~/secrets# cat secretHere.txt
You are cute ^_^
Please, store this message secure.
~/secrets#
~/secrets# ssh-keygen -t rsa -b 4096 -m PEM -f myAwesomeKey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in myAwesomeKey
Your public key has been saved in myAwesomeKey.pub
The key fingerprint is:
SHA256:6TEKIfFgrnl2lCBdksQi8utHX/2/cJpglnITzBV/PIM root@DESKTOP-5CF020O
The key's randomart image is:
+---[RSA 4096]----+
|.+Bo. . |
|+++* . o.. |
|ooo = .E.oo|
| o.o . + . .o|
|o o.o S.+ |
| o.... o.o.o |
| . . ...o B.. . |
| . . . = o.= |
| . o.o. |
+----[SHA256]-----+
~/secrets# ll
total 8
drwxr-xr-x 1 root root 512 Aug 11 12:53 ./
drwx------ 1 root root 512 Aug 11 12:49 ../
-rw------- 1 root root 3243 Aug 11 12:53 myAwesomeKey
-rw-r--r-- 1 root root 746 Aug 11 12:53 myAwesomeKey.pub
-rw-r--r-- 1 root root 53 Aug 11 12:49 secretHere.txt
~/secrets# cat myAwesomeKey.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDStzOPSrLYSLYp8XMIzFNFuft5OyZm7SoaHRQZFMb1B++Wt8UYCCK+lZmNhdoOQwAP4qL1xCk1bp3w3tEARhugqjAOemhk6lLmY/8dCbAEVTkmF4RihPE8LIjMM0HrpPKASm2E97uKt/7AXRDwhzBzfeGdI0CRjLS3ndv2dI1Np0ZlxPUFVEpeK6aMyeVoUVgvjO0brKL2XGR4MKyLfJKX8PFWLomD2KUJtA6StM4MHcBRTBGQ2E03Q201lj47BgLEJthOUAW6iWUlvBqhIY+njYhgafG7Fmz8NtUZjeB1vt//m4NhRudVg9yzndEtR1fqFoUjVXJPr+IMEcqz79C9+tzmBAaD0fiD6yG+pl9aCN3veH1n1/Vo1mJVhr0lWiERMKvhdjD3OBs7z1ndIbyW7nLTgBZXRXxiA7qeKCp+pY+5tGIWb8pgTqcPaHr3hl4T07GB6bST5jY+XUoY3Qy9YMBGlf7Js1DBYXLADZtrQUSPqTHVGMOv0LoICGSwBklCXXV0YCDiD8rZMOFRBAapG9QDjdYYQKRvNa4KK7kK5bUr4op+2kLH1hWEeKKwldZrY+KJJODk+9joYn6xdEtmX36b09SLiPzfJJItNLriZhWtdqgX55YfyCf40qA+Ack+T9gyTc2RP/iJhigSxXRdiQ3Lvk6BFW1/kPZ7y1PSSw== root@DESKTOP-5CF020O
  • myAwesomeKey.pub — the Public key that can be shared with anyone you want.
  • myAwesomeKey — the Private key that should be stored securely and will be used to decrypt received encrypted files.
~/secrets# ssh-keygen -f myAwesomeKey.pub -e -m PKCS8 > myAwesomeKey.public
~/secrets# openssl rsautl -encrypt -pubin -inkey myAwesomeKey.public -in secretHere.txt -out secret
HereEncrypted.txt
~/secrets# ll
total 16
drwxr-xr-x 1 root root 512 Aug 11 13:10 ./
drwx — — — 1 root root 512 Aug 11 12:49 ../
-rw — — — — 1 root root 3243 Aug 11 12:53 myAwesomeKey
-rw-r — r — 1 root root 746 Aug 11 12:53 myAwesomeKey.pub
-rw-r — r — 1 root root 800 Aug 11 13:09 myAwesomeKey.public
-rw-r — r — 1 root root 53 Aug 11 12:49 secretHere.txt
-rw-r — r — 1 root root 512 Aug 11 13:10 secretHereEncrypted.txt
~/secrets# cat secretHereEncrypted.txt
é�B�����%�6t�W4�+C��9�1��l����V|��)����p�D�ϳT�su�75u?I����[6��G�it���u^z��
J�N�:�x����/&��e��̫��N��[<�E���z�#�uS�74Cx˱ ��Ͷ��7>�a��Ӫ���:P���[��!��~�� �٘\$�aך�/~�]Ă��܊t ��5����������X��~�gT���}�~C
�H�G���r:u�[��_��¤ki���76Q���>*:�K�4w.|������\�))1�h��hd`259���R^r C~���+(�4VP�X��Q)�9�H؏�6�^��͂�;��ݳ��4�*:�B��d{
~/secrets# openssl rsautl -decrypt -inkey myAwesomeKey -in secretHereEncrypted.txt -out secretHereDecrypted.txt
~/secrets# ll
total 16
drwxr-xr-x 1 root root 512 Aug 11 13:13 ./
drwx — — — 1 root root 512 Aug 11 12:49 ../
-rw — — — — 1 root root 3243 Aug 11 12:53 myAwesomeKey
-rw-r — r — 1 root root 746 Aug 11 12:53 myAwesomeKey.pub
-rw-r — r — 1 root root 800 Aug 11 13:09 myAwesomeKey.public
-rw-r — r — 1 root root 53 Aug 11 12:49 secretHere.txt
-rw-r — r — 1 root root 53 Aug 11 13:13 secretHereDecrypted.txt
-rw-r — r — 1 root root 512 Aug 11 13:10 secretHereEncrypted.txt
~/secrets# cat secretHereDecrypted.txt
You are cute ^_^
Please, store this message secure.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store