json-secure-crypt is a Node.js package that provides tools to securely encrypt and decrypt JSON files using AES encryption. It supports both local files and remote files accessed via URLs, with optional proxy configuration.
To install the package, use one of the following commands:
npm install json-secure-cryptyarn add json-secure-cryptThis package provides two primary command-line tools: encrypt and decrypt, which can be executed via npm or Yarn scripts.
Encrypt a local JSON file and save the encrypted content to a new file:
npm run encrypt -- <input-file-path> <output-file-path> <secret-key>yarn encrypt <input-file-path> <output-file-path> <secret-key><input-file-path>: Path to the JSON file you want to encrypt.<output-file-path>: Path where the encrypted file will be saved.<secret-key>: The secret key used for encryption. Keep this key safe, as it will be required to decrypt the file.
Example:
npm run encrypt -- ./data.json ./encrypted-data.json mySecretKey123or using Yarn:
yarn encrypt ./data.json ./encrypted-data.json mySecretKey123Decrypt a local encrypted JSON file and save the decrypted content to a new file:
npm run decrypt -- <input-file-path> <output-file-path> <secret-key>yarn decrypt <input-file-path> <output-file-path> <secret-key><input-file-path>: Path to the encrypted JSON file.<output-file-path>: Path where the decrypted file will be saved.<secret-key>: The secret key used to decrypt the file.
Example:
npm run decrypt -- ./encrypted-data.json ./decrypted-data.json mySecretKey123or using Yarn:
yarn decrypt ./encrypted-data.json ./decrypted-data.json mySecretKey123You can also decrypt a JSON file hosted remotely via a URL:
npm run decrypt -- <url> <output-file-path> <secret-key> [proxy-host:proxy-port]yarn decrypt <url> <output-file-path> <secret-key> [proxy-host:proxy-port]<url>: The URL of the remote JSON file.<output-file-path>: Path where the decrypted file will be saved.<secret-key>: The secret key used to decrypt the file.[proxy-host:proxy-port](optional): If your network requires a proxy to access the internet, specify the proxy host and port here.
Example:
npm run decrypt -- https://example.com/encrypted.json ./decrypted.json mySecretKey123or using Yarn:
yarn decrypt https://example.com/encrypted.json ./decrypted.json mySecretKey123Example with Proxy:
npm run decrypt -- https://example.com/encrypted.json ./decrypted.json mySecretKey123 127.0.0.1:7890or using Yarn:
yarn decrypt https://example.com/encrypted.json ./decrypted.json mySecretKey123 127.0.0.1:7890You can also use the encryption and decryption functions in your Node.js code.
const { encryptJson, decryptJson } = require('json-secure-crypt');
// Encrypt JSON data
const jsonData = { key: 'value' };
const secretKey = 'mySecretKey123';
const encryptedData = encryptJson(jsonData, secretKey);
// Decrypt JSON data
const decryptedData = decryptJson(encryptedData, secretKey);
console.log(decryptedData); // Output: { key: 'value' }If you need to decrypt JSON data from a remote URL within your application, you can do so as follows:
const { decryptJsonFromUrl } = require('json-secure-crypt');
const url = 'https://example.com/encrypted.json';
const secretKey = 'mySecretKey123';
const proxyConfig = {
host: '127.0.0.1',
port: 8080
};
decryptJsonFromUrl(url, secretKey, proxyConfig)
.then(decryptedData => {
console.log(decryptedData);
})
.catch(err => {
console.error('Error during decryption:', err);
});This package is licensed under the MIT License. See the LICENSE file for more details.