From 511578cee20c7ef3580a09b7b10a39e8d0403747 Mon Sep 17 00:00:00 2001 From: Bill Buchanan Date: Thu, 3 Feb 2022 13:56:08 +0000 Subject: [PATCH] Update README.md --- unit02_symmetric/lab/README.md | 50 +++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/unit02_symmetric/lab/README.md b/unit02_symmetric/lab/README.md index 65e0408..bd1ed95 100644 --- a/unit02_symmetric/lab/README.md +++ b/unit02_symmetric/lab/README.md @@ -375,38 +375,50 @@ The Chacha20 cipher is a stream cipher which uses a 256-bit key and a 64-bit non ### G.1 We can use node.js to implement ChaCha20: -```javascript -var chacha20 = require("chacha20"); -var crypto = require('crypto'); +```Python +from cryptography.hazmat.primitives import hashes +from cryptography.hazmat.primitives.ciphers.aead import ChaCha20Poly1305 +import sys +import binascii +from cryptography.hazmat.backends import default_backend -var keyname="test"; -var plaintext = "testing"; -var args = process.argv; -if (args.length>2) plaintext=args[2]; -if (args.length>3) keyname=args[3]; +msg = "edinburgh" +key = "qwerty" -var key = crypto.createHash('sha256').update(keyname).digest(); +if (len(sys.argv)>1): + msg=str(sys.argv[1]) -var nonce = new Buffer.alloc(8); -nonce.fill(0); +if (len(sys.argv)>2): + key=str(sys.argv[2]) -console.log( key); +print ("Data:\t",msg) +print ("Key:\t",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()); +digest = hashes.Hash(hashes.SHA256(),default_backend()) +digest.update(key.encode()) +k=digest.finalize() + +chacha = ChaCha20Poly1305(k) +nonce = '\0\0\0\0\0\0\0\0\0\0\0\0' +add='' +cipher = chacha.encrypt(nonce.encode(), msg.encode(), add.encode()) +rtn=chacha.decrypt(nonce.encode(), cipher, add.encode()) + +print ("\nKey:\t",binascii.b2a_hex(key.encode()).decode()) +print ("Nonce:\t",binascii.b2a_hex(nonce.encode()).decode()) +print ("\nCipher:\t",binascii.b2a_hex(cipher).decode()) +print ("Decrypted:\t",rtn.decode()) ``` -Repl.it code: [here](https://repl.it/@billbuchanan/chachalab#index.js) +Repl.it code: [here](https://replit.com/@billbuchanan/chacha03#main.py) 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
-
+``` What are the fruits?