About the 3DES encryption problem

Source: Internet
Author: User
I need to talk to you right now. NET, data is encrypted, but now the problem is, I use PHP encryption after processing and after the. NET encryption processing data is not equal, their code there is no problem, this can be confirmed, then my side of the encryption there is a problem, and now the specific problem is not very clear, please the great God, the following is my side of the code:

/**

* 3DES Encryption Processing class
*
* @author Fbbin fbbin@gmail.com
* @version 1.0 Beta
* @created 2014/01/07
* @logs
*/
Class Crypt3des {

/** * Encryption Required key * @var intval */protected static $key = "";/** * Static Construction initialization * * @return crypt3des */public static function I    Nstance ($key = ") {self:: $key = $key; return new Static;} /** * Encrypt String * * @return string */public function Encrypt ($input) {$size = Mcrypt_get_block_size (Mcrypt_3des, Mcryp    T_MODE_ECB);    $input = $this->PADDINGPKCS7 ($input);    $key = Str_pad (self:: $key, 24, ' 0 ');    $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, ');    $iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);    @mcrypt_generic_init ($TD, $key, $IV);    $data = Mcrypt_generic ($TD, $input);    Mcrypt_generic_deinit ($TD);    Mcrypt_module_close ($TD); Return Base64_encode ($data);}    /** * Decrypt String * * @return string */public function Decrypt ($encrypted) {$encrypted = Base64_decode ($encrypted);    $key = Str_pad (self:: $key, 24, ' 0 ');    $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, '); $iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($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);} /** * Package Strings according to rules * * @return string */public function paddingPKCS7 ($data) {$block _size = mcrypt_get_block_size (mcrypt    _3des, MCRYPT_MODE_ECB);    $padding _char = $block _size-(strlen ($data)% $block _size);    $data. = Str_repeat (Chr ($padding _char), $padding _char); return $data;}    /** * Unpack the string according to the 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);}

}

How to use:

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

The result of my side is: mzbvh6dmuxi=, and the correct value should be: sztimtsd0o;

Thank you for your advice.

Reply content:

I need to talk to you right now. NET, data is encrypted, but now the problem is, I use PHP encryption after processing and after the. NET encryption processing data is not equal, their code there is no problem, this can be confirmed, then my side of the encryption there is a problem, and now the specific problem is not very clear, please the great God, the following is my side of the code:

/**

* 3DES Encryption Processing class
*
* @author Fbbin fbbin@gmail.com
* @version 1.0 Beta
* @created 2014/01/07
* @logs
*/
Class Crypt3des {

/** * Encryption Required key * @var intval */protected static $key = "";/** * Static Construction initialization * * @return crypt3des */public static function I    Nstance ($key = ") {self:: $key = $key; return new Static;} /** * Encrypt String * * @return string */public function Encrypt ($input) {$size = Mcrypt_get_block_size (Mcrypt_3des, Mcryp    T_MODE_ECB);    $input = $this->PADDINGPKCS7 ($input);    $key = Str_pad (self:: $key, 24, ' 0 ');    $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, ');    $iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($TD), Mcrypt_rand);    @mcrypt_generic_init ($TD, $key, $IV);    $data = Mcrypt_generic ($TD, $input);    Mcrypt_generic_deinit ($TD);    Mcrypt_module_close ($TD); Return Base64_encode ($data);}    /** * Decrypt String * * @return string */public function Decrypt ($encrypted) {$encrypted = Base64_decode ($encrypted);    $key = Str_pad (self:: $key, 24, ' 0 ');    $TD = Mcrypt_module_open (Mcrypt_3des, ', MCRYPT_MODE_ECB, '); $iv = @mcrypt_create_iv (Mcrypt_enc_get_iv_size ($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);} /** * Package Strings according to rules * * @return string */public function paddingPKCS7 ($data) {$block _size = mcrypt_get_block_size (mcrypt    _3des, MCRYPT_MODE_ECB);    $padding _char = $block _size-(strlen ($data)% $block _size);    $data. = Str_repeat (Chr ($padding _char), $padding _char); return $data;}    /** * Unpack the string according to the 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);}

}

How to use:

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

The result of my side is: mzbvh6dmuxi=, and the correct value should be: sztimtsd0o;

Thank you 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.