1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP RSA加密解密

作者:用户 来源:互联网 时间:2017-12-01 15:18:44

加密解密

PHP RSA加密解密 - 摘要: 本文讲的是PHP RSA加密解密, Android或IOS需要的公鈅格式可能需要转换。 <?phpclass Rsa {private $private;private $public;function __construct($private, $public =

Android或IOS需要的公鈅格式可能需要转换。

<?phpclass Rsa {private $private;private $public;function __construct($private, $public = null) {$this->private = $private;$this->public = $public;}private function getPrivateKey() {return openssl_pkey_get_private ( $this->private );}private function getPublicKey() {return openssl_pkey_get_public ( $this->public );}function publicEncrypt($data) {if (! is_string ( $data )) {user_error ( 'data must be string', E_USER_WARNING );return;}return openssl_public_encrypt ( $data, $encrypted, $this->getPublicKey () ) ? base64_encode ( $encrypted ) : null;}function publicDecrypt($encrypted) {if (! is_string ( $encrypted )) {user_error ( 'encrypted must be string', E_USER_WARNING );return;}return (openssl_public_decrypt ( base64_decode ( $encrypted ), $decrypted, $this->getPublicKey () )) ? $decrypted : null;}function privateEncrypt($data) {if (! is_string ( $data )) {user_error ( 'data must be string', E_USER_WARNING );return;}return openssl_private_encrypt ( $data, $encrypted, $this->getPrivateKey () ) ? base64_encode ( $encrypted ) : null;}function privateDecrypt($encrypted) {if (! is_string ( $encrypted )) {user_error ( 'encrypted must be string', E_USER_WARNING );return;}return (openssl_private_decrypt ( base64_decode ( $encrypted ), $decrypted, $this->getPrivateKey () )) ? $decrypted : null;}}

base64通过http传输的时候需要处理一下,$data = rawurldecode ( urlencode ( $_GET['data'] ) );

下载:Rsa

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索加密解密 ,以便于您获取更多的相关知识。

弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率

40+云计算产品,6个月免费体验

稳定可靠、可弹性伸缩的在线数据库服务,全球最受欢迎的开源数据库之一

云服务器9.9元/月,大学必备