2

Im trying to reverse engineering some Javascript code and recode it in PHP. There is a String which is "converted" with parseInt(String, 36) to an Integer. I need a posibility to convert the integer back to the secret String in PHP, without knowing the secret string.

secretCode = "0vo8fz4kvy03";
decode = parseInt(secretCode, 36).toString();
console.log(decode); //= 115802171408044510

How i can do that in PHP? 115802171408044510 back to 0vo8fz4kvy03

This interger contains some informations:

decode="115802171408044510";
storeID = decode.substr(0, 4); // 1158
posID = decode.substr(12, 2); // 04
orderID = decode.substr(14, 2); // 45
day = decode.substr(6, 2); // 17
month = decode.substr(4, 2); // 02
hour = decode.substr(8, 2); // 14
minutes = decode.substr(10, 2); // 08

I would like to edit this values above and convert this back to a "secretCode" String. They are doing exactly this somehow server-side.

Kevin
  • 23
  • 4
  • you may have a look to this [question](https://stackoverflow.com/q/55646698/1447675), for gerater numbers than are safe integer numbers. why does this question have a [tag:javascript] tag? – Nina Scholz Jun 01 '19 at 15:55
  • Correct me if I am wrong but `115802171408044510 = vo8fz4kvxzy` and `0vo8fz4kvy03 = 115802171408044515` – Dharman Jun 01 '19 at 16:09

6 Answers6

3

You can't. The number overflows the maximum safe integer size, therefore it is impossible to reverse this process (as the number gets rounded to the next safe integer). Therefore the code is broken.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I added some Informations at the end of the Question, to make it better understandable. So, there is no possibility for me to do this? – Kevin Jun 01 '19 at 16:06
2

You can try.

decode.toString(36)
  • Tried this already, but that gives me another result. But altought, i need a possibility to do this in PHP. console.log(parseInt("0vo8fz4kvy03", 36).toString(36)); //= vo8fz4kvy00 – Kevin Jun 01 '19 at 15:51
2

To convert your javascript encoded number string from base 10 to base 36:

$decode = "115802171408044510";
echo base_convert ( $decode , 10 , 36 );
2

You could take BigInt and convert large values to decimal or back to string.

function convertFrom(value, radix) {
    return [...value.toString()]
        .reduce((r, v) => r * BigInt(radix) + BigInt(parseInt(v, radix)), 0n);
}

function convertTo(value, radix) {
    var result = '',
        r = BigInt(radix);
     
    do {
        result = (value % r).toString(36) + result;
        value = value / r;
    } while (value);
    return result;
}

var secretCode = "0vo8fz4kvy03",
    value = convertFrom(secretCode, 36),
    encode = convertTo(value, 36);

console.log(value.toString());
console.log(encode.toString());
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

The Number.prototype.toString() method receives an optional argument, which lets you set the base of the result.

So all you need to do is (+decode).toString(36).

secretCode = "0vo8fz4kvy03";
decode = parseInt(secretCode, 36).toString();
console.log(decode); //= 115802171408044510
console.log((+decode).toString(36));

Notice that I'm converting your decode to a number. This is needed because you converted the result of parseInt directly to a string, so you never actually hold the number.

ziggy wiggy
  • 1,039
  • 6
  • 6
0

The intval function in PHP is the closest to parseInt in Javascript including the support for radix parameter:

$int = intval('9', 36);

In Javascript, parseInt returns NaN for inputs that can not be parsed to integer; but in PHP, intval returns 0.

Look for the details about intval function in php docs.

muratgozel
  • 2,333
  • 27
  • 31