• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ThinkPHP Rsa加密类怎么用

php 搞代码 4年前 (2022-01-25) 7次浏览 已收录 0个评论
文章目录[隐藏]

<body>

百度出来说要加载openssl模块,phpinfo();显示下图

代码如下:
$e=Rsa::encrypt(‘hello’,$publickey);
echo $e;
ec本文来源[email protected]搞@^&代*@码)网9搞代gaodaima码ho Rsa::decrypt($e,$privatekey);

运行时无报错,也没有任何输出;

源码中方法如下,
public static function decrypt($message, $private_key, $modulus, $keylength)

这里的modulus,keylength是什么意思

回复内容:

<body>

百度出来说要加载openssl模块,phpinfo();显示下图

代码如下:
$e=Rsa::encrypt(‘hello’,$publickey);
echo $e;
echo Rsa::decrypt($e,$privatekey);

运行时无报错,也没有任何输出;

源码中方法如下,
public static function decrypt($message, $private_key, $modulus, $keylength)

这里的modulus,keylength是什么意思

我目前使用的类

<code><?phpnamespace libs;class Rsa{    private static $PRIVATE_KEY;    private static $PUBLIC_KEY;    function __construct($pubKey = '', $privKey = '') {        self::$PUBLIC_KEY = $pubKey;        self::$PRIVATE_KEY = $privKey;    }    /**     * Decode a string with URL-safe Base64.     *     * @param string $input A Base64 encoded string     *     * @return string A decoded string     */    public static function urlsafeB64Decode($input)    {        $remainder = strlen($input) % 4;        if ($remainder) {            $padlen = 4 - $remainder;            $input .= str_repeat('=', $padlen);        }        return base64_decode(strtr($input, '-_', '+/'));    }    /**     * Encode a string with URL-safe Base64.     *     * @param string $input The string you want encoded     *     * @return string The base64 encode of what you passed in     */    public static function urlsafeB64Encode($input)    {        return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));    }    /**    *返回对应的私钥(内部类调用)    */    private static function getPrivateKey(){        $privKey = self::$PRIVATE_KEY;        return openssl_pkey_get_private($privKey);          }    /**    *返回对应的公钥(内部类调用)    */    private static function getPublicKey(){        $pubKey = self::$PUBLIC_KEY;        return openssl_pkey_get_public($pubKey);          }     /**     * 私钥加密     */    public static function privEncrypt($data)    {        if(!is_string($data)){            return null;        }        return openssl_private_encrypt($data,$encrypted,self::getPrivateKey())? self::urlsafeB64Encode($encrypted) : null;    }        /**     * 私钥解密     */    public static function privDecrypt($encrypted)    {        if(!is_string($encrypted)){            return null;        }        return (openssl_private_decrypt(self::urlsafeB64Decode($encrypted), $decrypted, self::getPrivateKey()))? $decrypted : null;    }    /**     * 公钥加密     */    public static function pubEncrypt($data)    {        if(!is_string($data)){            return null;        }        return openssl_public_encrypt($data,$encrypted,self::getPublicKey())? self::urlsafeB64Encode($encrypted) : null;    }        /**     * 公钥解密     */    public static function pubDecrypt($encrypted)    {        if(!is_string($encrypted)){            return null;        }        return (openssl_public_decrypt(self::urlsafeB64Decode($encrypted), $decrypted, self::getPublicKey()))? $decrypted : null;    }}?></code>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ThinkPHP Rsa加密类怎么用

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址