# C# Encrypt and Decrypt the digits

## Recommended Posts

Hi, I want to encrypt 16 number digits in to 5 digits and decrypt it in C#. So can anybody help me out to write a proper algo. for it ?

##### Share on other sites

So how are you going to encode 10**16 possible values into 10**5 values? Since 5 is smaller than 16, each 10**5 value is used on average 10**(16-5) = 10**11 times.

Let's make it much simpler, say I want to encode values 10, 20, 30, and 40 into values 1 and 2. I can say

10 goes to 1,

20 goes to 1,

30 goes to 2, and

40 goes to 2.

However, if you get a 1, there is no information whether it was a 10 or a 20. That information is lost due to going from 4 numbers (10, 20, 30, 40) to 2 numbers (1 and 2).

To decrypt successfully, you need at least the same amount of information in the encrypted form.

##### Share on other sites

It sounds like you want one of two things, neither of which I'd call encrypting per se.

Forcing into X number of digits implies you want a hash function, however, hashing cannot generally be reversed.  Adding all the numbers than padding with zeros, or splitting the 16 digits into 4 of 4 digits and adding them, would be (very simple and generally bad) forms of hashing.

When you say you want to restore it that sound more like compression; however, you aren't going to be able to compress to a consistent 5 digits without loss.  I'm not an expert on compression by any means, but compression will generally result in a variable size change based on how much repetition or patterning you have -- possibly even make some numbers bigger.  Lossy compression can shrink things more but at the cost that won't get back exactly what you put in (MP3 and JPEGs are lossy).  Simply put, you can't degrees the amount of information without losing some of it.  A simple lossless compression might work by digit followed by the number of repetitions:   1111222444444333 would become 14234633, less but more than 5, 123456789123456 would actually double in size to 112131415161718191112131415161; there are much smarter methods that I don't really understand but I think most work better on larger amounts of data that a sequence of 16.  (More than 9 in a row would also break that.) Anyway, as I said, I don't really know any real compression algorithms but I think this illustrates the problem.  What you're asking, as I understand it, is logically impossible.

Now, if you know you will have less than 100k (10^5) sequences, never more, you could map them to arbitrary 5 digit numbers -- storing them in a table.

Edited by JaredBGreat

##### Share on other sites

Thank you Alberth, Is it not possible to encrypt in shorten value ?

Thank you very much JaredBGreat.

##### Share on other sites

What do you mean with "encryption", what is the goal of this operation?

##### Share on other sites

I think a better question to ask, is this homework?  Because it sounds like a homework problem.

##### Share on other sites

@Alberth.. I want to get motherboard ID and encrypt it in 5 digits, Then i send it to the client as this so one user can use it and only one machine should play the game. Then when I decrypt those 5 digits, i should get motherboard ID again

@CrazyCdn, Hahaha, No no man its not a home work actually I just want a serial key for a user that can only play the game if he/she has a serial key for a machine.

##### Share on other sites

By the way, I am also interested in finding a solution to this problem.

##### Share on other sites

But why does it have to be 5 digits?  If you just need it to be reasonably safe you could use every other digit(up to 5), then only compare those digits...  For that matter you could then take those 5 digits and actually encrypt them if you wanted...  But that may still have some false positive matches.  Depends on how secure you need it to be.

##### Share on other sites

@Banstel, Thank you so much

@Septopus, Thank you for your answer, It should be 5 digits and it should more secure because I need to apply for the slot game for a user.

## Create an account

Register a new account

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 23
• 18
• 11
• 19
• ### Forum Statistics

• Total Topics
634404
• Total Posts
3017266
×