simple-encryption
Simple encryption module for NodeJS using node-forge
Installation
npm install simple-encryption
Usage
Importing
At the top of your code, use something like this to import simple-encryption
var sencrypt = ;
Or alternatively, for ease of use, you could do this:
var RSA = RSA;var AES = AES;
RSA
Currently, RSA is just the basics: encrypting, decrypting, signing, and verifying
Encryption
var RSA = RSA;var publicKey; //Normally you'd need to assign this variable a PEM encoded public keyvar privateKey; //Normally you'd need to assign this variable a PEM encoded private keyvar encrypted = RSA; //Encrypts Hello, World and outputs it as Base64var decrypted = RSA; //Decrypts the messageconsole;console;
Signing
var RSA = RSA;var publicKey; //Normally you'd need to assign this variable a PEM encoded public keyvar privateKey; //Normally you'd need to assign this variable a PEM encoded private keyvar signed = RSA; //Sign the message Hello, Worldvar verified = RSA; //Verify the message with the base64 message signature and base64 message digest returned from the previous functionifverified //verified is a boolean, true on if verification was successful, and false when it wasn't console; else console;
Notes
- You need to generate your own keys, and they should be in a PEM format
- Any potential binary outputs (encrypted/signed outputs) are encoded in base64 to reduce errors
AES
Notes
- AES uses AES-GCM. If you need another mode, this module is currently not for you
Generating information needed
var AES = AES;var iv = AES; //Generate 12 byte IV. Outputs Base64var iv2 = AES; //Is also valid and generates a 16 byte IV, but AES-GCM requires a 12 bytes IV. Outputs Base64var key = AES; //Generate a 256 bit (32 byte) key. Outputs Base64var key2 = AES; //Generate a 128 bit (16 byte) key. Outputs Base64.
Encryption
var AES = AES;var iv = AES;var key = AES;var encrypted = AES; //Encrypts 'Hello, World!', outputting a JavaScript object with Base64 propertiesvar decrypted = AES; //Decrypts 'Hello, World!', and also verifies it at the same timeifdecrypted //If authentication was successful (i.e. it comes from the same person) console; else //Someone has tampered with it console;