Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello frnd I have a string variable. and I dont know it's size on the time of declaration. it's length may be grater than 25 or less than 25.
i want to convert this string is only 25 charater.
amd if string value is same the it always generate same charater.
please any one give me some ideas.
Thanks in adv.
Posted
Comments
phil.o 13-Nov-13 5:14am    
What have you tried? Please show some code.
Getting the length of any string is a trivial task.
♥…ЯҠ…♥ 13-Nov-13 5:15am    
Please you give me some idea to understand your requirement ;-)
Akinmade Bond 13-Nov-13 5:25am    
Do you want to sort the content of the string cause it looks like you want to limit the length of the string to 25 characters. Which is it?
Shambhoo kumar 13-Nov-13 5:36am    
No Actually now i am working on license generator software , and i want to generate unique key of each school on the behaif of school name and address. and my requirement is "if school name and address length may be less than 25 or gretor than 25, but i want generate 25 character unique value in both condition ".
Shambhoo kumar 19-Nov-13 23:32pm    
http://download-ln.jetbrains.com/dotpeek/dotPeekSetup-1.1.1.33.msi


Reflector Link

http://download.red-gate.com/ReflectorInstaller.exe

You should look into generating Hashes based on School Name + School Address. The issue of which 25 characters to select should be easy to handle.

How "secure" you want/need the Hashes to be will dictate the specific techniques you use, like using "salt" (appending, or pre-pending, a random string to each key-source before hashing).

There's a good Microsoft step-by-step C# tutorial on generating Hashes from strings here: [^]. This tutorial includes a section on how you compare two Hashes (which could be simplified using Linq).
 
Share this answer
 
Hi Shamboo,

I think you want to get 25 characters from the string, string may have 25 more or less characters.. If I am right, you can try this

So here is what I have tried, which might be useful and keep it as refernce

C#
string encodeStr = @"!#$%&'()*+,-./0123456789:;<=>?             @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

string decodeStr = @"@ABCDEFGHIJKLMNO0123456789:;<=>?           pqrstuvwxyz{|}~!#$%&'()*+,-./`abcdefghijklmnoPQRSTUVWXYZ[\]^_";

protected void Page_Load(object sender, EventArgs e)
        {
            string schoolName = "RMHSSchool";
            string schoolAddress = "MaduraiSchool";
            string regId = "101";
            string versionNum = "1.0";
            string softwareType = "ASP";
            string finalCipherText = string.Empty;
            string finalRawText = string.Empty;

            //25 Characters generation using custom encryption method
            finalCipherText = encodeString(schoolName.Substring(0, 8)) + encodeString(schoolAddress.Substring(0, 8))+ encodeString(regId.Substring(0, 3)) + encodeString(versionNum.Substring(0, 3)) + encodeString(softwareType.Substring(0, 3));

            //25 Characters generation using custom decryption method
            finalRawText = decodeString(schoolName.Substring(0, 8)) + decodeString(schoolAddress.Substring(0, 8)) + decodeString(regId.Substring(0, 3)) + decodeString(versionNum.Substring(0, 3)) + decodeString(softwareType.Substring(0, 3));

            //Getting the values from actual input and binding as single string
            string compareFinal = schoolName.Substring(0, 8) + schoolAddress.Substring(0, 8) + regId.Substring(0, 3) +
                                  versionNum.Substring(0, 3) + softwareType.Substring(0, 3);


            if (decodeString(finalCipherText) == compareFinal) //Just for re-checking the input and cipher text
            {
                //Decryption successfull
            }
            else
            {
                //Decryption failed
            }
        }

        //Custom Encryption method
        protected string encodeString(string rawText)
        {
            string cipherText = string.Empty;
            foreach (char rawChar in rawText)
            {
                cipherText += encodeStr.ElementAt(decodeStr.IndexOf(rawChar));
            }
            return cipherText;
        }

        //Custom Decryption method
        protected string decodeString(string cipherText)
        {
            string rawText = string.Empty;
            foreach (char cipherChar in cipherText)
            {
                rawText += decodeStr.ElementAt(encodeStr.IndexOf(cipherChar));
            }
            return rawText;
        }


You need to take care of validating the length of every string and other sort of validations.
Finally you will get 25 characters unique ID as per your requirement.
The above code is only for reference dont rely on it, customize the way you want and make it more secure.

Hope this helps you better

Regards,
RK
 
Share this answer
 
v7
Comments
lukeer 13-Nov-13 5:35am    
You don't need a SubString() call in the else branch. Just assign it.
♥…ЯҠ…♥ 13-Nov-13 5:37am    
Yes you are right... hard thinking...;-)
Shambhoo kumar 13-Nov-13 5:36am    
No Actually now i am working on license generator software , and i want to generate unique key of each school on the behaif of school name and address. and my requirement is "if school name and address length may be less than 25 or gretor than 25, but i want generate 25 character unique value in both condition ".
♥…ЯҠ…♥ 13-Nov-13 5:53am    
and you want to decrypt right or you just want to store it in DB?
Shambhoo kumar 13-Nov-13 5:57am    
yes you are right.
if you just want to check the length of the string write
if(mystringh.Trim().Length>=25)
{
    //Do Work
}


that will give you the length of string.
 
Share this answer
 
Comments
Shambhoo kumar 13-Nov-13 5:36am    
No Actually now i am working on license generator software , and i want to generate unique key of each school on the behaif of school name and address. and my requirement is "if school name and address length may be less than 25 or gretor than 25, but i want generate 25 character unique value in both condition ".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900