Philip - 6 months ago 67

Ruby Question

I need a simple encryption for some text strings. I want to create coupon codes and make them look cool so subsequently created code should look very different. (And besides looking cool, it shouldn't be easy to guess a code.) But I want to be able to decrypt them again. So the algorithm must be reversible.

I alread tried some stuff with moving bits around so they look kind of random already. But two subsequent codes (just one bit different) of course look very similar.

Any suggestions? I would like to do that without using external gems.

Philip

Answer

I guess the simplest way is a * x + b (mod 2^n)

Note that you need to calculate the inverse of a on 2^n, you can to that with Wolfram Alpha using extended GCD.