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.
- 🔑 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.
npm install encryptor-e2e
The following example demonstrates how to use e2e-secure-messaging for secure messaging between two parties, Alice and Bob.
import * as secureMessaging from 'encryptor-e2e';
// or for CommonJS
// const secureMessaging = require('encryptor-e2e');
Each user (e.g., Alice and Bob) needs an RSA key pair to securely exchange symmetric keys. This can be generated using generateKeyPair().
// Generate key pairs for Alice and Bob
const aliceKeys = secureMessaging.generateKeyPair();
const bobKeys = secureMessaging.generateKeyPair();
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.
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);
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.
const decryptedMessage = secureMessaging.receiveSecureMessage(
encryptedMessage,
encryptedSymmetricKey,
bobKeys.privateKey
);
console.log("Decrypted Message from Alice:", decryptedMessage); // Outputs: "Hello Bob, this is a secure message!"
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.
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.
- 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.
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);