About 3DES Encryption

Source: Internet
Author: User
I need to contact you here. NET to perform data interaction, the data needs to be encrypted, but now the problem is that after I use PHP to encrypt and. the data after the net encryption is not equal, and the Code on their side is correct. This can be confirmed, so the encryption on my side... I need to contact you here. NET to perform data interaction, the data needs to be encrypted, but now the problem is that after I use PHP to encrypt and. the data after encryption is not equal. The code on the other side is correct. This can be confirmed, so there is a problem with encryption on my side, the specific problem is not very clear. Please kindly advise. The following is my code:

/**

* 3DES encryption processing class
*
* @ Author fbbin fbbin@gmail.com
* @ Version 1.0 beta
* @ Created 2014/01/07
* @ Logs
*/
Class Crypt3Des {

/*** Key required for encryption * @ var intval */protected static $ key = ""; /*** initialize the static structure ** @ return Crypt3Des */public static function instance ($ key = '') {self: $ key = $ key; return new static ;} /*** encrypt the string ** @ return string */public function encrypt ($ input) {$ size = mcrypt_get_block_size (MCRYPT_3DES, MCRYPT_MODE_ECB ); $ input = $ this-> paddingPKCS7 ($ input); $ key = str_pad (self: $ key, 24, '0'); $ td = mcrypt_mod Ule_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB,''); $ iv = @ mcrypt_create_iv (encrypt ($ td), MCRYPT_RAND); @ mcrypt_generic_init ($ td, $ key, $ iv); $ data = mcrypt_generic ($ td, $ input); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); basereturn 64_encode ($ data );} /*** decrypt the string ** @ return string */public function decrypt ($ encrypted) {$ encrypted = base64_decode ($ encrypted); $ key = str_p Ad (self: $ key, 24, '0'); $ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB ,''); $ iv = @ mcrypt_create_iv (encrypt ($ td), MCRYPT_RAND); $ ks = mcrypt_enc_get_key_size ($ td); @ mcrypt_generic_init ($ td, $ key, $ iv ); $ decrypted = mdecrypt_generic ($ td, $ encrypted); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); return $ this-> pkcs5Unpad ($ decrypted );} /*** pack strings according to rules ** @ return st Ring */public function paddingPKCS7 ($ data) {$ block_size = encrypt (MCRYPT_3DES, MCRYPT_MODE_ECB); $ padding_char = $ block_size-(strlen ($ data) % $ block_size ); $ data. = str_repeat (chr ($ padding_char), $ padding_char); return $ data ;} /*** unpack strings according to rules ** @ return string */public function pkcs5Unpad ($ text) {$ pad = ord ($ text {strlen ($ text) -1}); if ($ pad> strlen ($ text) {return false;} if (Strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) {return false;} return substr ($ text, 0,-1 * $ pad );}

}

Usage:

echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1');

The result I got here is: mzbVH6DmUXI =, and the correct value should be: sztImTSd0o;

Thank you very much for your advice.

Reply content:

I need to contact you here. NET to perform data interaction, the data needs to be encrypted, but now the problem is that after I use PHP to encrypt and. the data after encryption is not equal. The code on the other side is correct. This can be confirmed, so there is a problem with encryption on my side, the specific problem is not very clear. Please kindly advise. The following is my code:

/**

* 3DES encryption processing class
*
* @ Author fbbin fbbin@gmail.com
* @ Version 1.0 beta
* @ Created 2014/01/07
* @ Logs
*/
Class Crypt3Des {

/*** Key required for encryption * @ var intval */protected static $ key = ""; /*** initialize the static structure ** @ return Crypt3Des */public static function instance ($ key = '') {self: $ key = $ key; return new static ;} /*** encrypt the string ** @ return string */public function encrypt ($ input) {$ size = mcrypt_get_block_size (MCRYPT_3DES, MCRYPT_MODE_ECB ); $ input = $ this-> paddingPKCS7 ($ input); $ key = str_pad (self: $ key, 24, '0'); $ td = mcrypt_mod Ule_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB,''); $ iv = @ mcrypt_create_iv (encrypt ($ td), MCRYPT_RAND); @ mcrypt_generic_init ($ td, $ key, $ iv); $ data = mcrypt_generic ($ td, $ input); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); basereturn 64_encode ($ data );} /*** decrypt the string ** @ return string */public function decrypt ($ encrypted) {$ encrypted = base64_decode ($ encrypted); $ key = str_p Ad (self: $ key, 24, '0'); $ td = mcrypt_module_open (MCRYPT_3DES, '', MCRYPT_MODE_ECB ,''); $ iv = @ mcrypt_create_iv (encrypt ($ td), MCRYPT_RAND); $ ks = mcrypt_enc_get_key_size ($ td); @ mcrypt_generic_init ($ td, $ key, $ iv ); $ decrypted = mdecrypt_generic ($ td, $ encrypted); mcrypt_generic_deinit ($ td); mcrypt_module_close ($ td); return $ this-> pkcs5Unpad ($ decrypted );} /*** pack strings according to rules ** @ return st Ring */public function paddingPKCS7 ($ data) {$ block_size = encrypt (MCRYPT_3DES, MCRYPT_MODE_ECB); $ padding_char = $ block_size-(strlen ($ data) % $ block_size ); $ data. = str_repeat (chr ($ padding_char), $ padding_char); return $ data ;} /*** unpack strings according to rules ** @ return string */public function pkcs5Unpad ($ text) {$ pad = ord ($ text {strlen ($ text) -1}); if ($ pad> strlen ($ text) {return false;} if (Strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) {return false;} return substr ($ text, 0,-1 * $ pad );}

}

Usage:

echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1');

The result I got here is: mzbVH6DmUXI =, and the correct value should be: sztImTSd0o;

Thank you very much for your advice.

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.