Home  >  Article  >  Backend Development  >  How to encrypt php

How to encrypt php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-30 10:12:523132browse

How to encrypt php

MD5 encryption:

string md5 ( string $str [, bool $raw_output = false ] )

1.md5() returns the hash value as a 32-character hexadecimal number by default, It accepts two parameters, the first is the string to be encrypted, and the second is the Boolean value of raw_output. The default is false. If set to true, md5() will return the original 16-bit binary format message digest.

2.md5() is a one-way encryption and does not have a reverse decryption algorithm, but it can still crack some common strings through collection, enumeration, collision and other methods.

Related recommendations: "PHP Tutorial"

  $username='jellybool';
  $password='abcd';
  /*简单地对字符串进行md5加密*/
  echo md5($username);
  echo "
";
  echo md5($password);
  echo "
";
  /*更推荐的做法是对重要的敏感数据进行多次加密,以防被轻易破解*/
  echo md5(md5($password));
  /*以上输出:
  username:4f5436e5d72608fb647b691e8edcf42e
  password:7bf02cf0f4af6da4accbc73d2a175476
  password(两次加密):864704bb35754f8cd0232cba6b91521b
  string crypt ( string $str [, string $salt ] )

1.crypt() accepts two parameters, the first is the string that needs to be encrypted, and the second The second is the salt value (which is the encryption interference value, if not provided, it is automatically generated by PHP by default); returns a hashed string or a string of less than 13 characters, the latter is to distinguish the salt value.

2.crypt() is one-way encryption, the same as md5.

  $password='jellybool.com';
  echo crypt($password);
  echo "
";
  echo crypt($password,"jellybool");
  //输出:je7fNiu1KNaEs
  /*当我们要加自定义的盐值时,如例子中的jellybool作为第二个参数直接加入,

Sha1 encryption:

string sha1 ( string $str [, bool $raw_output = false ]

1. It is very similar to md5, except that sha1() returns a 40-character hash value by default , the properties of the incoming parameters are the same, the first one is the encrypted string, the second one is the Boolean value of raw_output, the default is false, if set to true, sha1() will return the original 20-bit original format message digest.

2.sha1() is also one-way encryption and has no reverse decryption algorithm.

  $my_intro="abc";
  echo sha1($my_intro);
  //输出:c98885c04c1208fd4d0b1dadd3bd2a9ff4d042ca
  echo "
";
  //当然,可以将多种加密算法混合使用
  echo md5(sha1($my_intro));
  //输出:94f25bf9214f88b1ef065a3f9b5d9874

Urlencode encryption:

string urlencode ( string $str )

1. One parameter, pass in the string to be encrypted (usually used to encrypt URLs).

2. urlencode is two-way encryption and can be encrypted with urldecode (strictly speaking, it is not a real encryption).

3. Return a string. All non-alphanumeric characters in this string except -_. will be replaced with a percent sign (%) followed by two hexadecimal digits. , spaces are encoded as plus signs ( ).

  $a = "http abc你好";
  $b = urlencode($a);
  $c = urldecode($b);
  echo $b;
  echo '
';
  echo $c;
  ?>

The above is the detailed content of How to encrypt php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn