Hybrid Crypto JS
Introduction
Hybrid Crypto JS is a hybrid (RSA+AES) encryption and decryption toolkit for JavaScript. Hybrid Crypto JS combines RSA and AES encryption algorithms, making it possible to encrypt and decrypt large messages efficiently. This cross-platform library is based on Forge. Hybrid Crypto JS can be used in browsers, Node.js, or React Native.
Documentation
Getting started
Features
Installation
npm install hybrid-crypto-js
Importing
Node.js
var RSA = RSA;var Crypt = Crypt;
React Native
;
Web
Download minified hybrid-crypto.min.js file here.
Features
Initialization
// Basic initializationvar crypt = ;var rsa = ; // Increase amount of entropyvar entropy = 'Random string, integer or float';var crypt = entropy: entropy ;var rsa = entropy: entropy ; // Select default message digestvar crypt = md: 'sha512' ; // Select AES or RSA standardvar crypt = // Default AES standard is AES-CBC. Options are: // AES-ECB, AES-CBC, AES-CFB, AES-OFB, AES-CTR, AES-GCM, 3DES-ECB, 3DES-CBC, DES-ECB, DES-CBC aesStandard: 'AES-CBC' // Default RSA standard is RSA-OAEP. Options are: // RSA-OAEP, RSAES-PKCS1-V1_5 rsaStandard: 'RSA-OAEP'; // Alternate AES keysize (some AES algorithms requires specific key size)var crypt = aesKeySize: 192 // Defaults to 256;
Encryption
Hybrid Crypto JS provides basic encryption function that also supports multiple RSA keys, with or without signature. An encrypted message is a JSON formatted string.
var message = 'Hello world!'; // Encryption with one public RSA keyvar encrypted = crypt; // Function also supports encryption with multiple RSA public keysvar encrypted = crypt; // Encryption with signaturevar encrypted = crypt;
Pretty-printed sample output
"v": "hybrid-crypto-js_0.1.2" // Current package version "iv": "CmtyaZTyzoAp1mTNUTztic0v1..." // Initialization vector "keys": // Encrypted AES keys by RSA fingerprints "85:3d:10:e1:56...": "bHaTF9..." "d3:48:6a:e9:13...": "t9eds3..." "cipher": "+iwVFsC2dECBQvwcm9DND..." // Actual encrypted message "signature": "sdL93kfdm12feds3C2..." // Signature (optional)
Decryption
Decrypting message with Hybrid Crypto JS is as easy as encrypting. Decrypt function can decrypt any message which has been encrypted with key pair's public key. The decrypted message is a JSON object containing a message and an optional signature.
var encrypted = '{"v":"hybrid-crypto-js_0.1.0","iv":"CmtyaZTyzoAp1mTN...'; // Decrypt encryped message with private RSA keyvar decrypted = crypt; // Get decrypted messagevar message = decryptedmessage;
Sample output
message: "Hello world!" // Actual decrypted message signature: "sdL93kfdm12feds3C2..." // Signature (optional)
Signatures
Hybrid Crypto JS provides simple message signing. The encrypted message can be signed with the issuer's private key.
var message = 'Hello world!'; // Create a signature with ISSUER's private RSA keyvar signature = crypt; // Encrypt message with RECEIVERS public RSA key and attach the signaturevar encrypted = crypt; // Select default message digestvar crypt = md: 'sha512' // Options: sha1, sha256, sha384, sha512, and md5;
Verifying
The message receiver needs to have a message issuer's public RSA key in order to verify the message issuer.
// Encrypted message with signaturevar encrypted = '{"v":"hybri... ..."signature":"sdL93kfd...'; // Decrypt message with own (RECEIVER) private keyvar decrypted = crypt; // Verify message with ISSUER's public keyvar verified = crypt;
Verification function returns true or false depending on whether the verification was successful.
RSA key pairs
Hybrid Crypto JS RSA key generation function is based in Forge key pair generation function. As a difference, Hybrid Crypto JS returns key pair in PEM format.
// Initialize RSA-classvar rsa = ; // Generate RSA key pair, default key size is 4096 bitrsa; // ... or:rsa; // Generate 1024 bit RSA key pairrsa; // Key size // RSA can be also initialized with optionsvar rsa = keySize: 4096;