Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:47:20+00:00 2026-05-27T14:47:20+00:00

I’m looking for a way to encrypt/obfuscate an integer ID into another integer. More

  • 0

I’m looking for a way to encrypt/obfuscate an integer ID into another integer. More precisely, I need a function int F(int x), so that

  • x<->F(x) is one-to-one correspondence (if x != y, F(x) != F(y))
  • given F(x), it’s easy to find out x – so F is not a hash function
  • given x and F(x) it’s hard/impossible to find out F(y), something like x ^ 0x1234 won’t work

For clarity, I’m not looking for a strong encryption solution, it’s only obfuscation. Imagine a web application with urls like example.com/profile/1, example.com/profile/2 etc. The profiles themselves are not secret, but I’d like to prevent casual voyeurs to view/fetch all profiles one after another, so I’d rather hide them behind something like example.com/profile/23423, example.com/profile/80980234 etc. Although database-stored tokens can do the job quite easily, I’m curious if there’s some simple math available for this.

One important requirement I wasn’t clear about is that results should look “random”, that is, given a sequence x,x+1,...,x+n , F(x),F(x+1)...F(x+n) shouldn’t form a progression of any kind.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:47:21+00:00Added an answer on May 27, 2026 at 2:47 pm

    Obfuscate it with some combination of 2 or 3 simple methods:

    • XOR
    • shuffle individual bits
    • convert to modular representation (D.Knuth, Vol. 2, Chapter 4.3.2)
    • choose 32 (or 64) overlapping subsets of bits and XOR bits in each subset (parity bits of subsets)
    • represent it in variable-length numberic system and shuffle digits
    • choose a pair of odd integers x and y that are multiplicative inverses of each other (modulo 232), then multiply by x to obfuscate and multiply by y to restore, all multiplications are modulo 232 (source: “A practical use of multiplicative inverses” by Eric Lippert)

    Variable-length numberic system method does not obey your “progression” requirement on its own. It always produces short arithmetic progressions. But when combined with some other method, it gives good results.

    The same is true for the modular representation method.

    Here is C++ code example for 3 of these methods. Shuffle bits example may use some different masks and distances to be more unpredictable. Other 2 examples are good for small numbers (just to give the idea). They should be extended to obfuscate all integer values properly.

    // *** Numberic system base: (4, 3, 5) -> (5, 3, 4)
    // In real life all the bases multiplied should be near 2^32
    unsigned y = x/15 + ((x/5)%3)*4 + (x%5)*12; // obfuscate
    unsigned z = y/12 + ((y/4)%3)*5 + (y%4)*15; // restore
    
    // *** Shuffle bits (method used here is described in D.Knuth's vol.4a chapter 7.1.3)
    const unsigned mask1 = 0x00550055; const unsigned d1 = 7;
    const unsigned mask2 = 0x0000cccc; const unsigned d2 = 14;
    
    // Obfuscate
    unsigned t = (x ^ (x >> d1)) & mask1;
    unsigned u = x ^ t ^ (t << d1);
    t = (u ^ (u  >> d2)) & mask2;
    y = u ^ t ^ (t << d2);
    
    // Restore
    t = (y ^ (y >> d2)) & mask2;
    u = y ^ t ^ (t << d2);
    t = (u ^ (u >> d1)) & mask1;
    z = u ^ t ^ (t << d1);
    
    // *** Subset parity
    t = (x ^ (x >> 1)) & 0x44444444;
    u = (x ^ (x << 2)) & 0xcccccccc;
    y = ((x & 0x88888888) >> 3) | (t >> 1) | u; // obfuscate
    
    t = ((y & 0x11111111) << 3) | (((y & 0x11111111) << 2) ^ ((y & 0x22222222) << 1));
    z = t | ((t >> 2) ^ ((y >> 2) & 0x33333333)); // restore
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.