none
Encrypt a number to a 6 unique digits code RRS feed

Answers

  • The number will be from 1 to 999,999 only

    Then this function should work

    public string encodeNumber(int num)
    {
    	if (num < 0) //or throw an exception
    		return "";
    	string alphanums = "0123456789abcdefghijklmnopqrstuvwxyz";
    	int[] nums = new int[6];
    	int pos = 0;
    
    	while (!(num == 0))
    	{
    		nums[pos] = num % nums.Length;
    		num /= alphanums.Length;
    		pos += 1;
    	}
    
    	string result = "";
    	for (int i = 0; i < alphanums.Length; i++)
    		result = alphanums[nums[i]].ToString() + result;
    
    	return result;
    }

    Note that you only need 4 alphanumeric characters to represent  the numbers 1 - 999,999

    • Edited by Blackwood Saturday, May 23, 2015 12:19 AM Correct code
    • Marked as answer by Fred BaoModerator Monday, June 1, 2015 9:32 AM
    Saturday, May 23, 2015 12:10 AM

All replies

  • You can't. The number of unique sets of 6 alphanumeric digits is 36 ^ 6 (about 2 billion). There are many more than  2 billion numbers in the universe.

    If you can be sure that the number is between 0 and 2 billion, it would be possible to come up with a way to convert it into 6 alphanumeric characters.

    Friday, May 22, 2015 11:51 PM
  • The number will be from 1 to 999,999 only

    Saturday, May 23, 2015 12:06 AM
  • The number will be from 1 to 999,999 only

    Then this function should work

    public string encodeNumber(int num)
    {
    	if (num < 0) //or throw an exception
    		return "";
    	string alphanums = "0123456789abcdefghijklmnopqrstuvwxyz";
    	int[] nums = new int[6];
    	int pos = 0;
    
    	while (!(num == 0))
    	{
    		nums[pos] = num % nums.Length;
    		num /= alphanums.Length;
    		pos += 1;
    	}
    
    	string result = "";
    	for (int i = 0; i < alphanums.Length; i++)
    		result = alphanums[nums[i]].ToString() + result;
    
    	return result;
    }

    Note that you only need 4 alphanumeric characters to represent  the numbers 1 - 999,999

    • Edited by Blackwood Saturday, May 23, 2015 12:19 AM Correct code
    • Marked as answer by Fred BaoModerator Monday, June 1, 2015 9:32 AM
    Saturday, May 23, 2015 12:10 AM
  • If you need to be able to convert to the alphanumeric string and back to an integer. you can use the following. Note that the code enforces the 1 - 999,999 range you mentioned and that the length of the string is set in a constant (codeLen) that can be any number that is not less than 4.

    private string alphanums = "0123456789abcdefghijklmnopqrstuvwxyz";
    private const int codeLen = 6; //Length of coded string. Must be at least 4
    
    public string EncodeNumber(int num)
    {
    	if (num < 1 || num > 999999) //or throw an exception
    		return "";
    	int[] nums = new int[codeLen];
    	int pos = 0;
    
    	while (!(num == 0))
    	{
    		nums[pos] = num % alphanums.Length;
    		num /= alphanums.Length;
    		pos += 1;
    	}
    
    	string result = "";
    	foreach (int numIndex in nums)
    		result = alphanums[numIndex].ToString() + result;
    
    	return result;
    }
    
    public int DecodeNumber(string str)
    {
    	//Check for invalid string
    	if (str.Length != codeLen) //Or throw an exception
    		return -1;
    	long num = 0;
    
    	foreach (char ch in str)
    	{
    		num *= alphanums.Length;
    		num += alphanums.IndexOf(ch);
    	}
    
    	//Check for invalid number
    	if (num < 1 || num > 999999) //or throw exception
    		return -1;
    	return System.Convert.ToInt32(num);
    }

    Saturday, May 23, 2015 12:50 AM
  • I was wondering why you asked about 6 alphanumeric characters for a number that only has to be between 1 and 999,999. Perhaps you meant hexadecimal characters (1-9 and a-f) rather than alphanumeric. If that is the case, you could use the follwing two function to convert the number to a hex string and back.

    public string ToHex(int num)
    {
    	//Check for invalid number
    	if (num < 1 || num > 999999) //or throw an exception
    		return "";
    	return num.ToString("X").PadLeft(6, '0');
    }
    
    public int ToNum(string str)
    {
    	//Check for invalid string
    	if (str.Length != 6) //Or throw an exception
    		return -1;
    	return int.Parse(str, System.Globalization.NumberStyles.HexNumber);
    }

    Saturday, May 23, 2015 1:20 AM
  • Btw, I think there is some misdirection.

    He is asking for a method to encrypting 6-digit number to 6-char alphanumeric string. By using the word "encrypt" I think he doesn't want plain conversion... say it's no good to encrypt 1 as 1. :P

    The "alphabet" range is added for you to get some space for enthalpy, I think.

    If you're willing to include chars in 0x3A to 0x40 into consideration, I think tricks like "XOR pre-shared key with range bouncing" will do. (Convert the number to string, then XOR it with some preshared key, AND it with 128 to disable the 8th bit, and adjust the 7th bit to make the outcome falls within range)

    Note that I just make it up with my mind and not mind tested all the edge case to see if it really works or not. I'm just giving out example.


    Wednesday, May 27, 2015 1:44 AM
    Answerer
  • You might be right, let's see what the OP has to say. Note that the initial post asks about "encrypting" any number (not just a 6 digit number) in 6 alphanumeric characters.
    Wednesday, May 27, 2015 10:38 AM
  • Alternatively you could create an internal lookup table, nothing more than a flat table with column 1 being a number and column 2 being a string. For every number that comes in (1-999,999) you could generate a random 6 digit alphanumeric string and write the set (number and generated string) to the data store. You could use a database as the store but if you can't or do not want to use a database you could use a flat file like CSV, JSON, Excel, or anything really as this is very simple data to model.

    Essentially this is a form of symmetric encryption but there is no key, just a lookup table. It is not possible to reverse engineer so code without access to the lookup table (or interface around the lookup table) could never get the number from the character set or a character set from a number.

    Constraints (checks) you should build in to your code:

    • Verify the incoming number does not already exist. Otherwise throw an Exception OR return the existing string that was already created. What you do depends on your requirements.
    • Verify the generated random string does not already exist before you persist/return it. If it does generate a new one.

    Mark as answer or vote as helpful if you find it useful | Igor

    Wednesday, May 27, 2015 11:06 AM
  • Hello Jassim,

    Any Update? I will mark some replies as answers since i think they are helpful for this case. I you feel they are not helpful, please unmark it.

    Thanks for your understanding and supporting.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, June 1, 2015 9:31 AM
    Moderator