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 = require('hybrid-crypto-js').RSA;
var Crypt = require('hybrid-crypto-js').Crypt;
React Native
import { Crypt, RSA } from 'hybrid-crypto-js';
Web
Download minified hybrid-crypto.min.js file here.
<script type="text/javascript" src="hybrid-crypto.min.js"></script>
Features
Initialization
var crypt = new Crypt();
var rsa = new RSA();
var entropy = 'Random string, integer or float';
var crypt = new Crypt({ entropy: entropy });
var rsa = new RSA({ entropy: entropy });
var crypt = new Crypt({ md: 'sha512' });
var crypt = new Crypt({
aesStandard: 'AES-CBC',
rsaStandard: 'RSA-OAEP',
});
var crypt = new Crypt({
aesKeySize: 192,
});
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!';
var encrypted = crypt.encrypt(publicKey, message);
var encrypted = crypt.encrypt([publicKey1, publicKey2, publicKey3], message);
var encrypted = crypt.encrypt(publicKey, message, signature);
Pretty-printed sample output
{
"v": "hybrid-crypto-js_0.1.2",
"iv": "CmtyaZTyzoAp1mTNUTztic0v1...",
"keys": {
"85:3d:10:e1:56...": "bHaTF9...",
"d3:48:6a:e9:13...": "t9eds3..."
},
"cipher": "+iwVFsC2dECBQvwcm9DND..."
"signature": "sdL93kfdm12feds3C2..."
}
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...';
var decrypted = crypt.decrypt(privateKey, encrypted);
var message = decrypted.message;
Sample output
{
message: "Hello world!",
signature: "sdL93kfdm12feds3C2..."
}
Signatures
Hybrid Crypto JS provides simple message signing. The encrypted message can be signed with the issuer’s private key.
var message = 'Hello world!';
var signature = crypt.signature(issuerPrivateKey, message);
var encrypted = crypt.encrypt(receiverPublicKey, message, signature);
var crypt = new Crypt({
md: 'sha512',
});
Verifying
The message receiver needs to have a message issuer’s public RSA key in order to verify the message issuer.
var encrypted = '{"v":"hybri... ..."signature":"sdL93kfd...';
var decrypted = crypt.decrypt(receiverPrivateKey, encrypted);
var verified = crypt.verify(
issuerPublicKey,
decrypted.signature,
decrypted.message,
);
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.
var rsa = new RSA();
rsa.generateKeyPair(function(keyPair) {
var publicKey = keyPair.publicKey;
var privateKey = keyPair.privateKey;
});
rsa.generateKeyPairAsync().then(keyPair => {
var publicKey = keyPair.publicKey;
var privateKey = keyPair.privateKey;
});
rsa.generateKeyPair(function(keyPair) {
var publicKey = keyPair.publicKey;
var privateKey = keyPair.privateKey;
}, 1024);
var rsa = new RSA({
keySize: 4096,
});
评论