W3Schools.com

PHP crypt() Function


PHP String Reference Complete PHP String Reference

Definition and Usage

The crypt() function returns a string encrypted using DES, Blowfish, or MD5 algorithms.

This function behaves different on different operating systems, some operating systems supports more than one type of encryption. PHP checks what algorithms are available and what algorithms to use when it is installed.

The exact algorithm depends on the format and length of the salt parameter. Salts help make the encryption more secure by increasing the number of encrypted strings that can be generated for one specific string with one specific encryption method.

There are some constants that are used together with the crypt() function. The value of these constants are set by PHP when it is installed.

Constants:

  • [CRYPT_SALT_LENGTH] - The length of the default encryption. With standard DES encryption, the length is 2
  • [CRYPT_STD_DES] - Value is 1 if the standard DES algorithm is supported, 0 otherwise. The Standard DES-based encryption has a two character salt
  • [CRYPT_EXT_DES] - Value is1 if the extended DES algorithm is supported, 0 otherwise. The Extended DES encryption has a nine character salt
  • [CRYPT_MD5] - Value is 1 if the MD5 algorithm is supported, 0 otherwise. The MD5 encryption has a 12 character salt starting with $1$
  • [CRYPT_BLOWFISH] - Value is 1 if the Blowfish algorithm is supported, 0 otherwise. The Blowfish encryption has a 16 character salt starting with $2$ or $2a$

Syntax

crypt(str,salt)

Parameter Description
str Required. Specifies the string to be encoded
salt Optional. A string used to increase the number of characters encoded, to make the encoding more secure. If the salt argument is not provided, one will be randomly generated by PHP each time you call this function.


Tips and Notes

Note: There is no decrypt function. The crypt() function uses a one-way algorithm.


Example 1

In this example we will test the different algorithms:

<?php
if (CRYPT_STD_DES == 1)
{
echo "Standard DES: ".crypt("hello world")."\n<br />";
}
else
{
echo "Standard DES not supported.\n<br />";
}

if (CRYPT_EXT_DES == 1)
{
echo "Extended DES: ".crypt("hello world")."\n<br />";
}
else
{
echo "Extended DES not supported.\n<br />";
}

if (CRYPT_MD5 == 1)
{
echo "MD5: ".crypt("hello world")."\n<br />";
}
else
{
echo "MD5 not supported.\n<br />";
}

if (CRYPT_BLOWFISH == 1)
{
echo "Blowfish: ".crypt("hello world");
}
else
{
echo "Blowfish DES not supported.";
}
?>

The output of the code above could be (depending on the operating system):

Standard DES: $1$r35.Y52.$iyiFuvM.zFGsscpU0aZ4e.
Extended DES not supported.
MD5: $1$BN1.0I2.$8oBI/4mufxK6Tq89M12mk/
Blowfish DES not supported.


PHP String Reference Complete PHP String Reference
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$7.95/mo SEO Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Free Website Templates Free CSS Templates
Make Your Own Website
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE