simple-rc4

0.0.1-b • Public • Published

SimpleRC4

import

Use

var RC4 = require('simple-rc4');

to import the package.

basic usage

The constructor call

var rc4 = new RC4(key)

creates a new encryption/decryption instance. Here key should be a variable of type Buffer or String. Subsequential calls to rc4.update(msg) encrypt the given argument. If msg is a Buffer it will be encrypted in place. Messages of type String are converted to Buffers and the encrypted message is given as return value.

simple example

var key = new Buffer([1, 2, 3, 4]);
var msg = new Buffer('secret message');
console.log('input:     ', msg.toString(), ' == ', msg.toString('hex'));
 
// create encryption instance
var enc = new RC4(key);
enc.update(msg);
console.log('encrypted: ', msg.toString(), ' == ', msg.toString('hex'));
 
// create decryption instance (equals encryption instance)
var dec = new RC4(key);
dec.update(msg);
console.log('output:    ', msg.toString(), ' == ', msg.toString('hex'));

encryption and decryption of streams

You can use RC4.Transform to encrypt a readable stream as follows:

var fIn = fs.createReadStream('message.txt'),
    fOut = fs.createWriteStream('encrypted_message.txt');
 
var transform = new RC4.Transform("abcd");
fIn.pipe(transform);
transform.pipe(fOut);

The same procedure can be used to decrypt a given stream:

var fIn = fs.createReadStream('encrypted_message.txt'),
    fOut = fs.createWriteStream('decrypted_message.txt');
 
var transform = new RC4.Transform("abcd");
fIn.pipe(transform);
transform.pipe(fOut);

Readme

Keywords

Package Sidebar

Install

npm i simple-rc4

Weekly Downloads

22

Version

0.0.1-b

License

MIT

Last publish

Collaborators

  • oelk