Encrypting Passwords

In this tutorial i will show you how you can easily encrypt and decrypt passwords using coldfusion

The first think we need to do is create in encryption password, this is what will be used to encrypt and unencrypt your password. If you loose this then you will not be able to unencrypt any of your encrypted data.

To create this key i will use an application variable so it can be called when needed

<cfparam name="Request.PasswordKey" default="H9OUhtsjsyIUHK23jhfkuHYT">

Ok, now i will show you how to encrypt your data, this is best done when saving the data to a database. I have a form to add new users so i post this form and just before we insert the data to the database we will encrypt the password with the encryption key

I have assumed that you all know how to make a basic form if you are at the level of encrypting passwords so i shall skip that part.

<cfset Encrypted = Encrypt(Form.Npassword, Request.PasswordKey)>
<cfquery datasource="encryption">
INSERT INTO users (username, password)
VALUES (<cfqueryparam value="#FORM.Nusername#" cfsqltype="cf_sql_clob" maxlength="255">, "#Encrypted#")
</cfquery>
<cflocation url="/Admin/users.cfm" addtoken="yes">

Ok so now we should have a database with a username and encrypted password, but for the users to be able to login we will need to decrypt this password.

This is basicly the same process as before, but we unencrypt the value and use the #Encrypted# variable insted of the #FORM.Password#

<cfset Encrypted = encrypt(Form.password, Request.PasswordKey)>
<cfquery name="Login" datasource="encryption">
SELECT *
FROM users
WHERE username = '#FORM.username#'
AND password = '#Encrypted#'
</cfquery>

Hope this code is of help to people

Related Blog Entries

Comments
Dan Vega's Gravatar Good Stuff. Just remember to use queryparam on the last query. That last query is subject to a sql injection attack.
# Posted By Dan Vega | 28/09/08 17:16
TJ Downes's Gravatar Good tip, and I recommend this method for anyone storing passwords!

Of course if you are using MS SQL 2005 or 2008 Server you can use the Hash function for one way encryption and then there's no need to store the key in your code on the web server.
# Posted By TJ Downes | 28/09/08 17:17
Jonny Shaw's Gravatar yeh, sorry forgot to use the queryparam on the last query. But as i said with regards to creating a basic form. I have assumed you have a certain level of knowledge to want to encrypt your passwords.
# Posted By Jonny Shaw | 28/09/08 17:22
duncan's Gravatar I'm just wondering why you'd want to encrypt the password rather than hash it, which I thought was always best practice?
# Posted By duncan | 28/09/08 18:51
Jonny Shaw's Gravatar Well i supose you could do it either way, it doesnt matter.
just means if anyone does manage to hack your database it makes it harder for them to get data.
# Posted By Jonny Shaw | 28/09/08 20:12
Gary Fenton's Gravatar I can't think of a single good reason why anyone would want to store users' passwords in their database. Storing a hash is much more secure and offers reassurance to users. People tend to reuse the same password so an evil webmaster could access users' passwords and try to log in to their gmail, hotmail, ebay, paypal, etc, accounts. Of course users should use a different password for each website for their own sake.
# Posted By Gary Fenton | 28/09/08 20:20
Jonny Shaw's Gravatar how about storing login information? that has a users password?
# Posted By Jonny Shaw | 28/09/08 20:23
Jason Dean's Gravatar I think the point that Gary is trying to make, and it is a good one, is why does the Application need a reversible encryption of the password stored in the database? What possible need do you have for storing the password in a reversible fashion?

It actually does matter whether you store a password encrypted vs hashed. For the primary reason that encryption IS reversible. A hash is not. All a hacker needs to get at every single password in your database is that one encryption key(of course after compromising the DB).

Even for login information, there is no reason to store the password encrypted, if you store it hashed, then when the user tries to log in again, you hash their input, and compare it to the hash in the database. If they match, then the entered the correct password. There is never a reason to decrypt the users password.

Also, you should be salting your users passwords before hashing to defeat weak password, brute force attacks and Birthday/Rainbow table attacks.
# Posted By Jason Dean | 28/09/08 21:12
James Marshall's Gravatar Although the 2 way encryption you describe is better than none at all I've got to say for the sake of any newbies reading this that I'd always opt for hash encryption and comparison in a production environment.

I recently had to recode an application that a junior developer had written because there were security holes everywhere of which he was unaware. He'd used 2 way encryption because he thought it would be useful to email a user their password if they forgot it. What he didn't consider was the insecure nature of email itself. IMHO it's far better to use hash encryption and send a user a temporary password if and when necessary.
# Posted By James Marshall | 30/09/08 17:06
Justice's Gravatar I agree with Jason, and in one system I developed each user gets a unique salt assigned to them upon registration, and their password is salted then hashed and stored in the database. I have never seen a real valid reason that a system admin needs to be able to see a users password rather than re-setting it to a temporary value.
# Posted By Justice | 09/10/08 15:30

Archives By Subject

Advertising (3) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (17) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Coldfusion to PHP?
psikoteknik said: Very nice blog.Thanks for the valuable contributions to this site. Have good works. Thanks for your... [More]

Coldfusion Hosting From £2.50
Jonny Shaw said: Yes sandboxing is enabled, and no tags are disabled. I am aware of the issure with IE7, stupid IE an... [More]

Coldfusion Hosting From £2.50
Gary F said: Sounds good. Do you use sandboxing and are any tags disabled? Your jassoft.co.uk home page doesn't r... [More]

Coldfusion to PHP?
Rob Wilkerson said: You won't have any problems as long as you keep your mind open. I made the same move about 2 years a... [More]

Coldfusion to PHP?
Jonny Shaw said: The framework i will be using is Zend, Has anyone used this? [More]

Recent Entries

Coldfusion Hosting From £2.50
cf8.co.uk For Sale
Coldfusion Blog | Coldfusion Hosting | About Me