关于php rsa加密处理最近刚好需要跟一个第三方系统对接几个接口,对方要求 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加密处理失败的解决方法的详细内容。