Simple symmetric encryption algorithm implementation in php

Source: Internet
Author: User
Recently, it was a whim to save some confidential things to the database, and then I thought about how to make others even if they enter the database and do not understand what the storage is, so there is only encryption; but we have to look at it ourselves. we can only find some symmetric encryption algorithms. we can decrypt them when we want. The following describes the implementation of simple symmetric encryption algorithms in php. Preface

I found a good symmetric encryption algorithm for PHP on the internet. in the PHP syntax environment, there are symmetric algorithms that come with urlencode and urldecode, base64_encode, and base64_decode, however, these built-in algorithms cannot be called encryption algorithms. they can only be called encoding methods. However, we can use this to perform some processing to implement simple encryption and decryption algorithms.

This encryption and decryption algorithm is adapted using base64. We usually use base64_encode ($ str) to generate strings. without any processing, base64_decode () can be converted back to our previous strings. However, if we use base64_encode () after a few characters are inserted into the string, it cannot be converted back, even if it is not our own string.

The sample code is as follows:

 $ 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 be decrypted * @ param String $ skey decryption KEY * @ return string */function decode ($ String = '', $ skey = 'wenzi') {$ strArr = str_split (str_replace (array ('Oss 0o', 'Oss 000o', 'oo00o'), array ('=', '+ ', '/'), $ string), 2); $ strCount = count ($ strArr); foreach (str_split ($ skey) as $ key => $ value) $ key <= $ strCount & $ strArr [$ key] [1] ===$ value & $ strArr [$ key] = $ strArr [$ key] [0]; return base64_decode (join ('', $ strArr);} echo'
';echo "string : " . $content . " 
";echo "encode : " . ($enstring = encode($content)) . '
';echo "decode : " . decode($enstring); exit();

In the above algorithm, we can see that we insert the key we set in advance into the characters produced by base64_encode (), and then replace the special characters in it, even if someone else sees such a string, they do not know what it is. Of course, here we will make a slight improvement, such as inserting the key into the string backwards and then inserting the key base64, and then inserting the key again.

Of course, decryption is the opposite direction of encryption. after thinking for a while, we will know the principle of decryption: We inserted some characters in the string before, and now we will extract it when decrypting, first, divide the encrypted string into two elements in each array, and then determine whether the second character is in the key. if so, the first character is the character in The Original base64.

Summary

The above is all the content of this article. of course, in addition to base64 encryption and decryption in php, there are also AES and DES algorithms. if you need them, you can pay attention to your own home, I believe it will help you.

For more information about the implementation of simple symmetric encryption algorithms in php, refer to the Chinese PHP website!

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.