Several commonly used PHP cryptographic functions

Source: Internet
Author: User
Tags crypt decrypt sha1 urlencode alphanumeric characters asymmetric encryption

1, Preface PHP encryption method is divided into single-hash encryption, symmetric encryption, asymmetric encryption of these categories.  Like commonly used MD5, hash, crypt, SHA1 This is the single hash encryption, single hash encryption is irreversible.  Like URL encoding, base64 encoding This is symmetric encryption, is reversible, that is, encryption and decryption are used with the same secret key. In addition to asymmetric encryption, encryption and decryption of the secret key is not the same, if from the security, encrypted information if you want to decrypt it back again, asymmetric encryption is undoubtedly the safest way.  2, irreversible cryptographic function (i), md5string md5 (String str[,boolraw_output = false]) 1.MD5 () returns the hash value as a 32-character hexadecimal number by default, which accepts two parameters, The first is the string to encrypt, the second is a Boolean value of Raw_output, the default is False, and if set to TRUE,MD5 () returns the original 16-bit binary format of the digest to 2.MD5 () for one-way encryption, no reverse decryption algorithm, However, it is possible to solve some common strings by collecting, enumerating, colliding, and so on.   (ii), Cryptstring crypt (String str[,stringsalt]) 1.crypt () accepts two parameters, The first is the string that needs to be encrypted, the second is the salt value (that is, the encryption interference value, if not provided, the default is automatically generated by PHP "Salt can only take two bits"); returns the hashed string or a string less than 13 characters, in order to distinguish the salt value. 2.crypt () is one-way encryption, just like MD5.   (c), sha1string SHA1 (String str[,boolraw_output = False]1. Unlike MD5, unlike SHA1 () returns a hash value of 40 characters by default, as in the case of an incoming parameter. The first is an encrypted string, the second is a Boolean value of Raw_output, the default is False, and if set to TRUE,SHA1 () returns the original 20-bit original format of the Digest to 2.SHA1 () is also one-way encrypted, no reverse decryption algorithm   (iv), HashString Hash ($ago, $data); 1, $ago can specify the hashing algorithm used by encryption, for example: "MD5", "sha256", "haval160,4" and so on. The $data is to encrypt the data hash encryption is also irreversible, because it is given an indeterminate string to return a specific length of the string, in the essence of the implementation of a singleItem hash encryption.  3, Reversible J cryptographic function (i), urlencodestring UrlEncode (String $str) 1. A parameter that passes in the string to be encrypted (usually applied to the encryption of the URL), 2.urlencode is two-way encryption, Can be encrypted with urldecode (strictly speaking, not true encryption) 3. Returns the string, in addition to-_. All non-alphanumeric characters are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+).   (b), Base64 encoded string Base64_decode (String $encoded _data) 1.base64_encode () accepts a parameter, that is, the data to be encoded (this does not say a string, Because many times base64 is used to encode pictures) 2.base64_encode () is two-way encryption, and Base64_decode () is used to decrypt Base64 encryption, which essentially translates data into ASCLL code. For example, a picture for Base64 encoding will become a bunch of ASCLL code connection strings, which will be more conducive to file transmission, of course, the role of Base64 in the transfer of files. For example, the mobile phone client uploads files to the server, using Base64 encoding can easily achieve the transfer of files.

Several commonly used PHP cryptographic functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.