PHP custom encryption method with encryption factor

Source: Internet
Author: User
Currently, there are many PHP encryption methods. generally, simple encryption methods are directly MD5, but some simple MD5 passwords can also be decompiled, therefore, we can use custom methods to encrypt data more securely. Currently, there are many PHP encryption methods. generally, simple encryption methods are directly MD5, but some simple MD5 passwords can also be decompiled, therefore, we can use custom methods to encrypt data more securely.

I have collected and summarized some PHP encryption methods for your reference.

Method 1:

/*** Encrypt the user's password * @ param $ encrypt // input the encrypted string, perform authentication * @ return array/password */function password ($ password, $ encrypt = '') {$ pwd = array (); $ pwd ['encrypt'] = $ encrypt? $ Encrypt: rand (, 9999); $ pwd ['password'] = md5 (md5 (trim ($ password). $ pwd ['encrypt']); return $ encrypt? $ Pwd ['password']: $ pwd ;}

Method description: if we only pass in the password, the method will return an encryption factor and a new encrypted password. the encryption factor and password are randomly generated, and each time they are different.


Method 2:

/** Rc4 encryption algorithm * $ pwd Key * $ data to be encrypted * // $ pwd key $ data requires the encrypted string function rc4 ($ pwd, $ data) {$ key [] = ""; $ box [] = ""; $ pwd_length = strlen ($ pwd); $ data_length = strlen ($ data ); for ($ I = 0; $ I <256; $ I ++) {$ key [$ I] = ord ($ pwd [$ I % $ pwd_length]); $ box [$ I] = $ I;} for ($ j = $ I = 0; $ I <256; $ I ++) {$ j = ($ j + $ box [$ I] + $ key [$ I]) % 256; $ tmp = $ box [$ I]; $ box [$ I] = $ box [$ j]; $ box [$ j] = $ tmp;} for ($ a = $ j = $ I = 0; $ I <$ data_length; $ I ++) {$ a = ($ a + 1) % 256; $ j = ($ j + $ box [$ a]) % 256; $ tmp = $ box [$ a]; $ box [$ a] = $ box [$ j]; $ box [$ j] = $ tmp; $ k = $ box [($ box [$ a] + $ box [$ j]) % 256)]; $ cipher. = chr (ord ($ data [$ I]) ^ $ k);} return $ cipher ;}

Method 3:

/** + ----------------------------------------------------- * Mcrypt encryption/decryption * @ param String $ date the data to be encrypted and decrypted * @ param String $ mode encode is encrypted/decode is decrypted * @ return String * @ author zxing@97md.net Mon Sep 14 22:59:28 CST 2009 + ----------------------------------------------------- * @ example */function ZxingCrypt ($ date, $ mode = 'encoding') {$ key = md5 ('zing'); // Generate a key using MD5 hash, note that the encryption and decryption keys must be unified if ($ mode = 'decode') {$ date = base64_decode ($ date);} if (function_exists ('mcrypt _ create_iv ')) {$ iv_size = Encrypt (encrypt, MCRYPT_MODE_ECB); $ iv = mcrypt_create_iv ($ iv_size, MCRYPT_RAND);} if (isset ($ iv) & $ mode = 'encoding ') {$ passcrypt = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ date, MCRYPT_MODE_ECB, $ iv);} elseif (isset ($ iv) & $ mode = 'decode ') {$ passcrypt = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $ key, $ date, MCRYPT_MODE_ECB, $ iv);} if ($ mode = 'encoding ') {$ passcrypt = base64_encode ($ passcrypt);} return $ passcrypt ;}

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.