天天看點

php RSA非對稱加密超長字元處理

class Rsa {

    const  RSA_ENCRYPT_BLOCK_SIZE = 117;//加密切割長度
    const  RSA_DECRYPT_BLOCK_SIZE = 128;//解密切割長度

    /**
     * @param $private_key私鑰
     * @return false|resource
     */
    public static function getPrivateKey($private_key)
    {
//        $abs_path = base_path().'/public/rsa/rsa_private_key.pem'; //私鑰檔案方式讀取
//        $content = file_get_contents($abs_path);
//        return openssl_pkey_get_private($content);
        return openssl_pkey_get_private($private_key);
    }

    /**
     * @param $public_key公鑰
     * @return false|resource
     */
    private static function getPublicKey($public_key)
    {
//        $abs_path = base_path().'/public/rsa/rsa_public_key.pem'; //公鑰檔案方式讀取
//        $content = file_get_contents($abs_path);
//        return openssl_pkey_get_public($content);
        return openssl_pkey_get_public($public_key);
    }

    /**
     * 私鑰加密
     * @param $private_key私鑰字元
     * @param string $content 加密字元串
     * @return string|null
     */
    public static function privEncrypt($private_key,$content = '')
    {
        if (!is_string($content)) {
            return null;
        }
        $result='';
        $data = str_split($content, self::RSA_ENCRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_private_encrypt($block, $dataEncrypt, self::getPrivateKey($private_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataEncrypt;
        }
        return $result ? base64_encode($result) : null;
    }

    /**
     * 公鑰加密
     * @param $public_key公鑰字元
     * @param string $content 加密字元串
     * @return string|null
     */
    public static function publicEncrypt($public_key, $content = '')
    {
        if (!is_string($content)) {
            return null;
        }
        $result='';
        $data = str_split($content, self::RSA_ENCRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_public_encrypt($block, $dataEncrypt, self::getPublicKey($public_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataEncrypt;
        }
        return  $result ? base64_encode($result) : null;
    }

    /**
     * 私鑰解密
     * @param $private_key私鑰
     * @param string $encrypted解密字元串
     * @return string|null
     */
    public static function privDecrypt($private_key,$encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        $result = '';
        $data = str_split(base64_decode($encrypted), self::RSA_DECRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_private_decrypt($block, $dataDecrypt, self::getPrivateKey($private_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataDecrypt;
        }
        return $result ? $result : null;
    }

    /**
     * 公鑰解密
     * @param $public_key公鑰
     * @param string $encrypted解密字元串
     * @return string|null
     */
    public static function publicDecrypt($public_key,$encrypted = '')
    {
        if (!is_string($encrypted)) {
            return null;
        }
        $result = '';
        $data = str_split(base64_decode($encrypted), self::RSA_DECRYPT_BLOCK_SIZE);
        foreach ($data as $block) {
            openssl_public_decrypt($block, $dataDecrypt, self::getPublicKey($public_key), OPENSSL_PKCS1_PADDING);
            $result .= $dataDecrypt;
        }
        return $result ? $result : null;
    }

}
           
php