久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      記錄:php rsa加密處理失敗的解決方法

      關(guān)于php rsa加密處理

      最近剛好需要跟一個(gè)第三方系統(tǒng)對(duì)接幾個(gè)接口,對(duì)方要求 post 數(shù)據(jù)需要 rsa 加密,于是百度搜了一下 php 關(guān)于 rsa 加密的處理,然后大家可能就會(huì)跟我一樣搜出以下示例:

        /**           * @uses 公鑰加密           * @param string $data           * @return null|string           */         public function publicEncrypt($data = '') {                 if (!is_string($data)) {             return null;                 }                 return openssl_public_encrypt($data, $encrypted, $this->_getPublicKey()) ? base64_encode($encrypted) : null;     }

      于是開開心心的復(fù)制到自己項(xiàng)目稍微修改修改后測(cè)試,簡簡單單傳幾個(gè)字符串進(jìn)去:

      <?php $string = '基督教解決基督教解決決'; $ret = publicEncrypt($string); var_dump($ret); /**       * @uses 公鑰加密       * @param string $data       * @return null|string       */     function publicEncrypt($data = '') {         $publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiX1bIq02AFypLOJ4byShfo6+D6pj0rQrdAtZ8Bb2Z4YwdCZS5vlEduBiVCZSKfF70M0nk4gMqhAKcgwqWxgI1/j8OrX401AssfaiXr2JqsAl679s+Xlwe0jppNe1832+3g0YOawDTpAQsUJDu1DpnyGnUz0qeac0/GiAJlXzKUP+/3db8haDuOkgYrT8A6twGAm7YwIuliieDWDcUS/CQzXGRtwtZQqUJDQsWC1lCML1kRUjbZ2EM2EzyttgHN0SsNryhVLHXSFXpDWbeqQwk36axojGF1lbg/oVQy+BnYJx8pKpTgSwIDAQAB';         $publicKey = "-----BEGIN PUBLIC KEY-----n" .     wordwrap($publicKey, 64, "n", true) .     "n-----END PUBLIC KEY-----";     if (!is_string($data)) {         return null;             }             return openssl_public_encrypt($data, $encrypted, $publicKey) ? base64_encode($encrypted) : null; }

      程序打?。?/p>

      string(344) "HSqVQbyhmWYrptvgzK+ggqmma88QRFVJerXTrZ+RpYqhZr/Dr9au9wxX+aAYy1wRh0eBk+fIpU4wkEZs6P5yozf5e/rAAEYUOImTJZcOvZqr89znT3yqaV8ME+vR16FLK5sk3BwgpOWI6X+wBwU2cLnHKDdj9RpYWAYhi/mn8XJj4/srKZbSgAjvzWqZI9gfqiJNdz8kf/MPtQ65cSlAhvh4eByY8cLGfgUXV0dxzWAkwTSPl2faSq3GHsNMXnxwoNjIvqz/IuZavqABNVZCwrZC3ZVb+Op7wF9GxrkIdJYzmHpX/wNn1DPLHUvghtO/WmfN4Jb2ZVzTsneB5B3Z6g=="

      看似一切正常,實(shí)際項(xiàng)目中對(duì)一個(gè)比較長的 json 字符串進(jìn)行加密時(shí),發(fā)現(xiàn)返回了 null,追溯了一下 openssl_public_encrypt 這個(gè)函數(shù)此時(shí)是返回 false 的,表示加密失敗。傳入不同長度的字符串測(cè)試了幾遍后發(fā)現(xiàn)字符串長度超過 100 多之后就會(huì)出現(xiàn)加密失敗的問題,參考了一下對(duì)方發(fā)來的 java 加密示例

          /**      * 用公鑰加密      * @param data      * @param publicKey      * @return      * @throws Exception      */     public static String rsaEncrypt(String data, PublicKey publicKey) throws Exception {         Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);         cipher.init(Cipher.ENCRYPT_MODE, publicKey);         int inputLen = data.getBytes().length;         ByteArrayOutputStream out = new ByteArrayOutputStream();         int offset = 0;         byte[] cache;         int i = 0;         // 對(duì)數(shù)據(jù)分段加密         while (inputLen - offset > 0) {             if (inputLen - offset > MAX_ENCRYPT_BLOCK) {                 cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);             } else {                 cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);             }             out.write(cache, 0, cache.length);             i++;             offset = i * MAX_ENCRYPT_BLOCK;         }         byte[] encryptedData = out.toByteArray();         out.close();         // 加密后的字符串         return Base64.getEncoder().encodeToString(encryptedData);     }

      發(fā)現(xiàn)他們是需要對(duì)要加密的字符串進(jìn)行一個(gè)分割操作,于是有了以下修改后的版本:

      /**  * 公鑰加密   * @param string $data   * @return null|string  */  public function publicEncrypt($data = '')  {     if (!is_string($data)) {         return null;     }     $dataLength = mb_strlen($data);     $offet = 0;     $length = 128;     $i = 0;     $string = '';     while ($dataLength - $offet > 0) {         if ($dataLength - $offet > $length) {             $str = mb_substr($data, $offet, $length);         } else {             $str = mb_substr($data, $offet, $dataLength - $offet);         }         $encrypted = '';         openssl_public_encrypt($str,$encrypted, $this->rsaPublicKey, OPENSSL_PKCS1_OAEP_PADDING);//這個(gè)OPENSSL_PKCS1_OAEP_PADDING是對(duì)方要求要用這種padding方式         $string .= $encrypted;         $i ++;         $offet = $i * $length;     }     return base64_encode($string); }

      目前測(cè)試沒有再發(fā)現(xiàn)加密失敗問題~問題解決

      推薦:《PHP視頻教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)