/** * @param $app_id * * @return RSA_Process */ public static function instance($app_id) { if (is_null(self::$self)) { self::$self = new self($app_id); } return self::$self; }
/** * 用私钥解密公钥加密内容 * * @param string $data 要解密的数据 * * @return string 解密后的字符串 */ public function decodePublicEncode($data) { $data = str_replace(array_values($this->aReplace), array_keys($this->aReplace), $data); $data = json_decode(base64_decode($data)); $result = ''; if (is_array($data) && count($data) > 0) { foreach ($data as $tmp) { openssl_private_decrypt(base64_decode($tmp), $decrypted, $this->oRSAProcess->getPriKeyId()); $result .= $decrypted; unset($tmp); } } unset($data); if (strlen($result) > 0) { return base64_decode($result); } return NULL; }
/** * 公钥加密 * * @param string $data 要加密的数据 * * @return string 加密后的字符串 */ public function publicKeyEncode($data) { $aEncrypted = array(); $data = base64_encode($data); $len = strlen($data); for ($i = 0; $i < $len; $i += 50) { $tmp = substr($data, $i, 50); openssl_public_encrypt($tmp, $encrypted, $this->oRSAProcess->getPubKeyId()); $aEncrypted[] = base64_encode($encrypted); unset($tmp); unset($encrypted); } unset($data); if (count($aEncrypted) > 0) { $enCodeData = base64_encode(json_encode($aEncrypted)); $enCodeData = str_replace(array_keys($this->aReplace), array_values($this->aReplace), $enCodeData); unset($aEncrypted); return $enCodeData; } return NULL; }