A simple method to obfuscate a string is to use a script like this, using Node.js.
This code is in Typescript.
import readline from 'readline';
const rl = readline.createInterface({
input:process.stdin,
output:process.stdout
});
const randNum = (max:number=10000)=>Math.floor(Math.random() * max);
// finds an equation that will make the target number.
const findEquation = (target:number)=>{
let equation = "";
const rand = randNum();
const diff = target - rand;
const rand2 = randNum()
const product = diff * rand2;
equation = `${rand}+(${product} / ${rand2})`;
return equation;
}
const randCharSequence = (length:number)=>{
let str = "";
for(let i = 0; i < length; i++){
str += String.fromCharCode(randNum(256));
}
return str
}
const sep = randCharSequence(8)
rl.question("Enter the string to obfuscate:\n", (str)=>{
let obfuscated = "(''";
str.split("").forEach(char=>{
const code = findEquation(char.charCodeAt(0));
obfuscated += `+(String.fromCharCode(${code})+\"${sep}\")`
})
obfuscated += `).split(\"${sep}\").join('')`;
console.log("Obfuscated String:");
console.log(obfuscated);
rl.close();
});
When you run this script, you are prompted to enter the string to obfuscate. Then, the script prints out an obfuscated string. It uses equations that involve numbers of up to 10000 to generate numbers, that then get converted and concatonated into a string. But, that's not all. It adds a randomly generated string of 8 characters to the output, that gets removed when it is ran.
This makes it harder to get a look at the string when reading the code, but keep in mind, that a person with access to the code, can easily add a console.log
statement, and then get the string.
Note, this isn't encryption, it just makes the code harder to understand and read.