RSA 加密

App-Api 数据安全

背景

1、接口请求地址和参数暴露
2、重要接口返回数据明文暴露
3、APP登陆状态请求的数据完全性问题
4、代码层的数据安全问题

怎么解决

加密,加密,加密
加密方式 ,MD5,AES,RSA,SHA1 ,异或,base64,urlencode ;

如何做

1、基本参数放入header
2、每次http请求都带sign
3、sign 唯一性保证
4、请求参数、返回数据、返回数据按安全性适当加密
5、access_token

代码实现

/** 数据的传递分两端,前端传数据,后端收数据, 前、后端必须都有着自己独一无二的一套公私钥,前端的公私钥对数据进行加密给后端,后端使用私钥对前端发过来的数据进行解密.

// 对sign 使用RSA 进行加密 解密

<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2018/10/14
* Time: 0:04
*/
namespace app\api\controller\v1;
class Rsa
{
private static $PRIVATE_KEY = ' ';
private static $PUBLIC_KEY = '';
/**
* 获取私钥
* @return bool|resource
*/
public function __construct()
{
self::$PUBLIC_KEY = config('app.public_key');
self::$PRIVATE_KEY = config('app.private_key');
}
private static function getPrivateKey()
{
$privKey = self::$PRIVATE_KEY;
return openssl_pkey_get_private($privKey);
}
/**

* 获取公钥

* @return bool|resource

*/

private static function getPublicKey()

{

$publicKey = self::$PUBLIC_KEY;
//从证书中提取公钥并准备使用
return openssl_pkey_get_public($publicKey);
}
/**
* 私钥加密
* @param string $data
* @return null|string
*/
public static function privEncrypt($data = '')
{
if (!is_string($data)) {
return null;
}
return openssl_private_encrypt($data, $encrypted, self::getPrivateKey()) ?
base64_encode($encrypted) : null;
}
/**
* 公钥加密
* @param string $data
* @return null|string
*/
public static function publicEncrypt($data = '')
{
if (!is_string($data)) {
return null;
}
return openssl_public_encrypt($data, $encrypted, self::getPublicKey()) ? base64_encode($encrypted) : null;
}
/**
* 私钥解密
* @param string $encrypted
* @return null
*/
public static function privDecrypt($encrypted = '')
{
if (!is_string($encrypted)) {
return null;
}
return (openssl_private_decrypt(base64_decode($encrypted), $decrypted, self::getPrivateKey())) ? $decrypted : null;
}
/**
* 公钥解密
* @param string $encrypted
* @return null
*/
public static function publicDecrypt($encrypted = '')
{
if (!is_string($encrypted)) {
return null;
}

return (openssl_public_decrypt(base64_decode($encrypted), $decrypted, self::getPublicKey())) ? $decrypted : null;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值