Convert Character to ASCII value

cyclonebri

Regular
Joined
Jul 30, 2003
Messages
93
Location
Ames, IA, USA
Hey Everybody,

Hopefully this is an easy one but I can't seem to find the answer through search, so I apologize in advance if this has already been posted.

A simple problem. I have a string that I want to iterate by character, converting each character to it's ascii representation.

I was wondering how I can accomplish this without making a table with the values and just finding them...my goal is to mask a plain text password stored in an XML file for later retrieval (since we don't use the registry anymore). Basically the user logs in and I want the ability to remember their settings like a cookie would do on a web page, but this is a desktop app.

I have all the code I need, except the ability to convert an unknown character (variable tmpChar) to it's ascii representation.

I do this:

for (int i = 0; i<mPassword.Length; i++)
{
tmpStr = mPassword.Substring(i,1);
tmpChar = Convert.ToChar(tmpStr);

//convert to ascii
//tmpInt = SomeConvertFunction(tmpChar);
//if I have to I could write a function but I'd rather not do that
}

Thanks in advance for any help!
Brian
 
No offence but that would be a pretty easy to crack scheme.

A much better way is to not store the password in any way shape or form but store a hash of the password. When the user enters their password - hash it and compare to the stored hash.

Best place to look is under System.Security.Cryptography at their implementation of SHA<whichever version> or similar.
 
no offense taken, I understand how easy this would be to crack :)
I didn't say that was the only method of security, just happens to be a portion of it ;), in any event it will still be easy to crack :).

I'm just looking for fast solutions. I will look under the System.Security.Cryptography and see what it has to offer. Thanks for the heads up :)

Brian
 
Well, just in case anyone ever cares about doing something like this in the future, I have found a site online that has helped and have used the code to produce a solution. The code can be found HERE

and when implemented could look something like this:

C#:
byte[] Bytstr = new byte[mPassword.Length];
System.Text.Encoding.ASCII.GetBytes(mPassword.ToCharArray(),0,mPassword.Length,Bytstr,0);
byte myByte;
string[] tmpBytStr = new string[mPassword.Length];

for(int i = 0; i<mPassword.Length;i++)
{
    myByte = Bytstr[i];
    tmpBytStr[i] = myByte.ToString();
}

hope this will help someone out in the future :)

Take care,
Brian
 
Back
Top