Node.js integrates cryptography with the crypto library. This example implements RC4, and which is a variable length key cipher:
Node.js Crypto (RC4) |
Theory
A sample run is:
Message: test Passphrase: test Ciphertext: 83d2e09c Decipher: test
The following is some sample code
// RC4 var crypto = require('crypto'); var keyname="test"; var plaintext = "testing"; var args = process.argv; if (args.length>1) plaintext=args[2]; if (args.length>2) keyname=args[3]; var key = crypto.createHash('sha256').update(keyname).digest(); var cipher = crypto.createCipheriv('rc4', key,'' ); var ciphertext = cipher.update( plaintext, 'utf8', 'hex'); console.log("Ciphertext:\t",ciphertext); var decipher = crypto.createDecipheriv('rc4', key,'' ); var text = decipher.update( ciphertext, 'hex','utf8'); console.log("Decipher:\t",text);
For a password of "napier, a range of fruits have been ciphered using the RC4 cipher stream:
8d1cc8bdf6da 911adbb2e6dda57cdaad 8907deba
Answer these:
- What are the fruits?
- What happens to the cipher when you add an IV (salt) string?
- For light-weight cryptography, what is the advantage of having a variable key size:
- How might we change the program to implement 128-bit RC4?