You are looking at an obfuscation algorithm that is based on converting characters into their numerical representation and storing them as integers in an array. The code you posted is decrypting this data and converting it back into a readable string. Here's how it works:
Each integer in the array erp is made up of four bytes, and each byte represents the ASCII value of one character.
In the loop, the code is dividing the integer by powers of 256 to get the individual bytes back. This is done by utilizing the bitwise layout of the 32-bit integer.
Math.floor((tmp/Math.pow(256,3))) is extracting the first (most significant) byte.
Math.floor((tmp/Math.pow(256,2))) is extracting the second byte.
Math.floor((tmp/Math.pow(256,1))) is extracting the third byte.
Math.floor((tmp/Math.pow(256,0))) is extracting the fourth (least significant) byte.
The String.fromCharCode() function is used to convert each byte back to a character based on ASCII values.
Now, for your question about the value 1111702575. This can be explained as follows:
- 1111702575 is
0x42430A3C in hexadecimal.
- This breaks down into the ASCII codes:
0x42 (B), 0x43 (C), 0x0A (line feed), 0x3C (<).
If you are looking to create obfuscation, you would need to do the reverse process: convert characters to ASCII codes, then combine them into 32-bit integers. Here is an example code to achieve that:
Decoding:
var encodedArray = [1013988929, 1111702575, 28734]; // Declare an array with encoded values
var decodedString = ''; // Initialize an empty string to hold the decoded string
// Loop through each element in the encoded array
for (var i = 0; i < encodedArray.length; i++) {
var value = encodedArray[i]; // Store the current encoded value in 'value'
var decodedChunk = ''; // Initialize an empty string to hold the decoded chunk
// Keep looping until 'value' becomes 0
while (value > 0) {
var charCode = value % 256; // Calculate the remainder when 'value' is divided by 256
decodedChunk = String.fromCharCode(charCode) + decodedChunk; // Convert the remainder to a character and prepend it to 'decodedChunk'
value = Math.floor(value / 256); // Divide 'value' by 256 and store the result back into 'value'
}
decodedString += decodedChunk; // Append the 'decodedChunk' to the 'decodedString'
}
console.log(decodedString); // Output the 'decodedString' to the console
Encoding:
function encodeString(input) { // Define a function named 'encodeString' that takes an input string
var encodedArray = []; // Initialize an empty array to hold the encoded values
var currentChunk = 0; // Initialize a variable to accumulate the encoding of up to 4 characters
var bytesInChunk = 0; // Initialize a variable to keep track of the number of characters in the current chunk
// Loop through each character in the input string
for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i); // Get the ASCII value of the current character
currentChunk = currentChunk * 256 + charCode; // Multiply 'currentChunk' by 256 and add the ASCII value of the current character
bytesInChunk++; // Increment the number of characters in the current chunk
// Check if 'bytesInChunk' is equal to 4
if (bytesInChunk === 4) {
encodedArray.push(currentChunk); // Append 'currentChunk' to 'encodedArray'
currentChunk = 0; // Reset 'currentChunk'
bytesInChunk = 0; // Reset 'bytesInChunk'
}
}
// Check if there are any characters left in 'currentChunk'
if (bytesInChunk > 0) {
encodedArray.push(currentChunk); // Append the remaining 'currentChunk' to 'encodedArray'
}
return encodedArray; // Return the 'encodedArray'
}
var inputString = '<p>ABC</p>'; // The string to be encoded
var encodedArray = encodeString(inputString); // Call 'encodeString' function with 'inputString' as an argument
console.log(encodedArray); // Output the 'encodedArray' to the console
It's important to note that the provided code is a simple and easily reversible obfuscation technique. It is not a strong security measure. For more robust obfuscation, advanced algorithms and transformations are used to make the code harder to understand and reverse engineer.
If you're interested in creating obfuscation techniques, there are libraries and tools available that provide more sophisticated and secure methods. These tools often offer various obfuscation transformations, including name mangling, code splitting, and control flow obfuscation, among others.
I've even provided a PHP based example on GitHub: https://github.com/DevilsNerve/HTML-obfuscator
Remember that while obfuscation can make code more difficult to understand, it does not provide absolute security. Determined attackers can still reverse engineer obfuscated code given enough time and resources.
All the best,
Austen