Multimobile Development: Building Applications for any Smartphone
Encrypting Passwords and .NET
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.


Harris Boyce III
Hello -

I'm looking for a way to store passwords in a SQL Server database using
encrytion. My thoughts are that when a user creates a new account the
password is sent and encrypted using SHA1 or something of that sort and then
stored in the database. When the user then sends the password to login,
it's encrypted using the same method and then compared. I don't know if
this is possible or not and I'd extremely appreciate examples, pointers,
etc. if at all possible. I am also wondering what the best data type would
be to store the encrypted data.

Thank you for your time.

- Harris Boyce

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
Jason S.
Yes this is possible... look in the System.Web.Security.FormsAuthentication
namespace.

"Harris Boyce III" <Click here to reveal e-mail address> wrote in message
news:#bqayu7TBHA.1860@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Michael Giagnocavo
Yes, this is very possible. I did it using MD5. I manage everything as
Base64, as I had bugs when passing the data around to different sites.

Here are my MD5 functions:

Public Function ComputeMD5Hash(ByVal s As String, Optional ByRef MD5 As
MD5CryptoServiceProvider = Nothing) As String
If MD5 Is Nothing Then
Return ToBase64String(New
MD5CryptoServiceProvider().ComputeHash(ASCII.GetBytes(s)))
Else
Return ToBase64String(MD5.ComputeHash(ASCII.GetBytes(s)))
End If
End Function

Public Function CompareStringToHash(ByVal sString As String, ByVal sHash As
String, Optional ByRef MD5 As MD5CryptoServiceProvider = Nothing) As Boolean
If MD5 Is Nothing Then
Return Equals(ToBase64String(New
MD5CryptoServiceProvider().ComputeHash(ASCII.GetBytes(sString))), sHash)
Else
Return Equals(ToBase64String(MD5.ComputeHash(ASCII.GetBytes(sString))),
sHash)
End If
End Function

I store the data in a char(24).
-mike

"Harris Boyce III" <Click here to reveal e-mail address> wrote in message
news:#bqayu7TBHA.1860@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Harris Boyce III
Thanks for your help.

I wasn't sure if I was on the right track or not and everything seems to be
working fine. I basically synthesized what you gave me and what Mr. Pratt
provided in C#. Thanks again.

- Harris Boyce

"Michael Giagnocavo" <Click here to reveal e-mail address> wrote in message
news:#YRJY1DUBHA.1524@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
    
Phillip Pratt
What you are talking about is hashing, not encrypting. A hash algorithm is a
one-way function (There's no way to get the original back from the hash), an
encryption algorithm is a two-way function. Other than that distinction,
though what you are doing is the way to go. A hash is the appropriate method
for storing passwords because there is never a reason to get back the
original. Also with a hash function, the output size is predetermined and
independent of the input size (20 Bytes in the case of SHA1). This makes
choosing a datatype in a database pretty easy. And as far as hashing
algorithms go, SHA1 is a good one.

Here's an example using a text box to get the input value:

UnicodeEncoding enc = new UnicodeEncoding();

byte[] plainbytes = enc.GetBytes(textBox1.Text);

SHA1 sha = new SHA1CryptoServiceProvider();

byte[] result = sha.ComputeHash(plainbytes);

You may want to then encode the result byte array to some sort of text
encoding (Base64 perhaps?) or just leave it as a byte array for storage.

Hope this helps.

"Harris Boyce III" <Click here to reveal e-mail address> wrote in message
news:#bqayu7TBHA.1860@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
New book project – Multimobile Development: Building Applications for any Smartphone
Dive into HTML5
 
    
Pablo Castro
Take a look at the CryptoStream class. It'll allow you to both encrypt and
decrypt data using I/O streams (if you don't want files, just use
MemoryStream as you undelying stream).

About the SQL data type...you can simple encode de encrypted password in
some textual format (i.e. Base64, CryptoStream can do that for you) and
store it in a VARCHAR field.

--

Pablo Castro [MVP]
Lagash Systems S.A.

"Harris Boyce III" <Click here to reveal e-mail address> wrote in message
news:#bqayu7TBHA.1860@tkmsftngp05...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.IO.MemoryStream
System.Security.Cryptography.CryptoStream
System.Security.Cryptography.MD5
System.Security.Cryptography.MD5CryptoServiceProvider
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1CryptoServiceProvider
System.Text.UnicodeEncoding
System.Web.Security.FormsAuthentication




Multimobile Development: Building Applications for any Smartphone
Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734