PHP based on Mcrypt_encrypt and mcrypt_decrypt implementation of string encryption and decryption method _php skills

Source: Internet
Author: User
Tags md5

This article describes the PHP based on Mcrypt_encrypt and mcrypt_decrypt implementation of string encryption and decryption method. Share to everyone for your reference, specific as follows:

Because of the security considerations, the parameter transmission needs to be encrypted and decrypted, a relatively simple method is directly using PHP functions Mcrypt_encrypt, Mcrypt_decrypt, a cryptographic, a decryption, but the problem has arisen, This encryption process will create some symbols that confuse the URL, and then after the encryption of the encryption characters again processing, and then more than one by one resolution:

$key = "Miyao";//Key
$string = "Jiami"//required encrypted character//
with the encrypted function
$crypttext = Base64_encode (Mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, MD5 ($key), $string, MCRYPT_MODE_CBC, MD5 (MD5 ($key)));
$encrypted =trim ($this->safe_b64encode ($crypttext))//To handle special characters
$key = "Miyao"
$CRYPTTEXTTB =safe_ B64decode ($encrypted)//to the special character parsing
$decryptedtb = RTrim (Mcrypt_decrypt (mcrypt_rijndael_256, MD5 ($key), base64_ Decode ($CRYPTTEXTTB), MCRYPT_MODE_CBC, MD5 (MD5 ($KEY)))//decryption function
//handling special characters public function
Safe_b64encode ($string) {
    $data = Base64_encode ($string);
    $data = str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), $data);
    return $data;
}
Resolves special characters public
function Safe_b64decode ($string) {
    $data = str_replace (Array ('-', ' _ '), Array (' + ', '/'), $ string);
    $mod 4 = strlen ($data)% 4;
    if ($mod 4) {
      $data. = substr (' = = = ', $mod 4);
    }
    Return Base64_decode ($data);


PS: About encryption and decryption interested friends can also refer to the site online tools:

Password Security online detection:
Http://tools.jb51.net/password/my_password_safe

High Strength Password Generator:
Http://tools.jb51.net/password/CreateStrongPassword

MD5 Online Encryption Tool:
Http://tools.jb51.net/password/CreateMD5Password

Thunderbolt, Express, Cyclone URL encryption/decryption tool:
Http://tools.jb51.net/password/urlrethunder

Online hashing/hashing algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

More about PHP related content to view the site topics: "PHP Encryption Method Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP operation Office Document Skills Summary (including Word,excel, Access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.