injekt / openapi-demo-php

96 stars 112 forks source link

mcrypt_module_open() 有替代方案可用吗? #22

Open xiangzidetiandi opened 6 years ago

xiangzidetiandi commented 6 years ago

mcrypt_module_open() 有替代方案可用吗?

fisher-89 commented 6 years ago

要用openssl代替mcrypt $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

LTaooo commented 4 years ago

要用openssl代替mcrypt $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

非常感谢了

sunweij commented 4 years ago

php7.2.1-nts(windows) Prpcrypt类中的构造函数写法是一个坑,难道就我发现吗,改成__construct就行了,也不用替换任何函数

ByteByteDance commented 4 years ago

php7.2.1-nts(windows) Prpcrypt类中的构造函数写法是一个坑,难道就我发现吗,改成__construct就行了,也不用替换任何函数

是因为这个sdk太老了

miaowangjian commented 1 year ago

pkcs7Encoder.php 文件,解密解密两个函数替换为:

public function encrypt($text, $corpid)
{
    try {
        $random = $this->getRandomStr();
        $text = $random . pack("N", strlen($text)) . $text . $corpid;
        $iv = substr($this->key, 0, 16);
        $pkc_encoder = new PKCS7Encoder;
        $text = $pkc_encoder->encode($text);
        $encrypted = openssl_encrypt($text, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);

        //print(base64_encode($encrypted));
        //使用BASE64对加密后的字符串进行编码
        return array(ErrorCode::$OK, base64_encode($encrypted));
    } catch (Exception $e) {
        print $e;
        return array(ErrorCode::$EncryptAESError, null);
    }
}

public function decrypt($encrypted, $corpid)
{
    try {
        $ciphertext_dec = base64_decode($encrypted);
        $iv = substr($this->key, 0, 16);
        $decrypted = openssl_decrypt($ciphertext_dec, 'AES-256-CBC', $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
    } catch (Exception $e) {
        return array(ErrorCode::$DecryptAESError, null);
    }
    try {
        //去除补位字符
        $pkc_encoder = new PKCS7Encoder;
        $result = $pkc_encoder->decode($decrypted);
        //去除16位随机字符串,网络字节序和AppId
        if (strlen($result) < 16)
            return "";
        $content = substr($result, 16, strlen($result));
        $len_list = unpack("N", substr($content, 0, 4));
        $xml_len = $len_list[1];
        $xml_content = substr($content, 4, $xml_len);
        $from_corpid = substr($content, $xml_len + 4);
    } catch (Exception $e) {
        print $e;
        return array(ErrorCode::$IllegalBuffer, null);
    }
    if ($from_corpid != $corpid)
        return array(ErrorCode::$ValidateCorpidError, null);
    return array(0, $xml_content);
}