-2

We want to generate coupon code series and then encrypt those coupon code in numeric only and that must also be able to decrypt.

We will be sharing encrypted code as coupon code with customer.

These are the conditions:

  1. encrypted code must not be in series or not easily able to guess. It must be NUMERIC
  2. Encrypted code must be having specific number of digits.

Example :

We generate coupon code from 1 to 100 (needs in millions and plainCode will be either numeric or alphanumeric doesn't matter).

We want share encrypted numeric coupon code with specific digits to user. i.e. if code is 52 then encrypted code gives us between 10000 to 99999 (if 5 digits are specified or it need to be fix number of digits)

We are also able to decrypt the same.

HOW WE CAN DO THIS. ANY IDEA OR SUGGESTIONS?

Thank You.

NOTE:

  1. Above is just an example. possibilities are in millions.
  2. Security is not a major concern. just not able to guess is matters a lot.
  3. encrypted code must be in numeric.
murtaza.webdev
  • 3,523
  • 4
  • 22
  • 32
  • you can generate uuids and encode-decode – ΦXocę 웃 Пepeúpa ツ Oct 20 '16 at 14:29
  • *"if code is 52 then encrypted code gives us between 10000 to 99999"* - That's already secure as only **you** know the values relate to 52. There are only 100 possibilities why not use a lookup table. – Alex K. Oct 20 '16 at 14:32
  • 1
    If you've only got 100 coupons, just pick a different random "encrypted" number for each of them. If only you have the table to map back from the "encrypted" to "decrypted" numbers, it's basically unbreakable (barring rubber-hose cryptography, or plain carelessness). – Andy Turner Oct 20 '16 at 14:33
  • 1 to 100 was just an example. possibilities are in millions. Yes lookup table is their but trying to get better way... – murtaza.webdev Oct 20 '16 at 18:28
  • 1
    You should look up the topic of [Format-preserving encryption](https://en.wikipedia.org/wiki/Format-preserving_encryption) for problems like these. – President James K. Polk Oct 20 '16 at 18:32
  • yes have looked and FPE. any good algo for the same recommended which return in specific number of digits. we have one which gives encryt number less than specified number but we want in between specific range to maintain number of digits without 0 appending. – murtaza.webdev Oct 20 '16 at 18:33

1 Answers1

0

if security is not your concern...

int key=123456;

List<int> availableNumbers=new List<int>();
for(int i=10000;i<100000;i++)
    availableNumbers.Add(i);

Dictionary<int,int> forwards=new Dictionary<int,int>();
Dictionary<int,int> backwards=new Dictionary<int,int>();

for(int i=1;i<1000;i++){
    var rnd=new System.Random(key+i);
    int pos=rnd.Next(availableNumbers.Count);
    int number=availableNumbers[pos];
    availableNumbers.RemoveAt(pos);
    forwards[i]=number;
    backwards[number]=i;
}

foreach(var kvp in forwards)
{
    Console.Out.WriteLine("{0,5}=>{1}",kvp.Key,kvp.Value);
}
DarkSquirrel42
  • 10,167
  • 3
  • 20
  • 31