@caratlanedg/cl-crypto
v1.1.0
Published
AES-128/192/256 ECB with PKCS5 padding: encrypt/decrypt AES/ECB/PKCS5Padding and UTF-8 SecretKeySpec.
Readme
cl-crypto
AES-128, AES-192, and AES-256 ECB with PKCS5 padding for Node.js, matching Java Cipher#getInstance("AES/ECB/PKCS5Padding") where the key comes from SecretKeySpec(keyString.getBytes(UTF_8), "AES").
Requires Node.js 18+ (ES modules and node:crypto).
Install
Published package:
npm install cl-cryptoLocal path dependency:
{
"dependencies": {
"cl-crypto": "file:../cl-crypto"
}
}Then:
npm installThe package exposes ESM and CommonJS via exports: import resolves to ESM, require to CJS.
Usage
ESM
import { encrypt, decrypt } from 'cl-crypto';
const key = '1234567890123456'; // 16 UTF‑8 bytes → AES-128
const ciphertext = encrypt('plain text', key);
const plaintext = decrypt(ciphertext, key);CommonJS
const { encrypt, decrypt } = require('cl-crypto');
const key = '1234567890123456';
const ciphertext = encrypt('plain text', key);
const plaintext = decrypt(ciphertext, key);API
encrypt(value, key)
value— plaintext string (UTF‑8 bytes are encrypted).key— secret string interpreted as UTF‑8; length must be 16, 24, or 32 bytes (AES‑128, AES‑192, AES‑256).- Returns — Base64 ciphertext.
decrypt(value, key)
value— Base64 string produced byencrypt.key— samekeyas for encryption.- Returns — plaintext string (UTF‑8 decoded).
Wrong key lengths throw: AES key must be 16, 24, or 32 bytes when UTF-8 encoded; got …
Java interoperability
Encrypt (conceptually the same pipeline as this package):
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,
new SecretKeySpec(keyString.getBytes(StandardCharsets.UTF_8), "AES"));
byte[] ciphertext = cipher.doFinal(plaintextString.getBytes(StandardCharsets.UTF_8));
String base64 = Base64.getEncoder().encodeToString(ciphertext);Decrypt:
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,
new SecretKeySpec(keyString.getBytes(StandardCharsets.UTF_8), "AES"));
byte[] plain = cipher.doFinal(Base64.getDecoder().decode(base64Ciphertext));
String plaintext = new String(plain, StandardCharsets.UTF_8);Use the same key string and UTF‑8 encoding on both sides.
Development
From the package root:
npm run validateRebuilds cjs/index.cjs from src/index.js (esbuild), then runs ESM and CJS smoke checks (scripts/validate.js and scripts/validate-cjs.cjs). After editing src/index.js, commit the regenerated cjs/index.cjs or run npm run build:cjs before publish.
Security
ECB does not use an IV; repeated plaintext blocks produce repeated ciphertext. Prefer AES‑GCM (or similar) for new systems. This package is for compatibility with existing ECB‑based integrations.
