Home Backend Development PHP Tutorial PHP 3DES encryption algorithm implementation

PHP 3DES encryption algorithm implementation

Nov 12, 2016 pm 03:03 PM
algorithm

What I need to do recently is that php returns a 3des encrypted URL to .net, and then the .net side decrypts it. The implementation of php has never been able to match the .net end. I will write it down here first and hope to see some guidance from experts. There are countless people on the Internet who copy and copy without verifying it personally. I hope everyone can adhere to the rigorous method in the future.

Before the PHP encryption extension library Mcrypt encrypts and decrypts data, it first creates an initialization vector, referred to as iv for short. From $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND); it can be seen that creating an initialization vector requires two parameters: size specifies the size of iv; source is the source of iv, and the value MCRYPT_RAND is the system random number.

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

function encrypt($input){//数据加密
 $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);//初始化向量
$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;
 }
Copy after login
 function pkcs5_pad ($text, $blocksize) {
 $pad = $blocksize - (strlen($text) % $blocksize);
 return $text . str_repeat(chr($pad), $pad);
 }
Copy after login

The code implementation on the .net side is 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));
Copy after login

I found another implementation method of php, neither of which works.

function encrypt($string) {
              //加密用的密钥文件 
               $key=md5("XXXXXXXXXXXX",TRUE);
               $key=base64_encode($key);
              //加密方法 
              $cipher_alg = MCRYPT_TRIPLEDES;
              //初始化向量来增加安全性 
              $iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,MCRYPT_MODE_ECB), MCRYPT_RAND); 
               
              //开始加密 
              $encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv); 
              return base64_encode($encrypted_string);//转化成16进制
       }
Copy after login

There are countless people on the Internet who copy and copy without verifying it personally. I hope everyone can adhere to the rigorous method in the future.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance Mar 26, 2024 pm 12:41 PM

CLIP-BEVFormer: Explicitly supervise the BEVFormer structure to improve long-tail detection performance

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions Jun 03, 2024 pm 01:25 PM

Implementing Machine Learning Algorithms in C++: Common Challenges and Solutions

Explore the underlying principles and algorithm selection of the C++sort function Explore the underlying principles and algorithm selection of the C++sort function Apr 02, 2024 pm 05:36 PM

Explore the underlying principles and algorithm selection of the C++sort function

Practice and reflections on Jiuzhang Yunji DataCanvas multi-modal large model platform Practice and reflections on Jiuzhang Yunji DataCanvas multi-modal large model platform Oct 20, 2023 am 08:45 AM

Practice and reflections on Jiuzhang Yunji DataCanvas multi-modal large model platform

Improved detection algorithm: for target detection in high-resolution optical remote sensing images Improved detection algorithm: for target detection in high-resolution optical remote sensing images Jun 06, 2024 pm 12:33 PM

Improved detection algorithm: for target detection in high-resolution optical remote sensing images

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Can artificial intelligence predict crime? Explore CrimeGPT's capabilities Mar 22, 2024 pm 10:10 PM

Can artificial intelligence predict crime? Explore CrimeGPT's capabilities

PHP algorithm analysis: efficient method to find missing numbers in an array PHP algorithm analysis: efficient method to find missing numbers in an array Mar 02, 2024 am 08:39 AM

PHP algorithm analysis: efficient method to find missing numbers in an array

Application of algorithms in the construction of 58 portrait platform Application of algorithms in the construction of 58 portrait platform May 09, 2024 am 09:01 AM

Application of algorithms in the construction of 58 portrait platform

See all articles