diff --git a/corp/crypto/DingtalkCrypt.php b/corp/crypto/DingtalkCrypt.php index 2eecd14..58eae24 100755 --- a/corp/crypto/DingtalkCrypt.php +++ b/corp/crypto/DingtalkCrypt.php @@ -11,7 +11,7 @@ class DingtalkCrypt private $m_suiteKey; - public function DingtalkCrypt($token, $encodingAesKey, $suiteKey) + public function __construct($token, $encodingAesKey, $suiteKey) { $this->m_token = $token; $this->m_encodingAesKey = $encodingAesKey; diff --git a/corp/crypto/pkcs7Encoder.php b/corp/crypto/pkcs7Encoder.php index 2b6948c..9d89b53 100755 --- a/corp/crypto/pkcs7Encoder.php +++ b/corp/crypto/pkcs7Encoder.php @@ -9,11 +9,10 @@ class PKCS7Encoder function encode($text) { - $block_size = PKCS7Encoder::$block_size; $text_length = strlen($text); $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size); if ($amount_to_pad == 0) { - $amount_to_pad = PKCS7Encoder::block_size; + $amount_to_pad = PKCS7Encoder::$block_size; } $pad_chr = chr($amount_to_pad); $tmp = ""; @@ -39,7 +38,7 @@ class Prpcrypt { public $key; - function Prpcrypt($k) + function __construct($k) { $this->key = base64_decode($k . "="); } @@ -51,22 +50,11 @@ public function encrypt($text, $corpid) //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $corpid; - // 网络字节序 - $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); - //使用自定义的填充方式对明文进行补位填充 $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); - mcrypt_generic_init($module, $this->key, $iv); - //加密 - $encrypted = mcrypt_generic($module, $text); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); - - //print(base64_encode($encrypted)); - //使用BASE64对加密后的字符串进行编码 - return array(ErrorCode::$OK, base64_encode($encrypted)); + $encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv); + return array(ErrorCode::$OK, $encrypted); } catch (Exception $e) { print $e; return array(ErrorCode::$EncryptAESError, null); @@ -77,14 +65,8 @@ public function decrypt($encrypted, $corpid) { try { - $ciphertext_dec = base64_decode($encrypted); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); - mcrypt_generic_init($module, $this->key, $iv); - - $decrypted = mdecrypt_generic($module, $ciphertext_dec); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); + $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); } diff --git a/isv/crypto/DingtalkCrypt.php b/isv/crypto/DingtalkCrypt.php index 2eecd14..58eae24 100755 --- a/isv/crypto/DingtalkCrypt.php +++ b/isv/crypto/DingtalkCrypt.php @@ -11,7 +11,7 @@ class DingtalkCrypt private $m_suiteKey; - public function DingtalkCrypt($token, $encodingAesKey, $suiteKey) + public function __construct($token, $encodingAesKey, $suiteKey) { $this->m_token = $token; $this->m_encodingAesKey = $encodingAesKey; diff --git a/isv/crypto/pkcs7Encoder.php b/isv/crypto/pkcs7Encoder.php index 2b6948c..9d89b53 100755 --- a/isv/crypto/pkcs7Encoder.php +++ b/isv/crypto/pkcs7Encoder.php @@ -9,11 +9,10 @@ class PKCS7Encoder function encode($text) { - $block_size = PKCS7Encoder::$block_size; $text_length = strlen($text); $amount_to_pad = PKCS7Encoder::$block_size - ($text_length % PKCS7Encoder::$block_size); if ($amount_to_pad == 0) { - $amount_to_pad = PKCS7Encoder::block_size; + $amount_to_pad = PKCS7Encoder::$block_size; } $pad_chr = chr($amount_to_pad); $tmp = ""; @@ -39,7 +38,7 @@ class Prpcrypt { public $key; - function Prpcrypt($k) + function __construct($k) { $this->key = base64_decode($k . "="); } @@ -51,22 +50,11 @@ public function encrypt($text, $corpid) //获得16位随机字符串,填充到明文之前 $random = $this->getRandomStr(); $text = $random . pack("N", strlen($text)) . $text . $corpid; - // 网络字节序 - $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); - //使用自定义的填充方式对明文进行补位填充 $pkc_encoder = new PKCS7Encoder; $text = $pkc_encoder->encode($text); - mcrypt_generic_init($module, $this->key, $iv); - //加密 - $encrypted = mcrypt_generic($module, $text); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); - - //print(base64_encode($encrypted)); - //使用BASE64对加密后的字符串进行编码 - return array(ErrorCode::$OK, base64_encode($encrypted)); + $encrypted = openssl_encrypt($text, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv); + return array(ErrorCode::$OK, $encrypted); } catch (Exception $e) { print $e; return array(ErrorCode::$EncryptAESError, null); @@ -77,14 +65,8 @@ public function decrypt($encrypted, $corpid) { try { - $ciphertext_dec = base64_decode($encrypted); - $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, ''); $iv = substr($this->key, 0, 16); - mcrypt_generic_init($module, $this->key, $iv); - - $decrypted = mdecrypt_generic($module, $ciphertext_dec); - mcrypt_generic_deinit($module); - mcrypt_module_close($module); + $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', substr($this->key, 0, 32), OPENSSL_ZERO_PADDING, $iv); } catch (Exception $e) { return array(ErrorCode::$DecryptAESError, null); }