2026-07-15

How do I encrypt plaintext with GnuPG?

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How do I encrypt plaintext with GnuPG?

How Do I Encrypt Plaintext with GnuPG? A Developer's Guide

I've been working a great deal with GnuPG lately and have come to depend on its ability to encrypt files, etc. However, I am currently working on a couple of projects that involve communication (i.e., chat, email, etc) where I'd like to use existing keys to encrypt/decrypt text itself as opposed to a "container" such as a file or disk image. I suppose I could encrypt the container, convert the stream to base64 (or something appropriate) then mark the text as such, but I would guess there is a more straightforward solution. Any ideas?

As a senior developer focused on secure communication protocols, I understand this need for direct plaintext encryption. While GnuPG (GPG) excels at encrypting files and messages, applying it directly to continuous data streams requires understanding the underlying mechanics of asymmetric versus symmetric cryptography. The good news is that while GPG doesn't handle raw stream encryption natively in a simple pipeline, we can leverage its robust key management system to achieve exactly what you need by combining it with established symmetric encryption methods.

Understanding the GPG Paradigm

GnuPG primarily operates on asymmetric cryptography. This means you use a public key for encryption and a private key for decryption. When dealing with streams of data (like chat messages), the most efficient approach is usually to use a fast, symmetric algorithm like AES-256 to encrypt the actual plaintext, and then use GPG to securely manage the exchange or wrapping of the symmetric key.

Trying to pipe raw plaintext directly through standard GPG encryption tools often results in complex handling of padding, headers, and metadata that are better managed by dedicated stream ciphers. The most straightforward developer approach involves a two-step process: encrypt the data with symmetry, then use GPG for secure key exchange or final wrapping.

The Practical Workflow: Symmetric Encryption with GPG Wrapping

To encrypt plaintext streams directly using existing keys, you need to generate a temporary, ephemeral symmetric key and then use that key to secure the stream before applying the public-key protection of GPG. This method ensures high performance while maintaining cryptographic integrity.

Here is a conceptual workflow for encrypting a text stream:

Step 1: Generate a Symmetric Key

First, generate a strong random symmetric key (e.g., using OpenSSL or a language library) that will be used to protect the actual message content. This key must be kept secret.

# Example of generating a secure random key for demonstration
openssl rand -base64 32 > session.key

Step 2: Encrypt the Plaintext (Symmetric Layer)

Use this symmetric key to encrypt your plaintext stream using a robust algorithm like AES-256 in GPG's preferred format, or directly via OpenSSL if you are operating outside the strict PGP framework for raw data.

For simplicity and security, we can use GPG’s direct file encryption method on the session key itself if we want to ensure only the intended recipient can unlock it. However, for stream content, a dedicated symmetric cipher wrapped by GPG is often cleaner:

# Using a tool like OpenSSL or a language library to encrypt the text using the session key
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted_stream.bin -pass file:session.key

Step 3: Securely Exchange/Wrap the Session Key (Asymmetric Layer)

Now, instead of encrypting the entire message with a public key (which is slow for large streams), you use GPG to securely transmit only the symmetric session key to the recipient. This leverages your existing public/private key infrastructure.

# Encrypt the session key using the recipient's public key
gpg --encrypt --recipient recipient@example.com session.key > session.key.gpg

The resulting session.key.gpg file is what you transmit. Only the holder of the corresponding private key can decrypt this file and recover the symmetric session key, which then unlocks your streamed data. This strategy keeps the heavy lifting (stream encryption) fast while using GPG for its strength: secure key exchange.

Conclusion and Best Practices

For encrypting communication streams directly with GnuPG, the key is recognizing that GPG is fundamentally a key management tool, not a raw stream cipher. By adopting a hybrid approach—using high-speed symmetric encryption (like AES) for the bulk data and using GPG solely for the secure wrapping and exchange of the session keys—you achieve both speed and robust cryptographic security.

When building secure communication systems, remember that layers are often more powerful than single tools. If you are architecting application-level security features, understanding this separation between symmetric data encryption and asymmetric key wrapping is crucial, much like when designing secure services on platforms like Laravel where strong authentication and authorization are paramount. Mastering these cryptographic principles will make your applications significantly more resilient.

Note: Blog content is currently available in English.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.