Encryption and decryption _php instance of PHP symmetric encryption function

Source: Internet
Author: User
Tags chr decrypt md5 ord strlen asymmetric encryption

One place in the project uses the need to encrypt the user ID and pass it to the next contact for the reverse solution. (Forgive me not to disclose too many-_-!), the first thought is a function in the kangsheng ucenter, later searched, in the concise magic also found a simple method, hence the integration of the next, formed its own use of the function.

First, symmetric encryption

The sender uses the secret key and algorithm to send out the ciphertext, the receiver uses the key and algorithm to process the ciphertext into plaintext, and the sending and receiving parties use the same key to encrypt and decrypt the data.

Because using the same key to encrypt and decrypt, security is not only related to the algorithm, the security of the key is also important.

Of course not the more complex the key is better, the opposite key is usually relatively small, because although the larger the key, the stronger the encryption, but the process of encryption and decryption more slowly, so the size of the key to take care of security, but also to take care of efficiency.

After all, the symmetric encryption algorithm is characterized by open algorithm, small amount of computation, fast encryption, high encryption efficiency, without the advantage of high efficiency, it is better to directly use asymmetric encryption.

In addition, each pair of users use a symmetric encryption algorithm each time, you need to use the only other people do not know the key, which will allow the sender of the number of keys to have a geometric growth, key management becomes the user's burden.

Symmetric encryption algorithm is difficult to use in distributed network system, mainly because of the difficulty of key management and high cost of use.

Two, asymmetric encryption

Asymmetric encryption, which is relatively secure, uses a pair of keys that expose the key and private keys, respectively, for encryption and decryption. The private key can only be safely kept by one party and cannot be leaked, while the public key may be sent to anyone who requests it.

The most common asymmetric encryption should be the banking system, the payment platform. For example, we apply for Alipay or UnionPay payment interface, will get a public key, the mall to pay is to use public key to encrypt information to the platform, the platform using the key to decrypt your information, to carry out payment operations.

Although asymmetric encryption is safe, it is compared with symmetric encryption, it is very slow, so we generally deal with, most of the use of symmetric encryption to send messages, but the key to symmetric encryption we can send through the asymmetric encryption, recall your application to the payment interface, is not given you a pair of keys? ^.^

Third, the combination of use

Symmetric encryption speed, send a large number of data is better. Asymmetric encryption and decryption takes a long, slow time and is only suitable for encrypting small amounts of data, but the security of asymmetric encryption is extremely high.

Weaknesses: The symmetric encryption of the key using asymmetric encryption of the public key to encrypt, and then sent out, the receiver uses the private key to decrypt the symmetric encryption of the key, and then the two sides can use symmetric encryption to communicate.

The method used in the project should not be disclosed, just list two other examples here. The first one is Ucenter, and the second one is seen in the simple magic.

It should be noted that, because it is the Base64 algorithm, the encrypted string may appear + \, if used in the URL, is not friendly, can be external or to change the method, the regular verification of recursion to remove.

/** * string encryption and Decryption function * @param string $string The original text or ciphertext * @param string $operation operation (ENCODE |  DECODE), the default is DECODE * @param string $key key * @param int $expiry ciphertext validity period, encryption time valid, unit seconds, 0 for permanent valid * @return string processing of the original or After Base64_encode processed ciphertext/function _authcode ($string, $operation = ' DECODE ', $key = ' Ruesin ', $expiry = 0) {$ckey _le
 Ngth = 4;
 $key = MD5 ($key);
 $keya = MD5 (substr ($key, 0, 16));
 $KEYB = MD5 (substr ($key, 16, 16)); $KEYC = $ckey _length?
 ($operation = = ' DECODE ' substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): "; $cryptkey = $keya.
 MD5 ($keya. $KEYC);
 $key _length = strlen ($cryptkey); $string = $operation = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', $expiry? $expiry + Time (): 0).
    SUBSTR (MD5 ($string. $keyb), 0, 16).
 $string _length = strlen ($string);
 $result = ';
 $box = Range (0, 255);
 $rndkey = Array (); for ($i = 0; $i <= 255 $i + +) {$rndkey [$i] = Ord($cryptkey [$i% $key _length]);
  for ($j = $i = 0; $i < 256 $i + +) {$j = ($j + $box [$i] + $rndkey [$i])% 256;
  $tmp = $box [$i];
  $box [$i] = $box [$j];
 $box [$j] = $tmp;
  for ($a = $j = $i = 0; $i < $string _length $i + +) {$a = ($a + 1)% 256;
  $j = ($j + $box [$a])% 256;
  $tmp = $box [$a];
  $box [$a] = $box [$j];
  $box [$j] = $tmp;
 $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]); } if ($operation = = ' DECODE ') {if (substr ($result, 0,) = 0 | | | substr ($result, 0)-time () > 0) &&amp
  substr ($result) = = substr (MD5 (substr ($result,). $keyb) (0)) {return substr ($result, 26);
  else {return ';
 } else {return $KEYC. Str_replace (' = ', ', Base64_encode ($result));
/********************************************************************* function Name: Encrypt function: Encrypt decryption string use method: Encryption: Encrypt (
' Str ', ' E ', ' nowamagic ';
Decryption: Encrypt (' encrypted string ', ' D ', ' nowamagic ');
Parameter description: $string: Need to encrypt the decrypted string $operation: To determine whether encryption or decryption: E: Encryption D: Decryption $key: Encryption key (key); /function Encrypt ($string, $operation, $key =
 "{$key =md5 ($key);
 $key _length=strlen ($key);
 $string = $operation = = ' D ' Base64_decode ($string): substr (MD5 ($string. $key), 0,8). $string;
 $string _length=strlen ($string);
 $rndkey = $box =array ();
 $result = ';
  For ($i =0 $i <=255; $i + +) {$rndkey [$i]=ord ($key [$i% $key _length]);
 $box [$i]= $i;
  for ($j = $i =0; $i <256; $i + +) {$j = ($j + $box [$i]+ $rndkey [$i])%256;
  $tmp = $box [$i];
  $box [$i]= $box [$j];
 $box [$j]= $tmp;
  for ($a = $j = $i =0 $i < $string _length $i + +) {$a = ($a + 1)%256;
  $j = ($j + $box [$a])%256;
  $tmp = $box [$a];
  $box [$a]= $box [$j];
  $box [$j]= $tmp;
 $result. =CHR (Ord ($string [$i]) ^ ($box [($box [$a]+ $box [$j])%256]);} if ($operation = = = ' D ') {if (substr ($result, 0,8) ==substr (MD5 (substr ($result, 8). $key), 0,8)) {return substr ($result,
  else {return ';
 } else {return str_replace (' = ', ', ' Base64_encode ($result));

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.