Numbers to Expressions
Protection ID: numbersToExpressions
Numbers to Expressions is a powerful obfuscation technique that converts numeric literals in your code into complex mathematical expressions. This adds significant complexity to your code, making it harder to understand while preserving its original functionality.
This protection is available in all editions of ByteHide Shield.
How It Works
Numbers to Expressions works by replacing simple numeric literals (such as 42
or 1000
) with equivalent but more complex mathematical expressions that evaluate to the same value at runtime. For example, a simple number like 5
might be transformed into something like (0x84e*0x3+-0xff8+0x41*0x67+-0x10b4+-0xd93)/0x2b
.
This transformation makes it harder for someone analyzing your code to quickly understand the values being used, especially when combined with other obfuscation techniques.
Parameters
- Enabled boolean : Enables the Numbers to Expressions transformation. false by default
This transformation applies to numeric literals throughout your code. The expressions generated vary in complexity to maintain unpredictability.
Configuration Examples
Basic Configuration (shield.config.json)
{
"numbersToExpressions": true
}
Combined with Control Flow Flattening
{
"numbersToExpressions": true,
"controlFlowFlattening": true
}
Build Tool Integration (Webpack)
// webpack.config.js
const ByteHideShieldPlugin = require('@bytehide/webpack-shield');
module.exports = {
// ... other webpack config
plugins: [
new ByteHideShieldPlugin({
projectToken: 'your-project-token',
config: {
numbersToExpressions: true
}
})
]
}
Code Transformation Example
Original Code
function calculateDiscount(price, discountPercent) {
const discount = price * (discountPercent / 100);
if (discountPercent >= 50) {
console.log("Large discount applied");
}
const finalPrice = price - discount;
if (finalPrice < 10) {
return 10; // Minimum price
}
return finalPrice;
}
Transformed Code (with Numbers to Expressions)
function calculateDiscount(price, discountPercent) {
const discount = price * (discountPercent / (0x3e8/0x28+0x1a*0x2-0x20));
if (discountPercent >= (0x12c/0x6+0x5*0x8-0xa)) {
console.log("Large discount applied");
}
const finalPrice = price - discount;
if (finalPrice < (0x1e-0xa+0x2*0x2-0x6)) {
return (0x36/0x3-0x1a+0x2a); // Minimum price
}
return finalPrice;
}
Transformed Code (with multiple protections)
function _0x38a2(_0x21b34f, _0x5af2c9) {
const _0x183d2e = _0x5c8e;
const _0x25b8ad = _0x21b34f * (_0x5af2c9 / (0x7c0/0x10+0x20*0x2-0x4));
if (_0x5af2c9 >= (0x10a/0x5+0x8*0xa+0x2)) {
console[_0x183d2e(0x12)](_0x183d2e(0x32));
}
const _0x2c8b37 = _0x21b34f - _0x25b8ad;
if (_0x2c8b37 < (0x18*0x5-0x5c/0x4)) {
return (0x1a-0x2+0x4*0x3-0xc); // Minimum price
}
return _0x2c8b37;
}
When to Use
Numbers to Expressions is particularly useful in these scenarios:
- Mathematical algorithms: Hide formulas and calculations by obfuscating the numeric values
- Threshold values: Mask important threshold values that determine program behavior
- Financial calculations: Obscure financial constants and formulas
- Key validation: Hide numeric values used in license or key validation routines
For optimal results:
- Use in combination with identifier renaming
- Apply to code with critical numeric constants
- Combine with Control Flow Flattening for maximum confusion
- Consider performance impact when applying to performance-critical code
Compatibility Notes
Numbers to Expressions has the following compatibility considerations:
- Browser compatibility: Works in all major browsers
- Performance impact: Slight runtime performance overhead due to expression evaluation
- Code size: Increases code size as numbers are replaced with larger expressions
- Debugging: Makes debugging more difficult as numeric values are not immediately apparent
While this protection has minimal performance impact for most applications, be cautious when applying it to code with heavy mathematical operations in tight loops, as it may affect performance in these specific scenarios.
Related Protections
For maximum security, combine Numbers to Expressions with: