php使用openssl進行Rsa長資料加密117解密128

WBOY
發布: 2016-07-28 08:28:43
原創
1708 人瀏覽過

PHP使用openssl進行Rsa加密,如果要加密的明文太長則會出錯,解決方法:加密的時候117個字元加密一次,然後把所有的密文拼接成一個密文;解密的時候需要128個字元解密一下,然後拼接成資料。

加密:

<code>    /<span>**</span>
     * 加密
     * <span>@param</span><span>$originalData</span>
     * <span>@return</span> string|void
     <span>*/</span>
    /<span>*function</span> encrypt(<span>$originalData</span>){

        <span>//</span><span>if</span> (openssl_private_encrypt(<span>$originalData</span>, <span>$encryptData</span>, <span>$this</span>->rsaPrivateKey)) {
        <span>if</span> (openssl_public_encrypt(<span>$originalData</span>, <span>$encryptData</span>, <span>$this</span>->rsaPublicKey)) {
            <span>return</span> base64_encode(<span>$encryptData</span>);
        } <span>else</span> {
            <span>return</span> false;
        }
    }<span>*/</span>
    function encrypt(<span>$originalData</span>){

        <span>$crypto</span> = <span>''</span>;

        <span>foreach</span> (str_split(<span>$originalData</span>, <span>117</span>) as <span>$chunk</span>) {

            openssl_public_encrypt(<span>$chunk</span>, <span>$encryptData</span>, <span>$this</span>->rsaPublicKey);

            <span>$crypto</span> .= <span>$encryptData</span>;
        }

        <span>return</span> base64_encode(<span>$crypto</span>);
    }</code>
登入後複製

解密:

<code>    /<span>**</span>
     * 私钥解密
     * <span>@param</span><span>$encryptData</span><span>*/</span>    /<span>*function</span> decrypt(<span>$encryptData</span>){

        <span>//</span><span>if</span> (openssl_public_decrypt(base64_decode(<span>$encryptData</span>), <span>$decryptData</span>, <span>$this</span>->rsaPublicKey)) {
        <span>if</span> (openssl_private_decrypt(base64_decode(<span>$encryptData</span>), <span>$decryptData</span>, <span>$this</span>->rsaPrivateKey)) {

            <span>return</span><span>$decryptData</span>;

        } <span>else</span> {

            <span>return</span> false;
        }
    }<span>*/</span>
    function decrypt(<span>$encryptData</span>){

        <span>$crypto</span> = <span>''</span>;

        <span>foreach</span> (str_split(base64_decode(<span>$encryptData</span>), <span>128</span>) as <span>$chunk</span>) {

            openssl_private_decrypt(<span>$chunk</span>, <span>$decryptData</span>, <span>$this</span>->rsaPrivateKey);

            <span>$crypto</span> .= <span>$decryptData</span>;
        }

        <span>return</span><span>$crypto</span>;
    }</code>
登入後複製

Author:leedaning
本文網址:http://blog.csdn.net/leedaning/article/details/51780511

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了 php使用openssl進行Rsa長資料加密117解密128,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!