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 ?
Encrypt and Decrypt the digits
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.
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.
Thank you Alberth, Is it not possible to encrypt in shorten value ?
Thank you very much JaredBGreat.
I think a better question to ask, is this homework? Because it sounds like a homework problem.
@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.
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.
@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.
This is very similar then to dealing with a password -- you want to know that the other party has the right number, but you don't want to share the number. The way this is usually done with passwords is that a hash is used; the hash is usually 256 bits long (sha-256 or sha-3 cryptographic hash algorithms). The hashes are stored on the server (sometimes with some extra randomness called a salt). You don't actually decrypt the hash; instead, the number is re-hashed and the hashes are compared. The hashes are usually larger than the password, too (a 10 character password would be 10 bytes but become a 32-byte hash).
One possibility would be to hash both MOBO data from both PCs, they send in the hash or yours and their info, which you then hash and combine the hashes and look up the combined result. Note that if you're worried about the data being stolen in-rout you might want to use a secure connection similar to what secure web pages use. There are networking libraries for that (Google AES). Note, that security can get complicated -- but if you aren't that concerned about high security giving a fairly simple hash of your MOBO info might work. There are many hash algorithms, and most languages have some standard library support for hashing, which might not be a cryptographic hash but if you aren't concerned about high security they might work. Then, if that's all you want you might as well just assign a random/made-up ID code.
EDIT:
Five digits in and of itself it inherently not secure -- that could be cracked by brute force in a fraction of a second. It sounds to me like what you need is two things: A randomly generated number to act as a ticket (five digits, well OK I guess) plus network security. If Unity (a guess from C#) then I just googled for you and this might help: https://docs.unity3d.com/ScriptReference/Network.InitializeSecurity.html; If not you might need to find a third party security library; if security is important to you I will warn the trying to roll your own is a very bad idea in most cases.