Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Decrypting source message from a code based on some algorithm in JavaScript
We need to write a JavaScript function that decrypts a message by reversing an encryption algorithm. The encryption process involves reversing the string and replacing letters with their ASCII codes in quotes.
Understanding the Encryption Algorithm
The original encryption algorithm works as follows:
- Reverse the message string
- Replace every letter with its ASCII code in quotes (A becomes '65', h becomes '104')
- Keep digits and spaces unchanged
The Decryption Function
To decrypt, we need to reverse this process by applying the same algorithm to the encrypted message:
const str = '12 hello world 30';
const decryptString = (str = '') => {
const alpha = 'abcdefghijklmnopqrstuvwxyz';
let res = '';
// Process string from right to left (reverse it)
for(let i = str.length - 1; i >= 0; i--){
const el = str[i];
// If character is a letter, convert to ASCII code
if(alpha.includes(el.toLowerCase())){
res += `'${el.charCodeAt(0)}'`;
} else {
// Keep digits and spaces as is
res += el;
}
}
return res;
};
console.log(decryptString(str));
03 '100''108''114''111''119' '111''108''108''101''104' 21
How It Works
The function processes each character of the input string:
// Breaking down the process for 'hello'
const word = 'hello';
console.log('Original:', word);
// Reverse and convert each letter
let encrypted = '';
for(let i = word.length - 1; i >= 0; i--){
const char = word[i];
encrypted += `'${char.charCodeAt(0)}'`;
}
console.log('Encrypted:', encrypted);
console.log('ASCII codes: o=111, l=108, l=108, e=101, h=104');
Original: hello Encrypted: '111''108''108''101''104' ASCII codes: o=111, l=108, l=108, e=101, h=104
Complete Example with Different Input
const message1 = 'ABC 123';
const message2 = 'test';
console.log('Input:', message1);
console.log('Output:', decryptString(message1));
console.log();
console.log('Input:', message2);
console.log('Output:', decryptString(message2));
function decryptString(str = '') {
const alpha = 'abcdefghijklmnopqrstuvwxyz';
let res = '';
for(let i = str.length - 1; i >= 0; i--){
const el = str[i];
if(alpha.includes(el.toLowerCase())){
res += `'${el.charCodeAt(0)}'`;
} else {
res += el;
}
}
return res;
}
Input: ABC 123 Output: 321 '67''66''65' Input: test Output: '116''115''101''116'
Key Points
- The function reverses the string by iterating from the last character to the first
- Letters are converted to their ASCII codes using
charCodeAt(0)and wrapped in quotes - Numbers and spaces remain unchanged in their new positions
- Both uppercase and lowercase letters are handled correctly
Conclusion
This decryption function effectively reverses a string while converting letters to ASCII codes. The algorithm maintains the original structure while transforming alphabetic characters into their numeric representations.
Advertisements
