encrypt-decrypt-js
v1.0.0
Published
EncryptDecryptJS is a lightweight **encryption and decryption library** for **React** and **Node.js**.
Readme
EncryptDecryptJS: Secure Encryption & Decryption for React & Node.js
🔐 Overview
EncryptDecryptJS is a lightweight encryption and decryption library for React and Node.js.
✨ Features
- AES-GCM and RSA-OAEP encryption
- HMAC (SHA-256) for integrity verification
- Base64-encoded key support for easy key sharing
- Secure key storage in IndexedDB
- Asynchronous API with Promises
- Fully compatible with React & Node.js
📦 Installation
npm i encrypt-decrypt-js🔑 Generating & Using Keys
Generate an AES Key
import { generateAESKey, exportAESKeyToBase64 } from "encrypt-decrypt-js";
async function generateKey() {
const key = await generateAESKey();
const base64Key = await exportAESKeyToBase64(key);
console.log("Generated AES Key (Base64):", base64Key);
}
generateKey();Generate RSA Keys
import { generateRSAKey, exportRSAKeyToBase64 } from "encrypt-decrypt-js";
async function generateRSAKeys() {
const { publicKey, privateKey } = await generateRSAKey();
const publicKeyBase64 = await exportRSAKeyToBase64(publicKey, false);
const privateKeyBase64 = await exportRSAKeyToBase64(privateKey, true);
console.log("Public Key (Base64):", publicKeyBase64);
console.log("Private Key (Base64):", privateKeyBase64);
}
generateRSAKeys();🔒 Encryption & Decryption
AES-GCM Encryption & Decryption
import { encrypt, decrypt } from "encrypt-decrypt-js";
async function testAES() {
const keyBase64 = "your-base64-encoded-key";
const encryptedData = await encrypt("Hello, World!", { type: "AES-GCM" }, keyBase64);
console.log("Encrypted:", encryptedData);
const decryptedData = await decrypt(encryptedData, { type: "AES-GCM" }, keyBase64);
console.log("Decrypted:", decryptedData);
}
testAES();RSA-OAEP Encryption & Decryption
import { encrypt, decrypt } from "encrypt-decrypt-js";
async function testRSA() {
const publicKeyBase64 = "your-public-key-in-base64";
const privateKeyBase64 = "your-private-key-in-base64";
const encryptedData = await encrypt("Hello, RSA!", { type: "RSA-OAEP" }, publicKeyBase64);
console.log("Encrypted:", encryptedData);
const decryptedData = await decrypt(encryptedData, { type: "RSA-OAEP" }, privateKeyBase64);
console.log("Decrypted:", decryptedData);
}
testRSA();🔏 HMAC: Message Integrity Verification
Generate & Verify HMAC Signature
import { generateHMACKey, generateHMAC, verifyHMAC, exportHMACKeyToBase64 } from "encrypt-decrypt-js";
async function testHMAC() {
const hmacKey = await generateHMACKey();
const hmacKeyBase64 = await exportHMACKeyToBase64(hmacKey);
const message = "Hello, Secure World!";
const signature = await generateHMAC(message, hmacKey);
console.log("HMAC Signature:", signature);
const isValid = await verifyHMAC(message, signature, hmacKey);
console.log("Signature Valid:", isValid);
}
testHMAC();💾 Secure Key Storage (IndexedDB)
Store & Retrieve Keys
import { storeKey, getKey, deleteKey } from "encrypt-decrypt-js";
async function manageKeys() {
const key = await generateAESKey();
await storeKey("my-encryption-key", key);
const retrievedKey = await getKey("my-encryption-key");
console.log("Retrieved Key:", retrievedKey);
await deleteKey("my-encryption-key");
console.log("Key deleted");
}
manageKeys();🛠️ API Reference
🔑 Key Management
| Function | Description |
|----------|-------------|
| generateAESKey() | Generate a new AES-GCM key |
| generateRSAKey() | Generate a new RSA-OAEP key pair |
| exportAESKeyToBase64(key) | Export AES key as Base64 |
| exportRSAKeyToBase64(key, isPrivate) | Export RSA key as Base64 |
| importAESKeyFromBase64(base64Key) | Import AES key from Base64 |
| importRSAKeyFromBase64(base64Key, isPrivate) | Import RSA key from Base64 |
🔒 Encryption & Decryption
| Function | Description |
|----------|-------------|
| encrypt(data, options, keyBase64, hmacKeyBase64?) | Encrypt data (AES or RSA) |
| decrypt(encryptedData, options, keyBase64, hmacKeyBase64?) | Decrypt data (AES or RSA) |
🖋️ HMAC (Integrity Verification)
| Function | Description |
|----------|-------------|
| generateHMACKey() | Generate a new HMAC key |
| generateHMAC(message, key) | Create an HMAC signature for a message |
| verifyHMAC(message, hmac, key) | Verify an HMAC signature |
💾 Secure Key Storage
| Function | Description |
|----------|-------------|
| storeKey(name, key) | Store a key securely in IndexedDB |
| getKey(name) | Retrieve a stored key from IndexedDB |
| deleteKey(name) | Delete a stored key from IndexedDB |
📧 Contact
For questions or support, feel free to reach out.
🚀 CryptoLib: Secure your React & Node.js applications today!
