cipherworld-antony-mse-2103-a
v1.1.11
Published
cipherProject is a JavaScript library used to encode and decode .txt files.
Readme
cipherworld-antony-mse-2103-a README
cipherworld-antony-mse-2103-a is a JavaScript library used to encode and decode .txt files.
How to install the program
With the command line, navigate to the directory containing the text files that you want to encrypt or decrypt. Enter:
$ npm install cipherworld-antony-mse-2103-aDirectory information
test_files- Directory containing 4 test files to be encrypted or decryptedcharacterSets- Directory containing the character sets that will be used for each ciphercharacter_set.txtis used for theletterNumberciphercharacter_set2.txtis used for theletterLettercipher- More character sets can be added in future
How to run code
Navigate to the project directory through the command line.
The command will take 3 or 4 arguments outlined below.
Input :
$ npx cipherWorld [character set] [cipher] [method] [file] <key>npx- node package executecharacter set- the file containing the necessary character set for your chosen cipher. Current possible inputs arecharacter_set.txt(for letter number ciphers) andcharacter_set2.txt(for letter letter ciphers)cipher- UseletterLetterorllif you want to use the LetterLetter cipher. UseletterNumberorlnif you want to use the LetterNumber cipher.method- Useencryptorencto encrypt a file. Usedecryptordecto decrypt a file.file- the filepath for the .txt file that needs encypting/decryptingkey- Optional. Used for letterNumber ciphersThis will create a new file for with the encrypted/decrypted output and store it in the same directory as the original file.
The ciphers currently work off two character sets, the
letterNumberCipherusescharacter_set.txtwhile theletterLetterCipher (cipher.js)usescharacter_set2.txtUsing a
letterNumbercipher inputlnrequires akey. This offsets the value returned value of the cipher by the value of thekey- E.g. with no
key-"d" = 6 - With
key = 3-"d" = 9
- E.g. with no
The
keycan be any integer >= 0, ifkey > 99the cipher rolls over to0and keeps offsettingSee expected outputs for further details
Example Files
This module comes with 4 example files that are located in:
[project_directory]/node_modules/cipherworld-antony-mse-2103-a/text_filesFor ease of use, copy/move these files to your working directory to test package.
Expected outputs
To easily test the examples below, move/copy the files from node_modules/cipherworld-antony-mse-2103-a/text_files into your current working directory
Example 1:
Encrypt testLNEnc.txt with key 31045 - LetterNumber Cipher
input code:
(Using the test files without moving from their original directory: )
$ npx cipherWorld character_set.txt ln enc node_modules/cipherworld-antony-mse-2103-a/test_files/testLNEnc.txt 31045or if moving the testLNEnc.txt file to the current working director
$ npx cipherWorld character_set.txt ln enc testLNEnc.txt 31045Expected output: A new text file called testLNEnc.txt.enc containing:
97747470597481647759796764776413
Example 2:
Decrypt testLNDec.txt.enc with key 4771 - LetterNumber Cipher
input code:
$ npx cipherWorld letterNumber decrypt testLNDec.txt.enc 4771Expected output: A new text file called testLNDec.txt containing:
Hi, Ed! I think someone's on to this cipher!
Example 3:
Encrypt testLLEnc.txt - LetterLetter Cipher
input code:
$ npx cipherWorld ll encrypt testLLEnc.txtExpected output: A new text file called testLLEnc.txt.enc containing:
B!!ym!9DAm2§DAD
Example 4:
Decrypt testLLDec.txt.enc - LetterLetter Cipher
input code:
$ npx cipherWorld ll dec testLLDec.txt.encExpected output: A new text file called testLLDec.txt containing:
Quick! We need a distraction! Once you read this message, find the community channel named "random" on Discord, and share a random fact regarding any insect - but it has to be about insects! Fingers crossed this will distract and slow down the people cracking these ciphers!
Approach to getting to this point
As the letterLetter Cipher and letterNumber cipher had previously been made, I started by recreating those classes.
- Split them into their own files and reworked them to make them more streamline, easier to read and efficient
invertCipher.jsandreadCipher.jswere two methods that I extracted into their own files. They are used in both types of ciphers- In future work, maybe create a
Cipherparent class that contains invertCipher and readCipher, and haveLetterLetterandLetterNumberand child classes.
- In future work, maybe create a
Majority of work on
solveCipher.js. Needed to create a method that would choose the appropriate cipher and appropriate method depending on the input.- Started as one long method that i wrote until it was able to run as expected
- Then i refacactored the file into multiple methods that were called by the
solveCiphermethod
Throughout the project, using console.log to check each method and file and debug as I worked through the task
- e.g. checking that
letterNumber.jsran as expected before moving on toletterLetter.jsand so on - This way, when working on
solveCipher.jsI could trust the output of each method would be what was intended
- e.g. checking that
After
solveCipher.jswas workign and creating new text files. Moved ontomain.js- Needed this to read the console input and execute
solveCipher() - Researched and experimented with
process.argvto save the input as an array to be used assolveCipher()arguments
- Needed this to read the console input and execute
Main problems encountered with file sources, what is the best way to input a file source to the console / input it in your code?
- e.g.
return new LetterNumber('src/character_set.txt')-This file source will work for this directory, but will it cause problems if the directory is moved/ when the repository is cloned to a different device.
Next challenge was to turn the program into a npm package
- Started by researching how to create a package from the npm documentation
- Found a couple of informative youtube videos (https://www.youtube.com/watch?v=N55jHr9qzpg&ab_channel=AndrewMead)
Created the
package.jsonfile and usednpm linkto test the code by usingrequire()in another directory- Had to rework some of the file paths for the
.readFile()ofcharacter_set.txtandcharacter_set2.txtwithinsolveCipher.jsso that the code would work when installed in another directory. - This ended up only being a temporary solution, as the file path now ran through
node_modules(more explained later)
- Had to rework some of the file paths for the
Was able to get the code working in a different directory but was not calling
cipherWorlddirectly through the command lineAfter more research, configured the
binkey inpackage.jsonand created abindirectory.- Set up the
cipherWorld.jsfile to run in the node environment, store command line input and executesolveCipher
- Set up the
Final block was with the file paths found in
solveCipher.jswhen creating a newLetterLetterCipherorLetterNumberCipherclass. -The file path was a relative string tocharacter_set.txtbut it would not work when the node package was installed in a different directory- Found that i needed to change the input to
${__dirname}/character_set.txtso that it would look for character set files from the same directory assolveCipher.js
- Found that i needed to change the input to
Now able to execute
cipherWorldfrom the command line to encrypt and decrypt text filesExtensive research into
yargsto configure thecipherWorldcommand withpositionals,options,choices, andexamples- Still more work can be done to improve this
- Lots of new stuff with
yargstrying to get my head around how things work and what each function does - Tried to keep it basic for now
