<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Cash &#187; password</title>
	<atom:link href="http://www.earn-web-cash.com/tag/password/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Understanding the Difference Between MD5 and Crypt Functions</title>
		<link>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 15:38:34 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/</guid>
		<description><![CDATA[If you're working with a user-management system in PHP (or any scripting language), than you're likely using either the md5() or crypt() function to encode and store the user's password.

An oft-asked question - and oft-misunderstood - is what is the difference?  Is one better than the other?]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working with a user-management system in PHP (or any scripting language), than you&#8217;re likely using either the md5() or crypt() function to encode and store the user&#8217;s password.</p>
<p>An oft-asked question &#8211; and oft-misunderstood &#8211; is what is the difference?  Is one better than the other?<br />
<span id="more-95"></span><br />
First, let&#8217;s take a quick look at how each one works.</p>
<h5>How MD5 Works</h5>
<p>MD5 creates a &#8220;hash&#8221; value based on an input string.  It uses a one-way algorithm to turn the password into an unintelligible garble of words.  Here&#8217;s a sample usage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
1ee31b77d0697c36914b99d1428f7f32</pre>
<p>This long string (32 characters) is the md5 hash value of &#8220;Bananas.&#8221;  You can now store it in the database, and when a user wants to log in you compare <code>md5($passwordInput)</code> to this hash value.</p>
<h5>Using Crypt to Encrypt a Password</h5>
<p>Crypt has very similar functionality, but with a unique twist.</p>
<p>Crypt still encrypts a phrase in a one-way algorithm to create a garbled bunch of characters.  Here&#8217;s an example&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
$1$upPJosTV$HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>What&#8217;s unique about the crypt() function is that it uses an encryption key (or &#8220;salt&#8221;) to vary the encryption process.  This means that you can encrypt the same password (&#8220;Bananas&#8221;) multiple times and get different hash values to store in your database.</p>
<h5>Do I Have to Store the Encryption Key or Salt?</h5>
<p>This is where the crypt() function gets a bit confusing.  No, you don&#8217;t&#8230;  because the encryption key is stored in the password itself.</p>
<p>Take another look at this output.</p>
<pre><strong>$1$upPJosTV$</strong>HC4n2bUsQFZk2IDN1CLdg.</pre>
<p>The bold bit of text ($1$upPJosTV$) is the stored salt or encryption key (read <a href="http://www.earn-web-cash.com/2008/01/22/crypt-function-php/" title="How to Use the Crypt() Function to Encrypt and Check Passwords in PHP">how to use crypt()</a> for more information on how this salt is created).</p>
<p>So, no.  You don&#8217;t have to store the salt anywhere, because it&#8217;s stored in the hash&#8217;ed text.  You can simply use that as the salt in the future to check if the password is correct.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$passwordInput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$hashedPassword</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//  Ok, log in and stuff.  }</span></pre></div></div>

<h5>Wait&#8230; My Encryption Key Is Stored <strong>IN</strong> the Password?!?</h5>
<p>This is where most people think that crypt() is a waste of time.  If the great thing about crypt() is that it uses a customizable encryption key, then isn&#8217;t it self-defeating to include the encryption key in the stored password?</p>
<p>Not entirely.</p>
<p>The problem with md5() is that everyone knows the encryption algorithm.  Sure, you can&#8217;t go backwards&#8230; but you can easily build a dictionary of known passwords and known hashes.  By storing these in a database, it is feasible to do a simple dictionary check to see if you have a password that goes with a known hash.</p>
<h5>Why You Should Use Crypt() Instead</h5>
<p>Since each crypt() call can use a unique encryption key, there can be no stored dictionary of password hashes.</p>
<p>Here&#8217;s an example.  Try running this script to see how different salts or encryption keys can change the output of a crypt() call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ab'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ed'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pz'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bananas&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'qp'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<pre>Output:
abBGdAR.aTnBE
edHaZeqWhmLpw
pzqz3tbQxuuRI
qp/4Jsj38Cq0Y</pre>
<p>This illustrates the strength of crypt().  By using a different encryption key, the same password (&#8220;Bananas&#8221;) can be turned into many different hashes.  You can&#8217;t simply create a lookup table with known passwords and known hashes &#8211; because the hash changes based on the encryption key.</p>
<p>The bottom line is that crypt() doesn&#8217;t make your passwords unbreakable or protect them from brute force attacks.  If someone wants to take the time to check every possible permutation of characters to get to your password, they can do it.</p>
<p>But crypt() does prevent the use of a password dictionary that contains known passwords and known hashes.  This would be a far more efficient way to hack a password than a simple brute force attack.  So by using crypt() you&#8217;re getting an extra layer of security.</p>
<p>[Note:  This article assumes you are using an md5 hash as the salt for crypt().  You could use an extended DES salt or a Blowfish salt for slightly different functionality, but these are not supported on all servers - including mine.]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/02/md5-vs-crypt-password/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Create a Random Password for Users</title>
		<link>http://www.earn-web-cash.com/2008/02/01/random-password/</link>
		<comments>http://www.earn-web-cash.com/2008/02/01/random-password/#comments</comments>
		<pubDate>Fri, 01 Feb 2008 21:56:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/01/random-password/</guid>
		<description><![CDATA[If you're working on a user-management system, you may find it useful to be able to create a random password for users.

Some systems generate these initially and have the user log in to set a permanent password.  You might also have a "Reset" button, where the script generates a random password and e-mails it to the user.

This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you're impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function's source code</a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re working on a user-management system, you may find it useful to be able to create a random password for users.</p>
<p>Some systems generate these initially and have the user log in to set a permanent password.  You might also have a &#8220;Reset&#8221; button, where the script generates a random password and e-mails it to the user.</p>
<p>This quick tutorial will show you how to create an 8 character random password containing a mix of letters and numbers.  Or, if you&#8217;re impatient, jump straight to the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/randpassword.txt' title='Random Password Generator'>function&#8217;s source code</a><br />
<span id="more-90"></span><br />
There are plenty of ways to do this.  The simplest method would be to take a random number, generate an md5 hash, and then use the first 8 characters as the password.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But this doesn&#8217;t guarantee you an even mix of upper case letters, lower case letters, and numbers.  To do that, we&#8217;ll need to use a few simple php functions and build a short script.</p>
<h5>Build a Loop to Create Eight Characters</h5>
<p>We&#8217;ll start our script by creating a blank string, looping eight times, and entering a character in the string each time.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This creates a blank string ($password) and iterates a loop eight times.  At this point, the loop simply enters the letter &#8216;a&#8217; into $password &#8211; so you end output should be &#8216;aaaaaaaa.&#8217;</p>
<h5>Generate Random Characters</h5>
<p>Now we need to generate random characters to go inside of the string.  To do this, we can make use of the rand() and chr() functions.  Replace the loop contents with this line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">95</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>chr() takes an integer and returns the ASCII equivalent of that number.  In this case, we&#8217;re using rand() to get a number between 60 and 95 &#8211; so we should get an uppercase letter in return.  Our random password should now contain eight random upper-case letters.</p>
<h5>Randomly Enter Uppercase, Lowercase, and Numbers</h5>
<p>To make the random password more secure, we should randomize whether the new character is a number, an upper-case letter, or a lower-case letter.  We can execute a simple &#8220;switch&#8221; statement to randomly choose which type of character to enter.</p>
<p>Replace the loop contents with this new snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
   <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
   <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We&#8217;re using rand(1, 3) to randomly choose which case to execute.  Each case then enters a different type of character in the string.  The first case simply returns a random digit.  The second and third cases use chr() and rand() to return a random character.</p>
<p>At this point, the script should give you an eight-character password with a random mix of uppercase letters, lowercase letters, and numbers.  Now you can e-mail the password to the user, take a hash of the password, and store it in the database.</p>
<p>For reference, here&#8217;s the entire script placed in a function.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> randPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span> <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random digit, 0-9</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random upper-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">65</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//  Add a random lower-case letter</span>
      <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$password</span>  <span style="color: #339933;">.=</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">97</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">122</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/01/random-password/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>How to Use the Crypt() Function to Encrypt and Check Passwords</title>
		<link>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/</link>
		<comments>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 02:48:13 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/01/22/how-to-use-the-crypt-function-to-encrypt-and-check-passwords/</guid>
		<description><![CDATA[Every php-script that involves some kind of user login and database interaction has one very important feature &#8211; password checking and encryption. There are a bunch of ways you could create and check passwords &#8211; from an insecure string in a database to an encrypted &#8220;hash&#8221; that you check against user input. This tutorial will [...]]]></description>
			<content:encoded><![CDATA[<p>Every php-script that involves some kind of user login and database interaction has one very important feature &#8211; password checking and encryption.</p>
<p>There are a bunch of ways you could create and check passwords &#8211; from an insecure string in a database to an encrypted &#8220;hash&#8221; that you check against user input.  This tutorial will show you how to use the <a href="http://us.php.net/manual/en/function.crypt.php" title="Crypt() function on php.net"><code>crypt()</code></a> function to store and check passwords in a php script.<br />
<span id="more-57"></span></p>
<h5>What Does the Crypt() Function Do?</h5>
<p>The <code>crypt()</code> function takes two parameters &#8211; the first parameter is the actual input (the password to test) and the second parameter is a &#8220;salt&#8221; or encryption key that is used to encrypt the password phrase.</p>
<p>Let&#8217;s take a look at what the <code>crypt()</code> function does with some input.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;xt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Would yield the output&#8230;</p>
<pre><code>xt0iPj3UKFQSM</code></pre>
<p>The function used the encryption key &#8220;xt&#8221; to turn &#8220;Gobble&#8221; into an encrypted mess.  Now, a person looking through the database won&#8217;t be able to find out a person&#8217;s password.  They&#8217;ll only find the encrypted password &#8211; which won&#8217;t work if you enter it into a script.</p>
<h5>The Crypt() Function Stores the Encryption Key in the Output</h5>
<p>There&#8217;s an important pattern here, though, that we can see if we look at a couple of <code>crypt()</code> calls in a row.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;td&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;pz&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Would yield the output&#8230;</p>
<pre><code>ab30/okS7bRdo
tdylLlJ9zwOss
pz0u5z5fgyCK.</code></pre>
<p>You can break each piece of output into two pieces &#8211; the first two characters and the last 11 characters.</p>
<p>The first two characters &#8220;ab,&#8221; &#8220;td,&#8221; and &#8220;pz&#8221; are the three &#8220;salts&#8221; or encryption keys that we used in our <code>crypt()</code> calls.  The last 11 characters are the actual encrypted pass phrases.</p>
<p>This simple point is crucial to the functioning of <code>crypt()</code>.  It stores the encryption key inside the encrypted phrase, so that you can use it to encrypt a new phrase &#8211; and compare them.  If you use the encrypted phrase as your &#8220;salt&#8221; (the second parameter for <code>crypt()</code>), the function will isolate the encryption key and ignore the rest.</p>
<p>So this example would output &#8220;Passwords match!&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// User input</span>
<span style="color: #000088;">$salt</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Encryption key</span>
<span style="color: #000088;">$encrypted</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$salt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #339933;">,</span> <span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> 
    <span style="color: #000088;">$encrypted</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Passwords match!&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this case we&#8217;re using the encrypted password ($encypt) to perform the encryption algorithm on the user&#8217;s input ($password) to see if they match.  Normally, you would have $encrypted stored in the database to perform comparisons in your script.</p>
<h5>Use an MD5 Hash Salt to Encrypt Phrases Over 8 Characters</h5>
<p>The final thing to keep in mind about <code>crypt()</code> is that it can use different kinds of salts or encryption keys.  The two-character salt we&#8217;ve been using is pretty weak.  It also has a flaw in functionality &#8211; using a two-character encryption key, the <code>crypt()</code> function will ignore everything past the first 8 characters of the phrase to be encrypted.</p>
<p>So both of these statements would have the same output.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobbledeygook&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobbledeygah&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>crypt()</code> function is only encrypting the first eight characters &#8211; &#8220;Gobblede&#8221;.  The rest is ignored.</p>
<p>You can change this by using a special type of encryption key &#8211; a md5 hash.  Under normal circumstances, this is enabled in php, but you can double check by seeing if the constant <code>CRYPT_MD5</code> is set to &#8217;1&#8242;.</p>
<p>An md5 hash salt is formatted like this &#8211; <code>$1$xxxxxxxx$</code>.  &#8220;$1$&#8221;, followed by eight random characters, followed by a closing &#8220;$&#8221;.  You could create one yourself to use as a salt.</p>
<p>However, in most cases if you provide no salt or encryption key at all, php will generate a random salt for you.  So, for example, when you are entering a new password into the database you can use this statement&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gobble&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This variable ($password) can now be stored in the database.  Remember that $password holds both the encrypted phrase (Gobble) and the random encryption key.  So to check if a user entered the correct password you would fetch $password from the database and use this statement&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">crypt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userInput</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//  Ok, the passwords matched }</span></pre></div></div>

<p>Now that you know how <code>crypt()</code> works, get to it.  Start creating user-authentication scripts and work your encryption magic.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/01/22/crypt-function-php/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		</item>
	</channel>
</rss>

