鍍金池/ 問答/Java  PHP/ java的RSA加密類用PHP怎么實現(xiàn),需要知道具體的實際效果或者類

java的RSA加密類用PHP怎么實現(xiàn),需要知道具體的實際效果或者類

1、java的RSA加密類用PHP怎么實現(xiàn),需要知道具體的實際效果或者類,原理我都是理解的
2、使用私鑰對信息生成數(shù)字簽名
圖片描述

3、字符拼接后進行下面執(zhí)行
圖片描述

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

5、解決了,下面的答案,謝謝各位大佬
圖片描述

回答
編輯回答
野橘

可以通過安裝 openssl擴展

2017年1月11日 17:43
編輯回答
過客

接續(xù)http_query的事,就不管了,只說RSA加解密、簽名和驗證的

<?php
    namespace models\tool;
    Class RSA {
        const password = 'password';
        const expires  = 36500;
        /**
        *生成公鑰和私鑰,如果已經(jīng)有了,直接調(diào)用其它靜態(tài)方法就行了
        *@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;
        }

       
    }
2018年9月22日 18:17
編輯回答
不將就

搞過一次,代碼不記得了,記得當時的坑是java用md5,php里需要用md5($a, true)來對應,其他的好像沒啥障礙就調(diào)通了。

2017年1月12日 12:27