Node.js integrates cryptography with the crypto library. This example implements ChaCha20, and which is a stream cipher:
Node.js Crypto (ChaCha20) |
Theory
A sample run is:
Message: qwerty Passphrase: fred Ciphertext: 746f1985050b Decipher qwerty
The following is some sample code
var chacha20 = require("chacha20"); 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 nonce = new Buffer.alloc(8); nonce.fill(0); console.log( key); var ciphertext = chacha20.encrypt(key, nonce, new Buffer.from(plaintext)); console.log("Ciphertext:\t",ciphertext.toString("hex")); console.log("Decipher\t",chacha20.decrypt(key, nonce, ciphertext).toString());
If we use a key of “qwerty”, can you find the well-known fruits (in lower case) of the following ChaCha20 cipher streams:
e47a2bfe646a ea783afc66 e96924f16d6e
Answer the following:
- What are the fruits?
- What can you say about the length of the cipher stream as related to the plaintext?
- How are we generating the key and what is the key length?
- What is the first two bytes of the key?
- What is the salt used in this example?
- How do you convert the cipherstream to Base64?
- How many bits will the salt use?