Build Encryption Method from Decryption (Reverse Modulo)
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).