java的RSA加密类用PHP怎么实现,需要知道具体的实际效果或者类

1、java的RSA加密类用PHP怎么实现,需要知道具体的实际效果或者类,原理我都是理解的
2、使用私钥对信息生成数字签名
图片描述

3、字符拼接后进行下面执行
图片描述

4、sign的值正确的是
B0076C2O3J2Z%2BmBcYHUBmefpEioS3MgzTUbRdhcG3DhP9lv2DdggXyWWP7LozRiNidGiZbdvaIwf%0D%0A32h9DIWEFIjKrPQu1reOkz%2Ft69p6RDwR7tuFYvs6PXcoa%2Bd7RzKwZWu3z5JhoSJq7rNmKqr0Zwg%2F%0D%0A6D6G3SzE%2F4i1gEflNuM%3D

5、解决了,下面的答案,谢谢各位大佬
图片描述

阅读 3.6k
3 个回答

搞过一次,代码不记得了,记得当时的坑是java用md5,php里需要用md5($a, true)来对应,其他的好像没啥障碍就调通了。

可以通过安装 openssl扩展

接续http_query的事,就不管了,只说RSA加解密、签名和验证的

<?php
    namespace models\tool;
    Class RSA {
        const password = 'password';
        const expires  = 36500;
        /**
        *生成公钥和私钥,如果已经有了,直接调用其它静态方法就行了
        *@param $publicKey 公钥
        *@param $privateKey 私钥
        */
        public function __construct(&$publicKey, &$privateKey) {
            $dn = array (
                "countryName"            => 'CN',
                "stateOrProvinceName"    => 'bj',
                "localityName"           => 'bj',
                "organizationName"       => 'bjcom',
                "organizationalUnitName" => 'bj',
                "commonName"             => 'beijing ',
                "emailAddress"           => 'user@beijing.beigjin.com'
            );
            //RSA encryption and 1024 bits length
            $res_private = openssl_pkey_new(array (
                'private_key_bits' => 1024,
                'private_key_type' => OPENSSL_KEYTYPE_RSA
            ));
            $res_csr     = openssl_csr_new($dn, $res_private);
            $res_cert    = openssl_csr_sign($res_csr, null, $res_private, static::expires);
            $res_pubkey  = openssl_pkey_get_public($res_cert);
            openssl_pkey_export($res_private, $privateKey);
            $publicKeyDetail = openssl_pkey_get_details($res_pubkey);
            $publicKey       = $publicKeyDetail['key'];


        }
        /**
        *@param $pubKey 公钥
        *@param $source 要加密的字符串
        */
        public static function en($pubKey, $source) {
            openssl_get_publickey($pubKey);
            $crt = '';
            $r   = openssl_public_encrypt($source, $crt, $pubKey);
            return $r === false ? false : base64_encode($crt);
        }
        
        /**
        *@param $priKey 密钥
        *@pram $source 要解密的字符串
        */
        public static function de($priKey, $source) {
            $crypttext = base64_decode($source);
            $res1      = openssl_get_privatekey($priKey, static::password);
            $str       = '';
            $r         = openssl_private_decrypt($crypttext, $str, $res1);
            return $r === false ? false : $str;
        }
        
        /**
        *@param $str 要签名的字符串
        *@param $priKey 私钥
        */
        public static function sign($str, $priKey) {
            $priKeyRes = openssl_pkey_get_private($priKey);
            openssl_sign($str, $signature, $priKeyRes, 'sha1WithRSAEncryption');
            openssl_free_key($priKeyRes);
            $signature = base64_encode($signature);
            return $signature;
        }

        /**
        *@param $str 被签名的字符串
        *@param $sign 签名
        *@param $pubKey 公钥
        */
        public static function verify($str, $sign, $pubKey) {
            $pubKeyRes = openssl_get_publickey($pubKey);
            $result    = openssl_verify($str, base64_decode($sign), $pubKeyRes, 'sha1WithRSAEncryption');
            openssl_free_key($pubKeyRes);
            return $result;
        }

       
    }
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题