最近剛好需要跟一個第三方系統對接幾個接口,對方要求post 資料需要rsa 加密,於是百度搜了一下php 關於rsa 加密的處理,然後大家可能就會跟我一樣搜出以下範例:
/** * @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; }
於是開開心心的複製到自己專案稍微修改修改後測試,簡簡單單傳幾個字串進去:
<?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; }
程式列印:
string(344) "HSqVQbyhmWYrptvgzK+ggqmma88QRFVJerXTrZ+RpYqhZr/Dr9au9wxX+aAYy1wRh0eBk+fIpU4wkEZs6P5yozf5e/rAAEYUOImTJZcOvZqr89znT3yqaV8ME+vR16FLK5sk3BwgpOWI6X+wBwU2cLnHKDdj9RpYWAYhi/mn8XJj4/srKZbSgAjvzWqZI9gfqiJNdz8kf/MPtQ65cSlAhvh4eByY8cLGfgUXV0dxzWAkwTSPl2faSq3GHsNMXnxwoNjIvqz/IuZavqABNVZCwrZC3ZVb+Op7wF9GxrkIdJYzmHpX/wNn1DPLHUvghtO/WmfN4Jb2ZVzTsneB5B3Z6g=="
看似一切正常,實際專案中對一個比較長的json 字串進行加密時,發現返回了null,追溯了一下openssl_public_encrypt 這個函數此時是返回false的,表示加密失敗。傳入不同長度的字串測試了幾遍後發現字串長度超過100 多之後就會出現加密失敗的問題,參考了一下對方發來的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; // 对数据分段加密 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); }
發現他們是需要對要加密的字串進行一個分割操作,於是有了以下修改後的版本:
/** * 公钥加密 * @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);//这个OPENSSL_PKCS1_OAEP_PADDING是对方要求要用这种padding方式 $string .= $encrypted; $i ++; $offet = $i * $length; } return base64_encode($string); }
目前測試沒有再發現加密失敗問題~問題解決
推薦:《PHP影片教學》
以上是記錄:php rsa加密處理失敗的解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!