PHP AES Encryption compatible net

Source: Internet
Author: User
Tags decrypt php tutorial pkcs7
These days to do a PHP AES encryption and then to the. NET decryption program, did a PKCS7 complement, ciphertext to. NET still decryption failed, prompting The padding is invalid and cannot be removed.By checking the program, the original encryption vector was incorrectly written. The format of the crypto vector in. NET is an array, and in PHP it should be converted to a string with a slash, and I've deleted a 0 more in the conversion. See the program below, set your own key and IV to use these programs.

Class aesmcrypt{/** * Set default encryption key 32 bit * @var STR * For confidentiality omit the latter half */private static $defaultKey = "1a426b    316fb648 ... ";"; " /** * Set default encryption vector * @var str * For confidentiality omit the latter part *///IN. NET in format =/$iv = ' {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF    ............}';         Private $iv = "\x12\x34\x56\x78\x90\xab\xcd\xef\ ....";;          /** * Set Encryption algorithm * @var STR */private $cipher;          /** * Set Encryption mode * @var STR */private $mode;         Public function __construct ($cipher = mcrypt_rijndael_128, $mode = MCRYPT_MODE_CBC) {$this->cipher = $cipher;     $this->mode = $mode;      /** * Encrypt content, note that this encryption method uses padding PKCS7 for content before encryption. * @param str $content What needs to be encrypted * @return ciphertext after STR encryption */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 Complement * * @param string $string plaintext * * @return String */function addpkcs7padding ($string) {$blocksize = Mcrypt_ Get_block_size ($this->cipher, $this->mode); $len = strlen ($string); Gets the string length $pad = $blocksize-($len% $blocksize); The length of the complement is $string. = Str_repeat (Chr ($pad), $pad);    Complement the last return $string with an ASCII-complement-length character;} /** * Decrypt the content, note that the encryption method first decrypts the content.      Then use padding PKCS7 for decrypted content to remove special characters. * @param string $content What to decrypt * @return The decrypted content of String */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 () {returnSelf:: $defaultKey;}} 

The above describes the PHP AES encryption compatible NET, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.