简体   繁体   中英

String.fromCharCode() not working as intended

So, I'm trying to make a simple encryption and decryption application where the user inputs are upped by some amount of characters. For Eg: user inputs "abcd" then it converts to "bcde".

Code used for encryption:

 const encryptedInput = (input) => { try { // convert to array let splitInput = sanitize(input).split(""); //down the characters by derived let mapped = splitInput.map((element) => element == " " ? element : String.fromCharCode(element.charCodeAt(0) + process.env.NUMBEROFCHARS) ); let encryptedInput = mapped.join(""); // result return encryptedInput; //return } catch (err) {} };

The above code works perfectly fine. Its when I try to decrypt it back to original form, It gives me weird symbols and not the original message.

Code used for decryption:

 const decryptedInput = (input) => { try { // convert to array let splitInput = sanitize(input).split(""); //down the characters by derived let mapped = splitInput.map((element) => element == " " ? element : String.fromCharCode(element.charCodeAt(0) - process.env.NUMBEROFCHARS) ); let decryptedInput = mapped.join(""); // result return decryptedInput; //return } catch (err) {} };

Output / Result :

During encryption:
Input : abcd
Output : bcde

During decryption:
Input : bcde
Output : ϕϟϩϳ

But I want the output as abcd not ϕϟϩϳ.

所以我重新启动了我的电脑,代码现在可以工作了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM