How to use crypt in PHP

In this article I will explain how the crypt() function can be used in PHP.
  • 2734

crypt() function in PHP

  • The crypt() function is used to return a string encrypted using the standard DES, Blowfish, or MD5 algorithms.
  • The crypt() function returns the salt as the first two characters of the output.
  • The crypt() function will return a hashed string using alternative algorithms that may be available on the system.
  • The crypt() function behave different on different operating systems, some operating systems supports more than one type of encryption.
  • In crypt() function, the exact algorithm depends on the format and length of the salt parameter.
  • In crypt() function, salts help make the encryption more secure by increasing the number of encrypted strings.

There are some constants that are used together with the crypt() function.

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

  • str str is required parameter. Determine a string to be encoded.
  • salt salt is optional parameter. A string used to increase the number of characters encoded, to make the encoding more secure. one will be randomly generated by PHP each time you call this function, If the salt argument is not provided.

Example

The following example show to how the crypt() function can be used in PHP.

<html>

<body>

<h3 style="color: blueviolet;">crypt() function example in PHP</h3>

   <?php

    if (CRYPT_STD_DES == 1)

    {

    echo "Standard DES: ".crypt("C-sharpcorner")."\n<br />";

    }

    else

    {

    echo "Standard DES not supported.\n<br />";

    }

    if (CRYPT_EXT_DES == 1)

    {

    echo "Extended DES: ".crypt("C-sharpcorner")."\n<br />";

    }

    else

    {

    echo "Extended DES not supported.\n<br />";

    }

    if (CRYPT_MD5 == 1)

    {

    echo "MD5: ".crypt("C-sharpcorner")."\n<br />";

    }

    else

    {

    echo "MD5 not supported.\n<br />";

    }

    if (CRYPT_BLOWFISH == 1)

    {

    echo "Blowfish: ".crypt("C-sharpcorner");

    }

    else

    {

    echo "Blowfish DES not supported.";

    }

    ?>

</body>

</html>

 

Output

crypt-php.jpg
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.