java rsa 加密 php解密

public function test123()
    {

        $privateKeyStr = "MIICdgIBA";

        //openssl rsa -in pri.pem -text -out private.txt 
        $content = chunk_split($privateKeyStr, 64, "\n");
        $pem = "-----BEGIN PRIVATE KEY-----\n" . $content . "-----END PRIVATE KEY-----";

        $privateKey = openssl_pkey_get_private($pem);

        $deatils = openssl_pkey_get_details($privateKey)['bits'];
        $data_contens = file_get_contents(APPPATH . 'app/test.txt');
        //这一步 java哪里 传过来的是16进制的 那这里就需要转换为二进制 建议base64吧 这样简单
        $data_contens = hex2bin($data_contens);

        $crypto = '';

        $lens = $deatils / 8;

        foreach (str_split($data_contens, $lens) as $chunk) {

            if (!openssl_private_decrypt($chunk, $decryptData, $privateKey, OPENSSL_PKCS1_PADDING)) {

                $erros = [];
                while ($msg = openssl_error_string()) {
                    array_push($erros, $msg);
                }
            }
            $crypto .= $decryptData;
        }



        var_dump($crypto);
    }

posted @ 2023-07-03 16:23  尘梦  阅读(120)  评论(0编辑  收藏  举报