PHP 3DES Encryption Algorithm implementation

Source: Internet
Author: User
Something that needs to be done recently, PHP returns a 3DES encrypted URL to. NET, and then. NET-side decryption. PHP implementation has been unable to match the. NET side, first remember here, want to see the big God pointing. Countless copy of the online people, there is no personal verification, I hope we can uphold the rigorous way.

PHP encryption Extension Library MCrypt before data is encrypted and decrypted, an initialization vector is first created, referred to as IV. by $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ($cipher, $modes), mcrypt_rand); the creation of an initialization vector requires two parameters: size Specifies the magnitude of IV; Source is the origin of IV , where the value Mcrypt_rand is the system random number.

The function mcrypt_get_iv_size ($cipher, $modes) returns the initialization vector size, and the parameter cipher and mode respectively refer to the algorithm and encryption mode.

function Encrypt ($input) {//Data encryption $key =MD5 ("xxxxxxxxx", TRUE), $key =base64_encode ($key); $size = Mcrypt_get_block_size ( MCRYPT_3DES,MCRYPT_MODE_ECB); $input = $this->pkcs5_pad ($input, $size); $key = Str_pad ($this->key,24, ' 0 '); $key = Str_pad ($key, 24, ' 0 '); $TD = Mcrypt_module_open (Mcrypt_3des, ' ', MCRYPT_MODE_ECB, ');//$iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);//initialization vector $iv = Mcrypt_create_iv (Mcrypt_get_iv_size (MCRYPT_3DES,MCRYPT_MODE_ECB), MCRYPT_RAND); @mcrypt_generic_init ($TD, $key, $IV); $data = Mcrypt_generic ($TD, $input); Mcrypt_generic_deinit ($TD); Mcrypt_module_close ($TD);    $data = Base64_encode ($this->paddingpkcs7 ($data)); $data = Base64_encode ($data); return  $data;}
function Pkcs5_pad ($text, $blocksize) {$pad = $blocksize-(strlen ($text)% $blocksize), return $text. Str_repeat (Chr ($ PAD), $pad); }

The. NET side code is implemented as follows:

var des = new TripleDESCryptoServiceProvider ();                var hashMd5 = new MD5CryptoServiceProvider ();                Des. Key = Hashmd5.computehash (Encoding.Default.GetBytes (strkey));                Des. Mode = CIPHERMODE.ECB;                Des. Padding = Paddingmode.zeros;                var desencrypt = des. CreateEncryptor ();                var buffer = Encoding.Default.GetBytes (strstring);                Return convert.tobase64string (desencrypt.transformfinalblock (buffer, 0, buffer. Length));

Found another way to implement PHP, is not.

function Encrypt ($string) {              //encryption key file                $key =md5 ("xxxxxxxxxxxx", TRUE);               $key =base64_encode ($key);              Encryption method               $cipher _alg = mcrypt_tripledes;              Initialize vectors to increase security               $iv = Mcrypt_create_iv (mcrypt_get_iv_size ($cipher _ALG,MCRYPT_MODE_ECB), mcrypt_rand);                              Start encrypting               $encrypted _string = Mcrypt_encrypt ($cipher _alg, $key, $string, MCRYPT_MODE_ECB, $iv);               Return Base64_encode ($encrypted _string);//Convert to 16 binary       }

Countless copy of the online people, there is no personal verification, I hope we can uphold the rigorous way.

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