DEV Community

LHJ Piper
LHJ Piper

Posted on

Unlocking Easy Encryption with `easy-cipher-mate`: A Developer's Dream Come True

In a world where data breaches make headlines daily, securing sensitive information is no longer optional—it's a necessity. Yet, for many developers, encryption feels like a daunting maze. JavaScript encryption libraries often come with steep learning curves: picking the right algorithm, managing keys securely, handling initialization vectors, and avoiding rookie mistakes that could expose your data. And what about those times when you just need a quick, reliable way to encrypt a file without writing a single line of code? The command-line tools out there are either too complex or nonexistent for simple tasks.

Say goodbye to those headaches. Meet easy-cipher-mate, a game-changing library and CLI tool that brings encryption to your fingertips—whether you're coding in Node.js, working in the browser, or just need a fast terminal command. With robust algorithms like AES-GCM and ChaCha20-Poly1305, it simplifies secure text and file encryption while offering a seamless experience for both beginners and seasoned pros. Let’s dive into how this tool can transform your workflow, with real-world examples and a peek under the hood for the geeks among us.


What is easy-cipher-mate?

easy-cipher-mate is a versatile encryption powerhouse designed to make security accessible. Here’s what it offers:

  • Multiple Algorithms: Choose AES-GCM for trusted symmetric encryption or ChaCha20-Poly1305 for high-performance scenarios.
  • Text and File Encryption: Secure strings in your app or entire files on disk.
  • CLI Convenience: Encrypt and decrypt files directly from the command line—no coding required.
  • Multi-Language Support: Handle different text encodings for global applications.
  • Flexible Configuration: Set up encryption via code, environment variables, or JSON files.

Whether you're safeguarding user data in a web app or locking down sensitive config files, this tool has you covered.


Installing easy-cipher-mate

If you’re new to this, don’t worry—installation is a breeze. Open your terminal and run:

npm install easy-cipher-mate
Enter fullscreen mode Exit fullscreen mode

Or, if you prefer Yarn:

yarn add easy-cipher-mate
Enter fullscreen mode Exit fullscreen mode

That’s it! You’re ready to start securing your data.


Solving Real Problems with the CLI

Imagine you’re a system administrator tasked with securing a secrets.json file containing API keys. Writing a custom script feels like overkill, and you need it done now. With easy-cipher-mate’s CLI, it’s a one-liner.

Encrypting a File

Run this command:

easy-cipher-mate encrypt-file -i secrets.json -o secrets.json.encrypted -p "my-super-secret" -a aes-gcm
Enter fullscreen mode Exit fullscreen mode
  • -i: Input file (secrets.json).
  • -o: Output file (secrets.json.encrypted).
  • -p: Your password.
  • -a: Algorithm (defaults to aes-gcm).

Boom! Your file is encrypted with AES-GCM, safe from prying eyes.

Decrypting a File

Later, when you need access:

easy-cipher-mate decrypt-file -i secrets.json.encrypted -o secrets.json.decrypted -p "my-super-secret" -a aes-gcm
Enter fullscreen mode Exit fullscreen mode

The original file is back, decrypted and ready to use.

Encrypting Logs Line by Line

Now, suppose you’re a developer managing a log file (app.log) where each line is a sensitive event. Encrypting the whole file might not suit your needs—you want to secure each line individually for easier processing later. Here’s how:

easy-cipher-mate encrypt-text-file -f app.log -p "logpassword123" -a aes-gcm -e utf-8
Enter fullscreen mode Exit fullscreen mode
  • -f: File to encrypt.
  • -e: Text encoding (e.g., utf-8 for standard text).

Each non-empty line is encrypted independently and saved in place as base64-encoded text. To decrypt:

easy-cipher-mate decrypt-text-file -f app.log -p "logpassword123" -a aes-gcm -e utf-8
Enter fullscreen mode Exit fullscreen mode

Your logs are back to readable text, line by line. This is perfect for automated scripts or incremental log processing.


Powering Your Code with the API

For developers building applications, easy-cipher-mate shines with its intuitive API. Let’s explore some practical scenarios.

Securing User Input

You’re building a chat app, and you need to encrypt messages before storing them. Here’s how to encrypt a user’s message:

import { AESGCMEncryption, AESGCMEncryptionConfigFromEnv, EncryptionService } from 'easy-cipher-mate';

const algo = new AESGCMEncryption();
const config = new AESGCMEncryptionConfigFromEnv('chatsecret123');
const service = new EncryptionService(algo, config);

async function encryptMessage() {
  const message = "Hey, let's meet at 5 PM!";
  const encrypted = await service.encryptText(message);
  console.log('Encrypted:', encrypted.data);
  return encrypted.data;
}
Enter fullscreen mode Exit fullscreen mode

To decrypt it later:

async function decryptMessage(encryptedData: ArrayBuffer) {
  const decrypted = await service.decryptText(encryptedData);
  console.log('Decrypted:', decrypted); // "Hey, let's meet at 5 PM!"
}
Enter fullscreen mode Exit fullscreen mode

Simple, secure, and ready to integrate into your app.

Protecting a PDF Invoice

Say you’re generating invoices as PDFs and need to encrypt them before emailing. Here’s the code:

import { readFileSync, writeFileSync } from 'fs';
import { AESGCMEncryption, AESGCMEncryptionConfigFromEnv, EncryptionService } from 'easy-cipher-mate';

const algo = new AESGCMEncryption();
const config = new AESGCMEncryptionConfigFromEnv('invoicekey');
const service = new EncryptionService(algo, config);

async function encryptInvoice() {
  const fileBuffer = readFileSync('invoice.pdf');
  const arrayBuffer = fileBuffer.buffer.slice(fileBuffer.byteOffset, fileBuffer.byteOffset + fileBuffer.byteLength);

  const encrypted = await service.encryptFile(arrayBuffer);
  writeFileSync('invoice.pdf.encrypted', Buffer.from(encrypted.data));
}
Enter fullscreen mode Exit fullscreen mode

To decrypt:

async function decryptInvoice() {
  const encryptedBuffer = readFileSync('invoice.pdf.encrypted');
  const encryptedArrayBuffer = encryptedBuffer.buffer.slice(
    encryptedBuffer.byteOffset,
    encryptedBuffer.byteOffset + encryptedBuffer.byteLength
  );

  const decrypted = await service.decryptFile(encryptedArrayBuffer);
  writeFileSync('invoice.pdf.decrypted', Buffer.from(decrypted));
}
Enter fullscreen mode Exit fullscreen mode

Your invoices are now secure and easy to manage.

Supporting Global Users

If your app serves users worldwide, text encoding matters. Encrypting Japanese text, for example:

const japaneseMessage = 'こんにちは、世界!';
const encrypted = await service.encryptText(japaneseMessage, 'utf16le');
const decrypted = await service.decryptText(encrypted.data, 'utf16le');
console.log(decrypted); // 'こんにちは、世界!'
Enter fullscreen mode Exit fullscreen mode

Supported encodings include utf-8, utf16le, base64, hex, latin1, and more—perfect for internationalization.


Configuration Made Easy

easy-cipher-mate offers flexible configuration options:

  • Environment Variables:
  export ECM_AESGCM_PASSWORD="mysecret"
  export ECM_AESGCM_SALT=$(echo -n "salt123" | base64)
  export ECM_AESGCM_IV=$(echo -n "iv456" | base64)
Enter fullscreen mode Exit fullscreen mode

Then in code:

  const config = new AESGCMEncryptionConfigFromEnv();
Enter fullscreen mode Exit fullscreen mode
  • JSON Config:
  {
    "password": "mysecret",
    "salt": "c2FsdDEyMw==", // base64 "salt123"
    "iv": "aXY0NTY=",      // base64 "iv456"
    "textEncoding": "utf-8"
  }
Enter fullscreen mode Exit fullscreen mode

Load it with:

  const config = new AESGCMEncryptionConfigFromJSONFile('config.json');
Enter fullscreen mode Exit fullscreen mode

Choose what fits your workflow—coding, scripting, or both.


For the Geeks: A Peek Under the Hood

Curious about how it works? Let’s look at the key derivation in AESGCMEncryption.ts. The library uses PBKDF2 to turn your password into a secure key:

async function deriveKey(password: string, salt: Uint8Array): Promise<CryptoKey> {
  const encoder = new TextEncoder();
  const keyMaterial = await crypto.subtle.importKey(
    "raw",
    encoder.encode(password),
    "PBKDF2",
    false,
    ["deriveKey"]
  );
  return crypto.subtle.deriveKey(
    {
      name: "PBKDF2",
      salt,
      iterations: 100000,
      hash: "SHA-256"
    },
    keyMaterial,
    { name: "AES-GCM", length: 256 },
    false,
    ["encrypt", "decrypt"]
  );
}
Enter fullscreen mode Exit fullscreen mode

This process iterates 100,000 times with SHA-256 hashing, making brute-force attacks impractical. The Web Crypto API then handles the actual encryption, ensuring top-tier security standards. For files, the same key encrypts the entire buffer, while the CLI’s line-by-line mode applies this per line, encoding results in base64 for text compatibility.


Why You’ll Love easy-cipher-mate

From its dead-simple CLI to its developer-friendly API, easy-cipher-mate takes the pain out of encryption. It’s fast, secure, and flexible enough for any use case—whether you’re a novice securing your first file or a pro building a global app. Install it today with npm install easy-cipher-mate, and see how easy encryption can be.

Ready to lock down your data? Give easy-cipher-mate a spin and share your experience!

Top comments (0)