Implementation of symmetric encryption algorithm in PHP

Source: Internet
Author: User
Tags decrypt
Recent whim to the database to save some confidential things, and then think how to let others even into the database can not understand what is stored, then only encryption; but we have to look at it, it can only find some symmetric encryption algorithm, we want to see when the decryption back. The following is an introduction to the simple implementation of symmetric encryption algorithm in PHP.

Objective

Found a good PHP symmetric encryption algorithm on the Internet, in the PHP syntax environment, there are UrlEncode and Urldecode,base64_encode and Base64_decode with the symmetric algorithm, but these self-band algorithm can not be called encryption algorithm, It can only be said to be a coding method. But we can use these to do some processing, to implement a simple encryption and decryption algorithm.

This time the encryption and decryption algorithms are adapted using the Base64 method. Usually we use the base64_encode($str) resulting string, without any processing, base64_decode() to return to our previous string, but if we base64_encode() insert a few characters into the string, then he will not turn back, even if it is not our own string.

The sample code is as follows:

 <?php $content = "Hello everyone, I am Chinese, who you are"; /** * Encryption of simple symmetric encryption * @param string $string strings that need to be encrypted * @param string $skey Encrypt Eky * @return String */function encode ($string = ', $skey = ' Wenzi ') {$STRARR = Str_split (Base64_encode ($string)); $strCount = count ($STRARR); foreach (Str_split ($skey As $key + $value) $key < $strCount && $strArr [$key].= $value; return Str_replace (Array (' = ', ' + ', '/'), Array (' o0o0o ', ' o000o ', ' oo00o '), join (', $STRARR)); /** * Decryption of simple symmetric encryption algorithm * @param string $string The string to decrypt * @param string $skey Decrypt key * @return String */function decode ($strin g = ', $skey = ' Wenzi ') {$STRARR = Str_split (Str_replace (' o0o0o ', ' o000o ', ' oo00o '), array (' = ', ' + ', '/'), $string ), 2); $strCount = count ($STRARR); foreach (Str_split ($skey) as $key + $value) $key <= $strCount && $strArr [$key][1] = = = = $value && $s trarr[$key] = $STRARR [$key][0]; Return Base64_decode (Join (', $STRARR)); } echo ' <pre> '; echo "string:". $content. "<br/>"; ECho "encode:". ($enstring = Encode ($content)). ' <br/> '; echo "Decode:". Decode ($enstring); Exit ();

In the algorithm above we can see: we base64_encode() insert the resulting character into our pre-set key, and then replace the special characters inside, even if others see such a string does not know what it is. Of course, here we have a little bit of improvement, such as inserting the key backwards into the string, the key base64 after the insertion, and so on, insert the key and then Base64 once.

Of course, decryption is the opposite direction of encryption, thinking for a while to know the principle of decryption: before we inserted some characters in the string, now decryption we have to pick him out, first the encrypted string in each array of 2 elements to group, and then determine whether the second character is in the key, if it is, Then the first character is the character in the original Base64.

The above is the whole content of this article, I hope that everyone's study has helped.


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.