Build Encryption Method from Decryption (Reverse Modulo) So I've been analysing some Javascript Obfuscator. I understand how the strings are decrypted, my interest is now into building the actual encryption method for those strings. I'm not going to post the full decryption method as my only concern is the actual cryptography part, but basically the algo just swap character indexes all around, so my goal is to make an encryption method that swap the characters to the proper places.
My question is related to this part:
for (var r = 0; r &lt; x; r++) {
    var f = g * (r + 72) + (g % 30846);
    var y = g * (r + 104) + (g % 25098);
    var d = f % x;
    var l = y % x;
    var n = k[d];
    k[d] = k[l];
    k[l] = n;
    g = (f + y) % 2012852
}

It loops over all the characters, starting g value is 1245836.
After one cycle, the value of g becomes 1907150, I'm wondering how you can go from that new g value back to the original one.
What are the maths to inverse those operations:
var f = g * (r + 72) + (g % 30846);
var y = g * (r + 104) + (g % 25098);

Not looking to be spoon fed with code. I'm actually trying to understand the maths.
Thanks a lot
so my goal is to make an encryption method that swap the characters to the proper places.

If that's the goal, that's what you do.
You were thinking about &quot;how do I generate the $g$ (actually, the $l, d$ values) in the reverse order&quot;; actually, there's no need.  You just compute the successive $l, d$ values generated by the algorithm, storing them in an array, rather than using them to do the exchanges.  Then, once you have generates all $x$ $l, d$ pairs, then perform the swaps in the reverse order (that is, starting with the last $l, d$ pair generated).