Encrypt Decrypt Strings

This subflow encrypts or decrypts a string according to some input parameters.

The subflow offers two types of encryption: The first one uses a shift number in order to complete the encryption or decryption. The second one requires a keyword for the encryption of decryption to work. The input for the flow should be an array with 4 fields. Example input: ["String for example!", 7, "encrypt", "keyword"]. The first field is the string that you want to be encrypted or decrypted. The second field is a shift number that is utilized if you choose to use the first encryption algorithm. The third field is used to indicate the process that you want to execute, encryption or decryption. The fourth field serves to purposes, holding the keyword that the second algorithm uses and also indicating what algorithm is going to be used for the encryption or decryption. If you leave the string of the fourth field empty the algorithm that utilizes the shift number will be used. If the string in this field is not empty the second algorithm is going to be used, utilizing the value of this string as a keyword. If you choose to use the second algorithm the shift number is not going to affect the process, you can leave that field empty of pass a random number as an argument. If you choose to use the first algorith leave the last field empty as aformentioned and make sure the second field that holds the shift number is NOT empty or zero. If it is empty there will be no output and if it is 0 the output will be the string you provided with no encryption at all.

[{"id":"b63c0686a5f9c2a1","type":"subflow","name":"Encrypt","info":"","category":"","in":[{"x":60,"y":160,"wires":[{"id":"649d7ceb44271465"}]}],"out":[{"x":820,"y":160,"wires":[{"id":"3b3518c922197d6c","port":0},{"id":"1ca5903a9ebb5e3a","port":0},{"id":"41b47f15bedad10d","port":0},{"id":"a2f58202307bb5ae","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"3b3518c922197d6c","type":"function","z":"b63c0686a5f9c2a1","name":"Encryption shift","func":"const alphabet = [\n    \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\",\n    \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\",\n    \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"\n];\n\nfunction encrypt(character, shift_number) {\n    let character_exists =\n    alphabet.includes(character.toUpperCase())\n\n    if (character_exists) {\n        let position_of_character =\n        alphabet.indexOf(character.toUpperCase()) \n\n        let encryption_character_position = \n        (position_of_character + shift_number) %\n        alphabet.length\n        \n        return alphabet[encryption_character_position]\n    }else {\n        return character\n    }\n}\n\nlet encrypted_characters = []\nlet shift = msg.payload[1]\n\nfor (let item of msg.payload[0]) {\n    encrypted_characters.push(encrypt(item,shift))\n}\n\nlet encrypted_text = encrypted_characters.join(\"\");\nmsg.payload[0] = encrypted_text\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":80,"wires":[[]]},{"id":"7920a9487196d790","type":"switch","z":"b63c0686a5f9c2a1","name":"Enc_Dec_1","property":"payload[2]","propertyType":"msg","rules":[{"t":"eq","v":"encrypt","vt":"str"},{"t":"eq","v":"decrypt","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":100,"wires":[["3b3518c922197d6c"],["1ca5903a9ebb5e3a"]]},{"id":"1ca5903a9ebb5e3a","type":"function","z":"b63c0686a5f9c2a1","name":"Decryption shift","func":"const alphabet = [\n    \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\",\n    \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\",\n    \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"\n];\n\nfunction decrypt(character, shift_number) {\n    let character_exists =\n        alphabet.includes(character.toUpperCase())\n\n    if (character_exists) {\n        let position_of_character =\n            alphabet.indexOf(character.toUpperCase())\n\n        let decryption_character_position =\n            (position_of_character + shift_number) %\n            alphabet.length\n\n        return alphabet[decryption_character_position]\n    } else {\n        return character\n    }\n}\n\nlet decrypted_characters = []\nlet shift = (alphabet.length - msg.payload[1]) %\n            alphabet.length\n\nfor (let item of msg.payload[0]) {\n    decrypted_characters.push(decrypt(item, shift))\n}\n\nlet decrypted_text = decrypted_characters.join(\"\");\nmsg.payload[0] = decrypted_text\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":120,"wires":[[]]},{"id":"649d7ceb44271465","type":"switch","z":"b63c0686a5f9c2a1","name":"Enc_type","property":"payload[3]","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":200,"y":160,"wires":[["7920a9487196d790"],["f3b22b896b1df6b3"]]},{"id":"f3b22b896b1df6b3","type":"switch","z":"b63c0686a5f9c2a1","name":"Enc_Dec_2","property":"payload[2]","propertyType":"msg","rules":[{"t":"eq","v":"encrypt","vt":"str"},{"t":"eq","v":"decrypt","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":220,"wires":[["41b47f15bedad10d"],["a2f58202307bb5ae"]]},{"id":"41b47f15bedad10d","type":"function","z":"b63c0686a5f9c2a1","name":"Encryption key","func":"const alphabet = [\n    \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\",\n    \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\",\n    \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"\n];\n\nfunction encrypt(text_character ,keyword_character) {\n    let character_exists =\n    alphabet.includes(text_character.toUpperCase())\n\n    if (character_exists) {\n        let position_of_text_character =\n        alphabet.indexOf(text_character.toUpperCase())\n\n        let position_of_keyword_character =\n        alphabet.indexOf(keyword_character.toUpperCase())\n\n        let encryption_character_position =\n        (position_of_text_character + position_of_keyword_character)\n        % alphabet.length\n\n        return alphabet[encryption_character_position]\n    }else {\n        return text_character\n    }\n}\n\nlet encrypted_characters = []\nlet keyword_index = 0\n\nfor (let item of msg.payload[0]) {\n    keyword_index = keyword_index === msg.payload[3].length - 1 ?\n    0 : keyword_index + 1; \n    encrypted_characters.push(encrypt(item, msg.payload[3][keyword_index]))\n}\n\nlet encrypted_text = encrypted_characters.join(\"\");\nmsg.payload[0] = encrypted_text\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":200,"wires":[[]]},{"id":"a2f58202307bb5ae","type":"function","z":"b63c0686a5f9c2a1","name":"Decryption key","func":"const alphabet = [\n    \"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\",\n    \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\",\n    \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"\n];\n\nfunction decrypt(text_character, keyword_character) {\n    let character_exists =\n    alphabet.includes(text_character.toUpperCase())\n\n    if (character_exists) {\n        let position_of_text_character =\n        alphabet.indexOf(text_character.toUpperCase())\n\n        let position_of_keyword_character =\n        alphabet.indexOf(keyword_character.toUpperCase())\n\n        let decryption_character_position =\n        (position_of_text_character - position_of_keyword_character + alphabet.length)\n        % alphabet.length\n\n        return alphabet[decryption_character_position]\n    } else {\n        return text_character\n    }\n}\n\nlet decrypted_characters = []\nlet keyword_index = 0\n\nfor (let item of msg.payload[0]) {\n    keyword_index = keyword_index === msg.payload[3].length - 1 ?\n    0 : keyword_index + 1;\n    decrypted_characters.push(decrypt(item, msg.payload[3][keyword_index]))\n}\n\nlet decrypted_text = decrypted_characters.join(\"\");\nmsg.payload[0] = decrypted_text\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":580,"y":240,"wires":[[]]},{"id":"805ea8234e1dca5a","type":"subflow:b63c0686a5f9c2a1","z":"1d0522c0f273c708","name":"","x":500,"y":260,"wires":[[]]}]

Flow Info

Created 2 years, 1 month ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x4)
  • switch (x3)
Other
  • subflow (x1)
  • subflow:b63c0686a5f9c2a1 (x1)

Tags

  • encrypt
  • decrypt
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option