<?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; login</title>
	<atom:link href="http://www.earn-web-cash.com/tag/login/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>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>

