ThinkPHP Rsa加密类怎么用

php中文网
发布: 2016-06-06 20:19:29
原创
3127人浏览过

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

ThinkPHP Rsa加密类怎么用

AppStruct
AppStruct

无代码应用开发平台

AppStruct 132
查看详情 AppStruct

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

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

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

立即学习PHP免费学习笔记(深入)”;

这里的modulus,keylength是什么意思

回复内容:

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

ThinkPHP Rsa加密类怎么用

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

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

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

立即学习PHP免费学习笔记(深入)”;

这里的modulus,keylength是什么意思

我目前使用的类

<?php
namespace 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;
    }
}
?>
登录后复制
相关标签:
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号