Get a Pentest and security assessment of your IT network.

Cyber Security

Encrypting Text Documents: A Practical Guide

TL;DR

Yes, structured text documents can be effectively encrypted. Choose a strong encryption algorithm (like AES), use a robust key management system, and consider the specific needs of your document (e.g., whether you need to search within it while encrypted). This guide covers practical steps for encrypting files on Linux/macOS using OpenSSL and GPG.

1. Understanding Encryption Basics

Encryption transforms readable data (plaintext) into unreadable data (ciphertext). Decryption does the reverse, requiring a key. Key strength is vital – longer keys are harder to crack. Common algorithms include:

  • AES: Advanced Encryption Standard – widely used and considered secure.
  • GPG: GNU Privacy Guard – often used for email encryption but works with files too.

There are two main types of encryption:

  • Symmetric-key encryption: Uses the same key for both encryption and decryption (faster).
  • Asymmetric-key encryption: Uses a pair of keys – a public key for encryption and a private key for decryption (slower, but useful for secure exchange of symmetric keys).

    2. Encrypting with OpenSSL (Symmetric Encryption)

    OpenSSL is a powerful command-line tool available on most Linux/macOS systems.

    1. Generate an encryption key: This creates a secret key that will be used to encrypt and decrypt the file.
    2. openssl rand -base64 32 > my_encryption_key.txt

      This command generates a 32-byte (256-bit) random key and saves it to my_encryption_key.txt. Keep this file *very* safe!

    3. Encrypt the file:
    4. openssl enc -aes-256-cbc -salt -in my_document.txt -out my_document.enc -k "your_password"

      Replace my_document.txt with the name of your file and your_password with a strong password. The -salt option adds a random salt to improve security.

    5. Decrypt the file:
    6. openssl enc -aes-256-cbc -d -salt -in my_document.enc -out my_document_decrypted.txt -k "your_password"

      Replace my_document.enc with the encrypted file name and your_password with the password you used for encryption.

    3. Encrypting with GPG (Asymmetric Encryption)

    GPG uses public/private key pairs. You’ll need to generate a key pair first if you don’t have one already.

    1. Generate a GPG key pair:
    2. gpg --gen-key

      Follow the prompts to create your key. This process takes some time and requires careful handling of your passphrase.

    3. Encrypt the file using a recipient’s public key: You need the public key of the person you want to be able to decrypt the file.
    4. gpg --encrypt --recipient "[email protected]" my_document.txt

      Replace [email protected] with the email address associated with the recipient’s public key.

    5. Decrypt the file:
    6. gpg --decrypt my_document.txt.gpg > my_document_decrypted.txt

      You will be prompted for your private key passphrase.

    4. Key Management

    Your encryption is only as strong as your key management. Consider these points:

    • Secure storage: Protect your keys (both symmetric and private keys) from unauthorized access.
    • Key rotation: Regularly change your keys to minimize the impact of a potential compromise.
    • Backup: Back up your keys securely, but separately from the encrypted data.

    5. Encrypting Structured Documents

    For structured documents (like JSON or XML), encryption works the same way as with plain text files. However, searching within an encrypted document is not possible without decrypting it first.

    • Full-file encryption: The entire file is encrypted, making it unreadable until decrypted.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation