PhpAES encryption is compatible with net

Source: Internet
Author: User
Tags pkcs7
: This article mainly introduces phpAES encryption and compatibility with net. if you are interested in the PHP Tutorial, refer to it. Perform a php aes encryption over the past few days and then go. net decryption program, completed the pkcs7 complement, ciphertext.. net still fails to be decrypted, prompting that the filling is invalid and cannot be removed. by checking the program, it turns out that the encrypted vector is wrong. In. net, the format of the encryption vector is an array. in php, it should be converted to a string with a slash. I deleted a 0 more during the conversion. Next, let's look at the program. after setting your own key and iv, you can use these programs.

Class AESMcrypt {/*** sets the default encryption key 32-bit * @ var str * for the sake of confidentiality, the latter half is Omitted */private static $ defaultKey = "1a% B %fb648 ....... .... ";/*** set the default encryption vector * @ var str * to keep the second half confidential * // in. net format: // $ iv = '{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF ............} '; private $ iv = "\ x12 \ x34 \ x56 \ x78 \ x90 \ xAB \ xCD \ xEF \........... ";/*** set the encryption algorithm * @ var str */private $ cipher;/*** set the encryption mode * @ var str */private $ mode; publi C function _ construct ($ cipher = MCRYPT_RIJNDAEL_128, $ mode = MCRYPT_MODE_CBC) {$ this-> cipher = $ cipher; $ this-> mode = $ mode ;} /*** encrypt the content. Note that this encryption method uses padding pkcs7 for the content and then encrypts it. * @ Param str $ content to be encrypted * @ return str encrypted ciphertext */public function encrypt ($ content) {if (empty ($ content )) {return null;} $ srcdata = $ this-> addPkcs7Padding ($ content); return mcrypt_encrypt ($ this-> cipher, $ this-> getSecretKey (), $ srcdata, $ this-> mode, $ this-> iv );} /*** pkcs7 completion code ** @ param string $ string plaintext ** @ return String */function addPkcs7Padding ($ string) {$ blocksize = mcrypt_get_block_size ($ This-> cipher, $ this-> mode); $ len = strlen ($ string ); // Obtain the string length $ pad = $ blocksize-($ len % $ blocksize); // Obtain the length of the complement code $ string. = str_repeat (chr ($ pad), $ pad); // use the ASCII code to pad the last return $ string;}/*** to decrypt the content, note that content is decrypted first in this encryption method. Then, use padding pkcs7 to remove special characters from the decrypted content. * @ Param String $ content the content to be decrypted * @ return String the decrypted content */public function decrypt ($ content) {if (empty ($ content )) {return null;} $ content = mcrypt_decrypt ($ this-> cipher, $ this-> getSecretKey (), $ content, $ this-> mode, $ this-> iv ); // $ block = mcrypt_get_block_size ($ this-> cipher, $ this-> mode); $ pad = ord ($ content [($ len = strlen ($ content )) -1]); return substr ($ content, 0, strlen ($ content)-$ pad);} public function getSecretKey () {return self: $ defaultKey ;}}

The above introduces php AES encryption and compatibility with net, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.