Skip to content
daniloritarossiPublic

About

Stand alone software developed in GO Language, allows you to uniquely encrypt the string of the application based on keywords: Special key+Machine ID+OS

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

52 Commits

Folders and files

Repository files navigation

ENCDEC logo

Go 1.17+ Release v2.0.7 License MIT Platform: Windows, macOS, Linux

Cipher: AES-256-GCM KDF: Argon2id and SHA-256 Modes: machine-bound and passphrase 100% offline

ENCDEC (encrypt/decrypt string)

CLI tool written in Go to encrypt/decrypt strings using AES-GCM.

It supports two modes:

  1. Machine-bound mode (default): the encryption key is deterministically derived from:

    • a secret prefix (configurable via env var)
    • Machine ID
    • OS
      This means ciphertext is typically decryptable only on the same machine.
  2. Passphrase mode (portable, optional): the encryption key is derived from a passphrase using Argon2id with a random salt. Ciphertext is self-contained and can be decrypted on any machine as long as you have the passphrase.

Security note: this is not a password hashing tool. If you need to store user passwords, use a dedicated password hashing algorithm (Argon2id / bcrypt / scrypt).


Features

  • Cross-platform (Windows / macOS / Linux)
  • Authenticated encryption: AES-GCM
  • Two modes:
    • Machine-bound key derivation (SHA-256, 32 bytes → AES-256)
    • Passphrase-based derivation (Argon2id + random salt, AES-256)
  • Optional file logging (best-effort; failures do not stop the CLI)
  • Input size limits (default 1 MiB) to mitigate trivial local DoS

Requirements

  • Go 1.17+ (as per go.mod)

Dependencies are managed via Go modules (go.mod).


Project layout

  • main.go: CLI entrypoint and command dispatch
  • lib/lib.go: crypto and key-derivation library (AES-GCM, machine-bound key, passphrase key)

Usage

The CLI expects 2 arguments after the program name:

usage: encdec ENC <plain-text> | encdec DEC <hex-ciphertext> | encdec ENCPASS <plain-text> | encdec DECPASS <p1:ciphertext>

Accepted operations:

  • ENC encrypt (machine-bound mode)
  • DEC decrypt (machine-bound mode)
  • ENCPASS encrypt (passphrase mode, requires ENCDEC_PASS)
  • DECPASS decrypt (passphrase mode, requires ENCDEC_PASS)

If an error occurs, the program prints the error on stderr and exits with code 1.


Installation / Run

From the module folder (encdec/):

go mod tidy

Run directly with go run

Examples below use go run . ... but the same arguments work with the built binary.


Encrypt / Decrypt (machine-bound)

Encrypt

go run . ENC 'PasswordToEncrypt'

Output:

encrypted : <hex-ciphertext>

Notes:

  • The output ciphertext is hex-encoded.
  • Internally the ciphertext contains the nonce prepended to the AES-GCM output (this is handled transparently by the tool).

Decrypt

go run . DEC <hex-ciphertext>

Output:

PasswordToEncrypt

Notes:

  • Decryption succeeds only if performed on the same machine (same Machine ID + OS) and with the same ENCDEC_SECRET_PREFIX used at encryption time.

  • If the Machine ID is empty or uninitialized the tool refuses to run:

    machine id: empty or uninitialized, refusing to derive a machine-bound key

    On Linux, set a real ID (for example systemd-machine-id-setup). Ciphertext created by an older version on such a host was not machine-bound: decrypt it with that version first, then encrypt it again once a real ID is set.


Passphrase mode (portable) (optional)

In this mode the passphrase is read from environment variable ENCDEC_PASS (to avoid passing secrets through CLI args / history / process list).

Set passphrase

export ENCDEC_PASS='your-strong-passphrase'

Encrypt

go run . ENCPASS 'PasswordToEncrypt'

Output:

encrypted : p1:<salt_b64>:<hex-ciphertext>

Decrypt

go run . DECPASS 'p1:<salt_b64>:<hex-ciphertext>'

Output:

PasswordToEncrypt

Passphrase ciphertext format details (as implemented in lib/lib.go):

  • Prefix: p1: (format/version marker)
  • Full format: p1:<salt_b64>:<cipher_hex>
  • <salt_b64> is encoded with base64.RawStdEncoding
  • Salt length is 16 bytes (randomly generated on encryption)
  • The derived key uses Argon2id with parameters:
    • iterations: 1
    • memory: 64*1024 KiB (≈ 64 MiB)
    • parallelism: 4
    • output key length: 32 bytes (AES-256)

If the ciphertext does not start with p1: the tool returns:

invalid passphrase ciphertext format (missing p1: prefix)

Building for source

1. (Recommended) change the secret prefix

For an extra layer of security in machine-bound mode, change the secret prefix.

  • Preferred (no rebuild): set the env var at runtime
    export ENCDEC_SECRET_PREFIX='my-secret-prefix'
  • Or compile a custom default into the binary by editing defaultSecretKeyPrefix in lib/lib.go:
    const defaultSecretKeyPrefix = "jY-1"

2. (Optional) change the log path

If you want file logging at a different location, edit pathLog in main.go:

const pathLog = "/opt/frm/writable/logs/"

3. Build the binary

Note: the project is now a multi-file Go package (main.go + lib/), so build the package (.), not a single file. go build main.go from older versions no longer works.

go build -o encdec .

Cross-compilation

General form:

env GOOS=<target-OS> GOARCH=<target-architecture> go build -o encdec .

env is a Linux/Unix command. On Windows use Git Bash, or set the variables with PowerShell / set (see Windows example below).

Linux example (amd64):

env GOOS=linux GOARCH=amd64 go build -o encdec .

macOS examples:

# Intel Macs
env GOOS=darwin GOARCH=amd64 go build -o encdec .

# Apple Silicon (M1/M2/M3...)
env GOOS=darwin GOARCH=arm64 go build -o encdec .

Windows examples (produces encdec.exe):

From Git Bash / Linux / macOS:

env GOOS=windows GOARCH=amd64 go build -o encdec.exe .

From Windows PowerShell:

$env:GOOS="windows"; $env:GOARCH="amd64"; go build -o encdec.exe .

From Windows cmd.exe:

set GOOS=windows
set GOARCH=amd64
go build -o encdec.exe .

Reminder: on Windows the fixed Unix log path /opt/... is not used, so file logging is skipped automatically (see Logging).

Table of config contents (GOOS / GOARCH)

Common combinations supported by the Go toolchain:

GOOS (Target OS) GOARCH (Target architecture)
android arm
darwin amd64
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64

Run go tool dist list to see the full, up-to-date list for your Go version.


Configuration / Environment Variables

ENCDEC_SECRET_PREFIX (machine-bound mode)

Overrides the default secret prefix used in machine-bound key derivation.

  • If not set, the code uses the built-in default prefix (see defaultSecretKeyPrefix in lib/lib.go).
  • If you keep the default value and the code is public, the prefix should be considered not secret.

Example:

export ENCDEC_SECRET_PREFIX='my-secret-prefix'

Important:

  • To decrypt successfully in machine-bound mode, ENCDEC_SECRET_PREFIX must match the value used during encryption.

ENCDEC_PASS (passphrase mode)

Required for ENCPASS / DECPASS.

Example:

export ENCDEC_PASS='your-strong-passphrase'

Logging

The CLI tries to write logs to:

const pathLog = "/opt/frm/writable/logs/"

Behavior (best-effort):

  • The tool attempts to create the directory and append to:
    /opt/frm/writable/logs/enc_dec.log
  • If any step fails (directory not writable/missing permissions/path not valid), the program continues without file logging.

Platform notes (as implemented in main.go):

  • On Windows, file logging is skipped because the fixed Unix path /opt/... is not suitable.
  • On non-Windows systems, the tool attempts to enforce file permissions 0664 on the log file when possible.

Input size limits

To reduce accidental/malicious resource usage, inputs are limited:

Library limits (lib/lib.go):

  • plaintext max: 1 MiB
  • ciphertext max: 1 MiB of hex chars

CLI-level additional check (main.go):

  • for DEC, ciphertext max: 1 MiB of hex chars (secondary defense)

If exceeded, the tool returns an error similar to:

plaintext too large (max 1048576 bytes)

or:

ciphertext too large (max 1048576 hex chars)

Security model (important)

  • Machine-bound mode prevents “copy & decrypt on another machine” but does not protect against an attacker already running on the same host.
  • Machine-bound mode requires a real machine ID: ENC and DEC exit with an error when the ID is empty, whitespace-only, or the systemd placeholder uninitialized. Without this check the key would depend only on the prefix and the OS, so with the default prefix anyone could compute it. Cloned images that share a real machine ID cannot be detected and still derive the same key.
  • Passphrase mode provides portability and shifts security to the strength/handling of the passphrase.

License

GNU General Public License v3.0

Author

Danilo Ritarossi

About

Stand alone software developed in GO Language, allows you to uniquely encrypt the string of the application based on keywords: Special key+Machine ID+OS

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages