Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to use GnuPG for encrypting files on MacOS
Efe Ertugrul
Efe Ertugrul

Posted on • Edited on

     

How to use GnuPG for encrypting files on MacOS

GnuPG is an implementation of OpenPGP standard.
People use it for public-private key encryption.
It is one of the tools that Edward Snowden used to uncover the secrets of the NSA.

GnuPG is a complex tool.
I will only show you how to use it for file encryption without using keys.
And i will show some configuration files to make commands more simple.

I assume you know how to use a Unix console and have Homebrew package manager installed.

First you should install GnuPG withHomebrew:

brew install gnupg
Enter fullscreen modeExit fullscreen mode

This will install GnuPG version 2.2.19 (or later)

Check installation:

gpg --version
Enter fullscreen modeExit fullscreen mode

You should be able to see something like this:

gpg (GnuPG) 2.2.19libgcrypt 1.8.5Copyright (C) 2019 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.
Enter fullscreen modeExit fullscreen mode

Now we can use GnuPG.

I have a text file namedtest.txt.
It contains this text:

this is a test file
Enter fullscreen modeExit fullscreen mode

To encrypttest.txt file i will use this command:

gpg -c --armor --cipher-algo AES256 --no-symkey-cache --output test.asc test.txt
Enter fullscreen modeExit fullscreen mode

This command will ask you a password and create an encrypted version oftest.txt file and save astest.asc file.

Command explanation:

-c means use symmetric cipher so you will enter a password for that file.
--armor is for a readable ascii output so you can easily copy/paste it.
--cipher-algo AES256 is for using AES-256 cipher. (U.S. government using it so why not)
--no-symkey-cache means GnuPG will not remember password. If you don't enter this--decrypt command won't ask your passphrase.
--output test.asc means save encrypted file astest.asc

Now i havetest.asc (encryptedtest.txt).
It contains this text:

-----BEGIN PGP MESSAGE-----jA0ECQMCFBL2lERVNBzj0kwBXxdKtTQSCu4aHyiP93EfUjqYX+Qsp6sWAF+RHUMWrqjQiLMSlSrxnBxG0E+qfoTmN+26Qb0qd9XAY7S3OTQTfi6XyvjjrNr0yiJ9=r3J6-----END PGP MESSAGE-----
Enter fullscreen modeExit fullscreen mode

As you can see it is readable but meaningless.
This is because of the--armor option we added to the command.

To decrypttest.asc file i will use this command:

gpg --decrypt --no-symkey-cache --output test1.txt test.asc
Enter fullscreen modeExit fullscreen mode

This command will ask you the password you used and if it is correct it will create a decrypted file astest1.txt.

Now i havetest1.txt.
It contains this text:

this is a test file
Enter fullscreen modeExit fullscreen mode

Configuration Files

There are some configuration files im using.
I will show you how to create these files.
These configuration files are not neccessary but they are shortening the commands i use everyday.

GnuPG creates a folder for itself.
It is normally in your$HOME folder named.gnupg.
It contains caches, your keyrings, your configuration files.
So go there and create a file namedgpg.conf.(if it doesn't exists)

Write these ingpg.conf:

armorpersonal-cipher-preferences AES256verboseuse-embedded-filename
Enter fullscreen modeExit fullscreen mode

Save it.

Now create another configuration file namedgpg-agent.conf.(gpg-agent comes with gnupg installation)

Write these ingpg-agent.conf:

default-cache-ttl 0max-cache-ttl 0disable-scdaemon
Enter fullscreen modeExit fullscreen mode

default-cache-ttl 0 andmax-cache-ttl 0 disables password cache.disable-scdaemon disables smart card daemon program. Smart Card program starts automatically whenevergpg-agent starts. I don't use smart cards so i'm disabling it.

Save it.

Now actually you should restart yourgpg-agent program manually.

Kill it with this command:

gpgconf --kill gpg-agent
Enter fullscreen modeExit fullscreen mode

It will launch automatically when you callgpg.
If it doesn't you can run this command to launch it:

gpgconf --launch gpg-agent
Enter fullscreen modeExit fullscreen mode

Anyway now we don't need to add any options. We can simply run our command like this:

gpg -c test.txt
Enter fullscreen modeExit fullscreen mode

This command will automatically create a file namedtest.txt.asc withcipher aes-256, also inascii format and won't remember thepassword.

To decrypt it simply enter this command:

gpg -d test.txt.asc
Enter fullscreen modeExit fullscreen mode

It will create a decrypted file astext.txt.

This is one of the many ways to use GnuPG.
As i said before it does so much more.
You can look for more info here atGnuPG

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Apple Developer
  • Joined

More fromEfe Ertugrul

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp