android、ios等客户端与PHP服务端验证登录,加密通讯
kongweb
9年前
来自: http://my.oschina.net/u/1987422/blog/624893
1、准备工作,生成RSA 私钥 公钥
openssl genrsa -out rsa_private_key.pem 1024 openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
第一条命令生成原始 RSA私钥文件 rsa_private_key.pem,第二条命令将原始 RSA私钥转换为 pkcs8格式,第三条生成RSA公钥 rsa_public_key.pem 私钥生成对应的公钥,我们将私钥private_key.pem用在服务器端,公钥发放给android跟ios等前端
2、服务器端 PHP
require __DIR__.'/ORG/Rsa.class.php'; require __DIR__.'/ORG/MagicCrypt.class.php'; //RSA私匙解密出公钥加密的AES key $rsaObject = new \V1\ORG\Rsa(); $appkey = $rsaObject->privDecrypt($_POST['rsakey']); $MagicCryptObject = new \V1\ORG\MagicCrypt(); $MagicCryptObject->MagicCrypt($appkey);//用key解密传输数据 $username = $MagicCryptObject->decrypt($_POST['username']); $password = $MagicCryptObject->decrypt($_POST['password']); $qtime = $MagicCryptObject->decrypt($_POST['qtime']); if ($qtime - time() > 3600*24) { //return } $member = $this->member->where('username=?', $username)->select(false);//获取会员 if ($member['password'] == md5(md5($password) . $member['salt'] . md5($password))) {//验证会员 //生成token,用rsa私钥加密并返回密文 token规则可以自定义 $userToken = md5($member['id'].$member['salt'].$appkey); $appAuth['memberid'] = $member['id']; $appAuth['token'] = $userToken; $appAuth['expire'] = time() + 7*24*3600;//7天有效 //存储或更新token 可以考虑不同存储 db redis memcached等 $appAuthOld = $this->model('member_appauth')->find($member['id']); if ($appAuthOld) { $this->model('member_appauth')->update($appAuth,array('memberid'),'memberid=' . $member['id']); } else { $this->model('member_appauth')->insert($appAuth); } var_dump($userToken);exit; }
客户端PHP 模拟 测试用的
require __DIR__.'/ORG/Rsa.class.php'; require __DIR__.'/ORG/MagicCrypt.class.php'; $appkey = 'myandroid-IEMI-20160101-'.date('YmdHis');//随机字符串 AES加密用 可以是根据客户端设备号/安装时间/请求时间等生成 $MagicCryptObject = new \V1\ORG\MagicCrypt(); $MagicCryptObject->MagicCrypt($appkey);//用随机密钥加密传输数据 AES rijndael-128 $post['username'] = $MagicCryptObject->encrypt('ehovel'); $post['password'] = $MagicCryptObject->encrypt('dpx890406???'); $post['qtime'] = $MagicCryptObject->encrypt(time());//客户端请求时间 服务端用来判断有效 $rsaObject = new \V1\ORG\Rsa(); $rsakey = $rsaObject->pubEncrypt($appkey);//用rsa公钥加密 $key $post['rsakey'] = $rsakey; //模拟请求 $url = 'http://www.dmibz-local.com/index.php?s=sapi&c=user&a=login'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $data = curl_exec($ch); $status = curl_getinfo($ch); $errno = curl_errno($ch); curl_close($ch); print_r($data);exit;
客户端 JAVA
参考实现 未测 BASE64 http://git.oschina.net/ehovel/codes/emkrvbhx8oq7c91s4tp3y AES http://git.oschina.net/ehovel/codes/52i4r0zwvkh1cmopngltf RSA http://git.oschina.net/ehovel/codes/2915gjp6t8ih4xkenbqof RSA http://git.oschina.net/ehovel/codes/kcey6uvrhzl79b4tqpwg5
需要注意的是,在初始化Cipher对象时,一定要指明使用"RSA/ECB/PKCS1Padding"格式如Cipher.getInstance("RSA/ECB/PKCS1Padding");打开rsa_public_key.pem文件,将上面代码的RSA_PUBLICE替换成其中内容即可。
客户端 IOS
iOS上没有直接处理RSA加密的API,网上说的大多数也是处理X.509的证书的方法来实现,不过X.509证书是带签名的,在php端openssl_pkey_get_private方法获取密钥时,第二个参数需要传签名,而android端实现X.509证书加密解密较为不易,在这里我们利用ios兼容c程序的特点,利用openssl的api实现rsa的加密解密,代码如下:
参考 http://www.lvtao.net/dev/android_ios_php_openssl.html