Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace mcrypt with openssl for PHP 7.1 compatibility #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion corp/crypto/DingtalkCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 5 additions & 23 deletions corp/crypto/pkcs7Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -39,7 +38,7 @@ class Prpcrypt
{
public $key;

function Prpcrypt($k)
function __construct($k)
{
$this->key = base64_decode($k . "=");
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion isv/crypto/DingtalkCrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 5 additions & 23 deletions isv/crypto/pkcs7Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand All @@ -39,7 +38,7 @@ class Prpcrypt
{
public $key;

function Prpcrypt($k)
function __construct($k)
{
$this->key = base64_decode($k . "=");
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down