This is a command-line encryption/decryption tool implemented in Go, designed to provide secure file and text encryption using AES-256-GCM with PBKDF2 key derivation. It mirrors the functionality of a web-based JavaScript version, allowing interoperability between the two.
- Go: The programming language used for the CLI implementation.
- AES-256-GCM: Symmetric encryption algorithm with authenticated encryption.
- PBKDF2: Password-Based Key Derivation Function 2, used to derive encryption keys from a secret passphrase.
- Base64: Encoding scheme for transmitting binary data as text.
- crypto/aes: Go standard library for AES encryption.
- crypto/cipher: For GCM mode.
- crypto/hmac: For HMAC in PBKDF2 implementation.
- crypto/sha256: Hash function used in key derivation.
- encoding/base64: For base64 encoding/decoding.
- flag: For command-line argument parsing.
The encrypted payload format is:
- Expiry timestamp (8 bytes, big-endian uint64)
- Salt (16 bytes)
- Iterations (4 bytes, big-endian uint32)
- Filename length (1 byte)
- Filename bytes (variable)
- IV (12 bytes)
- Ciphertext (variable)
- User provides a secret passphrase.
- PBKDF2 is applied with:
- Salt: 16 random bytes
- Iterations: 200,000
- Hash: SHA-256
- Output: 32-byte key for AES-256
- Input: File or text from stdin.
- Generate random IV (12 bytes).
- Encrypt plaintext with AES-256-GCM using derived key and IV.
- Construct payload with expiry, salt, iterations, filename, IV, and ciphertext.
- Base64-encode the payload.
- Output base64 string to file or stdout.
- Input: Base64-encoded payload from file or stdin.
- Base64-decode to binary payload.
- Parse payload: extract expiry, salt, iterations, filename, IV, ciphertext.
- Check expiry if set.
- Derive key using same PBKDF2 parameters.
- Decrypt ciphertext with AES-256-GCM.
- Write plaintext to file using embedded filename.
- Encode:
go run encoder.go -cmd encode -input <file> -secret <key> -expiry <seconds> -filename <name> -output <file> - Decode:
go run encoder.go -cmd decode -input <file> -secret <key> -output <dir>
- Uses strong key derivation (PBKDF2 with high iteration count).
- Authenticated encryption prevents tampering.
- Optional expiry prevents indefinite access.
- Secrets should be managed securely (not hardcoded).
The Go CLI produces payloads compatible with the JavaScript web app, allowing encryption in one and decryption in the other.
#encryption #go #aes #pbkdf2 #cli #security