encryptor-e2e
Version:
`encryptor-e2e` is a lightweight Node.js library that provides **end-to-end encrypted messaging** using a hybrid cryptographic approach — combining **RSA for key exchange** and **AES for message encryption**. It simplifies the process of securely sending
141 lines (103 loc) • 5.27 kB
Markdown
# 🔐 encryptor-e2e
`encryptor-e2e` is a lightweight Node.js library that provides **end-to-end encrypted messaging** using a hybrid cryptographic approach — combining **RSA for key exchange** and **AES for message encryption**. It simplifies the process of securely sending and receiving messages with just a few high-level functions.
---
## ✨ Features
- 🔑 **Hybrid Encryption**: Combines RSA (asymmetric) and AES (symmetric) encryption for optimal performance and security.
- 🔐 **End-to-End Secure Messaging**: Encrypt and decrypt messages using securely exchanged keys.
- 🧠 **High-Level API**: Abstracted functions make implementation effortless.
- 🔧 **Built with TypeScript**: Includes full type definitions out of the box.
- 🛡️ **Safe Key Management**: Generates and uses secure, random AES keys and RSA key pairs.
---
## 📦 Installation
```bash
npm install encryptor-e2e
```
## 🚀 Usage Example
The following example demonstrates how to use e2e-secure-messaging for secure messaging between two parties, Alice and Bob.
### 1. Import the Library
```bash
import * as secureMessaging from 'encryptor-e2e';
// or for CommonJS
// const secureMessaging = require('encryptor-e2e');
```
### 2. Generate Key Pairs
Each user (e.g., Alice and Bob) needs an RSA key pair to securely exchange symmetric keys. This can be generated using generateKeyPair().
```bash
// Generate key pairs for Alice and Bob
const aliceKeys = secureMessaging.generateKeyPair();
const bobKeys = secureMessaging.generateKeyPair();
```
### 3. Send a Secure Message (Alice → Bob)
Alice can send a secure message to Bob by calling sendSecureMessage(). This function:
- Generates a random symmetric key (AES).
- Encrypts the message using AES and the symmetric key.
- Encrypts the symmetric key with Bob’s RSA public key.
The result includes both the encryptedMessage and the encryptedSymmetricKey, which Alice can send to Bob.
```bash
const message = "Hello Bob, this is a secure message!";
const { encryptedMessage, encryptedSymmetricKey } = secureMessaging.sendSecureMessage(
message,
aliceKeys.privateKey,
bobKeys.publicKey
);
console.log("Encrypted Message:", encryptedMessage);
console.log("Encrypted Symmetric Key:", encryptedSymmetricKey);
```
### 4. Receiving a Secure Message
Bob receives the encrypted message and encrypted symmetric key from Alice. He can decrypt the message using receiveSecureMessage(), which:
- Decrypts the symmetric key using Bob’s private RSA key.
- Decrypts the message using the decrypted symmetric key.
```bash
const decryptedMessage = secureMessaging.receiveSecureMessage(
encryptedMessage,
encryptedSymmetricKey,
bobKeys.privateKey
);
console.log("Decrypted Message from Alice:", decryptedMessage); // Outputs: "Hello Bob, this is a secure message!"
```
## 📘 API Reference
```generateKeyPair()```
Generates an RSA key pair for secure key exchange.
- Returns: ```{ publicKey, privateKey }``` — The public and private RSA keys.
```sendSecureMessage(message, senderPrivateKey, receiverPublicKey)```
Encrypts a message for end-to-end secure communication.
- Parameters:
- ```message``` (string): The plaintext message to encrypt.
- ```senderPrivateKey``` (string): The sender’s private RSA key (for secure identity verification).
- ```receiverPublicKey``` (string): The receiver’s public RSA key.
- Returns: ```{ encryptedMessage, encryptedSymmetricKey }``` — The encrypted message and encrypted symmetric key.
```bash
receiveSecureMessage(encryptedMessage: string, encryptedSymmetricKey: string, receiverPrivateKey: string): string
```
Decrypts a message that was securely encrypted using sendSecureMessage.
- Parameters:
```encryptedMessage``` (string): The encrypted message.
```encryptedSymmetricKey``` (string): The encrypted symmetric key.
```receiverPrivateKey``` (string): The receiver’s private RSA key for decrypting the symmetric key.
- Returns: ```string``` — The decrypted message.
## Security Best Practices
- Keep Private Keys Secure: The private keys are essential to message decryption and should never be exposed or shared.
- Use HTTPS in Production: If sending encrypted data over the network, always use HTTPS to prevent interception.
## 🧪 Example (Quick Test)
```bash
const alice = secureMessaging.generateKeyPair();
const bob = secureMessaging.generateKeyPair();
const { encryptedMessage, encryptedSymmetricKey } = secureMessaging.sendSecureMessage(
"Confidential Message",
alice.privateKey,
bob.publicKey
);
const result = secureMessaging.receiveSecureMessage(
encryptedMessage,
encryptedSymmetricKey,
bob.privateKey
);
console.log("Bob received:", result);
```
### License
[](https://opensource.org/licenses/MIT)
## Authors
- [@Rahul Karmakar](https://www.npmjs.com/~rahul28112002)
### Acknowledgments
[](https://www.npmjs.com/package/crypto-js)
[](https://www.npmjs.com/package/node-rsa)