From a4abf465cc5bf4b79dac45ece89cd2cb7fb25264 Mon Sep 17 00:00:00 2001 From: SebestikCZ Date: Sun, 20 Mar 2022 17:12:08 +0100 Subject: [PATCH] Added npm file and source Signed-off-by: ENGO150 --- index.js | 4 +++ package.json | 24 +++++++++++++ src/decrypter.js | 83 +++++++++++++++++++++++++++++++++++++++++++++ src/encrypter.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 index.js create mode 100644 package.json create mode 100644 src/decrypter.js create mode 100644 src/encrypter.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..576c86b --- /dev/null +++ b/index.js @@ -0,0 +1,4 @@ +module.exports = { + decrypter: require("./src/decrypter"), + encrypter: require("./src/encrypter") +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..8d64297 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "why2-encryption-system.js", + "version": "1.0.0", + "description": "Node.js wrapper for encryption system WHY2 made by ENGO150 in C lang.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ENGO150/WHY2-Encryption-System.git#nodejs-wrapper" + }, + "keywords": [ + "why2", + "decrypter", + "encrypter" + ], + "author": "Šebestíček & ENGO150", + "license": "ISC", + "bugs": { + "url": "https://github.com/ENGO150/WHY2-Encryption-System/issues" + }, + "homepage": "https://github.com/ENGO150/WHY2-Encryption-System/tree/nodejs-wrapper#readme" +} diff --git a/src/decrypter.js b/src/decrypter.js new file mode 100644 index 0000000..c78b9b6 --- /dev/null +++ b/src/decrypter.js @@ -0,0 +1,83 @@ +module.exports = (text, key) => { + const KEY_LENGTH = 50; + const ENCRYPTION_SEPARATOR = "."; + + //CHECK FOR INVALID key + if (key.length < KEY_LENGTH) { + return { exitCode: -2 }; + } + + //VARIABLES + var returningText; + var numberBuffer; + var textBuffer = ""; + var textBuffer1 = ""; + + numberBuffer = 1; + + //GET LENGTH OF returningText AND textKeyChain + for (var i = 0; i < text.length; i++) { + if (text[i] == ENCRYPTION_SEPARATOR) numberBuffer++; + } + + //SET LENGTH + returningText = []; + returningText.length = numberBuffer; + var textKeyChain = []; + var encryptedTextKeyChain = []; + textKeyChain.length = encryptedTextKeyChain.length = numberBuffer; + + //LOAD textKeyChain + for (var i = 0; i < textKeyChain.length; i++) { + numberBuffer = i; + + //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH + while (numberBuffer >= key.length) { + numberBuffer -= key.length; + } + if (typeof key === 'string') { + key = key.split(""); + key = key.map(element => element.charCodeAt(0)); + } + + //FILL textKeyChain + if ((numberBuffer + 1) % 3 == 0) + { + textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1]; + } else if ((numberBuffer + 1) % 2 == 0) + { + textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1]; + } else + { + textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1]; + } + } + + //LOAD encryptedTextKeyChain + for (var i = 0; i < encryptedTextKeyChain.length; i++) { + numberBuffer = 0; + + textBuffer1 = text.split(ENCRYPTION_SEPARATOR); + + try { + textBuffer = textBuffer1[i]; + encryptedTextKeyChain[i] = parseInt(textBuffer); + } catch (e) { + return { exitCode: -1, error: e }; + } + textBuffer = ""; + } + + //DECRYPT TEXT + for (var i = 0; i < textKeyChain.length; i++) { + textKeyChain[i] -= encryptedTextKeyChain[i]; + } + + //LOAD returningText + for (var i = 0; i < textKeyChain.length; i++) { + returningText[i] = String.fromCharCode(textKeyChain[i]); + } + + returningText = returningText.join(""); + return { exitCode: 0, value: returningText }; + }, \ No newline at end of file diff --git a/src/encrypter.js b/src/encrypter.js new file mode 100644 index 0000000..736ce30 --- /dev/null +++ b/src/encrypter.js @@ -0,0 +1,88 @@ +module.exports = async (text, key = undefined) => { + const KEY_LENGTH = 50 + const ENCRYPTION_SEPARATOR_STRING = '.'; + const GENERATE_RANDOM_KEY = async (numberBuffer) => { + key = []; + //LOAD KEY + for (var i = 0; i < KEY_LENGTH; i++) { + //SET numberBuffer TO RANDOM NUMBER BETWEEN 0 AND 52 + numberBuffer = await crypto.randomInt(0, 52 + 1); + + //GET CHAR FROM numberBuffer + if (numberBuffer > 26) { + numberBuffer += 70; + } else { + numberBuffer += 64; + } + + key[i] = numberBuffer; + } + } + + //VARIABLES + var keyRand = KEY_LENGTH; + var returningText = new String(); + var textBuffer; + var textKeyChain = []; + textKeyChain.length = text.length; + var numberBuffer; + + if (key < KEY_LENGTH) { + return { exitCode: -2 }; + } + if (!key) { + await GENERATE_RANDOM_KEY(numberBuffer); + } + //LOAD textKeyChain + for (var i = 0; i < textKeyChain.length; i++) { + numberBuffer = i; + + //CHECK, IF numberBuffer ISN'T GREATER THAN KEY_LENGTH AND CUT UNUSED LENGTH + while (numberBuffer >= key.length) + { + numberBuffer -= key.length; + } + + if (typeof key === 'string') { + key = key.split(""); + key = key.map(element => element.charCodeAt(0)); + } + + //FILL textKeyChain + if ((numberBuffer + 1) % 3 == 0) + { + textKeyChain[i] = key[numberBuffer] * key[numberBuffer + 1]; + } else if ((numberBuffer + 1) % 2 == 0) + { + textKeyChain[i] = key[numberBuffer] - key[numberBuffer + 1]; + } else + { + textKeyChain[i] = key[numberBuffer] + key[numberBuffer + 1]; + } + } + + //ACTUALLY ENCRYPT TEXT + for (var i = 0; i < text.length; i++) { + textKeyChain[i] -= text[i].charCodeAt(0); + } + + numberBuffer = 0; + + //LOAD returningText + for (var i = 0; i < textKeyChain.length; i++) { + textBuffer = Math.floor(Math.log10(Math.abs(textKeyChain[i]))); + + textBuffer = textKeyChain[i].toString(); + + returningText += textBuffer; + + if (i != textKeyChain.length - 1) + { + returningText += ENCRYPTION_SEPARATOR_STRING; + } + + textBuffer = undefined; + } + key = key.map(element => String.fromCharCode(element)).join(""); + return { exitCode: 0, value: returningText, key: key }; +} \ No newline at end of file