A zero-dependency library for secure JSON encryption/decryption using Web Crypto API
npm install secure-json-encryptor


A lightweight, zero-dependency library for secure JSON encryption and decryption using the native Web Crypto API. Works seamlessly across React Native, Node.js, and modern browsers.
- ๐ Zero Dependencies โ Built entirely on the native Web Crypto API (no third-party libraries required)
- ๐ AES-GCM Encryption - Authenticated encryption with 256-bit keys
- ๐ฑ Cross-Platform - React Native, Node.js, and browser compatible
- ๐ Simple API - Easy-to-use encrypt/decrypt functions
- ๐ฆ Ultra Lightweight - Less than 2KB minified
- ๐ก๏ธ TypeScript Ready - Includes full TypeScript definitions
- ๐งช Well Tested - Comprehensive test coverage
Supports all environments with Web Crypto API:
- โ
Android (React Native 0.71+)
- โ
iOS (React Native 0.71+)
- Chrome 37+
- Firefox 34+
- Safari 14+
- Edge 79+
- Node.js 15+
bash
npm install secure-json-encryptor
or
yarn add secure-json-encryptor
or
pnpm add secure-json-encryptor`๐ฆ Usaging
`js
import { encrypt, decrypt } from 'secure-json-encryptor';const secretKey = 'your-secret-password-here';
const sensitiveData = {
username: 'john_doe',
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9',
preferences: { theme: 'dark', notifications: true }
};
// Encrypt data
const encrypted = await encrypt(sensitiveData, secretKey);
console.log('Encrypted:', encrypted); // Base64 string
// Decrypt data
const decrypted = await decrypt(encrypted, secretKey);
console.log('Decrypted:', decrypted); // Original object
`๐ฑ React Native Example
`js
import React, { useState } from 'react';
import { encrypt, decrypt } from 'secure-json-encryptor';
import AsyncStorage from '@react-native-async-storage/async-storage';const App = () => {
const [data, setData] = useState(null);
const saveSecureData = async (userData) => {
const SECRET_KEY = 'your-app-secret-key';
// Encrypt before storing
const encrypted = await encrypt(userData, SECRET_KEY);
await AsyncStorage.setItem('user_data', encrypted);
};
const loadSecureData = async () => {
const SECRET_KEY = 'your-app-secret-key';
const encrypted = await AsyncStorage.getItem('user_data');
if (encrypted) {
try {
const decrypted = await decrypt(encrypted, SECRET_KEY);
setData(decrypted);
} catch (error) {
console.error('Failed to decrypt:', error);
}
}
};
// Component implementation...
};
`๐ฅ๏ธ Node.js Example
`js
const { encrypt, decrypt } = require('secure-json-encryptor');
const fs = require('fs').promises;// Encrypt and save sensitive config
async function saveConfig(config) {
const secretKey = process.env.APP_SECRET_KEY;
const encrypted = await encrypt(config, secretKey);
await fs.writeFile('config.encrypted', encrypted);
}
// Load and decrypt config
async function loadConfig() {
const secretKey = process.env.APP_SECRET_KEY;
const encrypted = await fs.readFile('config.encrypted', 'utf8');
return await decrypt(encrypted, secretKey);
}
`๐ Security Best Practices
`
Key Management:javascript
// Good: Use environment variables
const secretKey = process.env.ENCRYPTION_SECRET;
// Bad: Hardcoded keys
const secretKey = 'my-hardcoded-key';
`
๐ License
`
Copyright (c) 2024 Secure JSON Encryptor
Licensed under the MIT License (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at:https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, this software
is provided on an "AS IS" basis, without any warranties or conditions of any kind,
either express or implied, including but not limited to the warranties of
merchantability, fitness for a particular purpose, and non-infringement.
See the License for the specific language governing permissions and
limitations under the License.
``