Home > Backend Development > PHP Tutorial > 关于3DES加密的问题

关于3DES加密的问题

WBOY
Release: 2016-06-06 20:46:00
Original
1135 people have browsed it

我现在这边需要跟.NET那边进行数据交互,数据是需要进行加密处理的,但是现在的问题是,我用PHP的加密处理之后和.net加密处理之后的数据不相等,他们那边的代码是没有问题的,这个是可以确认的,那么我这边的加密就有问题了,现在具体的问题不是很清楚,求各位大神指教,下面是我这边的代码:

<code>/**
</code>
Copy after login
Copy after login

* 3DES加密处理类
*
* @author fbbin fbbin@gmail.com
* @version 1.0 beta
* @created 2014/01/07
* @logs
*/
class Crypt3Des {

<code>/**
 * 加密需要的key 
 * @var intval
 */
protected static $key = "";

/**
 * 静态构造初始化
 * 
 * @return Crypt3Des
 */
public static function instance($key = '') {
    self::$key = $key;
    return new static;
}

/**
 * 对字符串进行加密
 * 
 * @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_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);
}

/**
 * 对字符串进行解密
 * 
 * @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);
}

/**
 * 对字符串按照规则打包
 * 
 * @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;
}

/**
 * 对字符串按照规则解包
 * 
 * @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);
}
</code>
Copy after login
Copy after login

}

使用方式:

<code>echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1');
</code>
Copy after login
Copy after login

我这边得出的结果是:mzbVH6DmUXI=, 而正确的值应该是:sztImTSd0o;

求指教,万分感谢。

回复内容:

我现在这边需要跟.NET那边进行数据交互,数据是需要进行加密处理的,但是现在的问题是,我用PHP的加密处理之后和.net加密处理之后的数据不相等,他们那边的代码是没有问题的,这个是可以确认的,那么我这边的加密就有问题了,现在具体的问题不是很清楚,求各位大神指教,下面是我这边的代码:

<code>/**
</code>
Copy after login
Copy after login

* 3DES加密处理类
*
* @author fbbin fbbin@gmail.com
* @version 1.0 beta
* @created 2014/01/07
* @logs
*/
class Crypt3Des {

<code>/**
 * 加密需要的key 
 * @var intval
 */
protected static $key = "";

/**
 * 静态构造初始化
 * 
 * @return Crypt3Des
 */
public static function instance($key = '') {
    self::$key = $key;
    return new static;
}

/**
 * 对字符串进行加密
 * 
 * @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_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);
}

/**
 * 对字符串进行解密
 * 
 * @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);
}

/**
 * 对字符串按照规则打包
 * 
 * @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;
}

/**
 * 对字符串按照规则解包
 * 
 * @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);
}
</code>
Copy after login
Copy after login

}

使用方式:

<code>echo Crypt3Des::instance(base64_encode('FD3BCD75678B4BABB6E8948149CA6C91'))->encrypt('1');
</code>
Copy after login
Copy after login

我这边得出的结果是:mzbVH6DmUXI=, 而正确的值应该是:sztImTSd0o;

求指教,万分感谢。

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template